Skip to content

Commit 01c9509

Browse files
authored
Merge pull request #17628 from smowton/smowton/admin/go-vendor-dir-extraction-option
Go: add extractor option for vendor-directory extraction
2 parents 6081ba5 + d689db2 commit 01c9509

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

go/codeql-extractor.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ options:
2727
The default is 'false'.
2828
type: string
2929
pattern: "^(false|true)$"
30+
extract_vendor_dirs:
31+
title: Whether to include Go vendor directories in the CodeQL database.
32+
description: >
33+
A value indicating whether Go vendor directories should be included in the CodeQL database.
34+
The default is 'false'.
35+
type: string
36+
pattern: "^(false|true)$"

go/extractor/configurebaseline/configurebaseline.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ type BaselineConfig struct {
2828
func GetConfigBaselineAsJSON(rootDir string) ([]byte, error) {
2929
vendorDirs := make([]string, 0)
3030

31-
if util.IsVendorDirExtractionEnabled() {
31+
extractVendorDirs, _ := util.IsVendorDirExtractionEnabled()
32+
if extractVendorDirs {
3233
// The user wants vendor directories scanned; emit an empty report.
3334
} else {
3435
filepath.WalkDir(rootDir, func(dirPath string, d fs.DirEntry, err error) error {

go/extractor/extractor.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,27 @@ func ExtractWithFlags(buildFlags []string, patterns []string, extractTests bool)
8181
}
8282
}
8383

84-
testMessage := ""
84+
// If CODEQL_EXTRACTOR_GO_[OPTION_]EXTRACT_VENDOR_DIRS is "true", we extract `vendor` directories;
85+
// otherwise (the default) is to exclude them from extraction
86+
includeVendor, oldOptionUsed := util.IsVendorDirExtractionEnabled()
87+
88+
if oldOptionUsed {
89+
log.Println("Warning: obsolete option \"CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS\" was set. Use \"CODEQL_EXTRACTOR_GO_OPTION_EXTRACT_VENDOR_DIRS\" or pass `--extractor-option extract_vendor_dirs=true` instead.")
90+
}
91+
92+
modeNotifications := make([]string, 0, 2)
8593
if extractTests {
86-
testMessage = " (test extraction enabled)"
94+
modeNotifications = append(modeNotifications, "test extraction enabled")
8795
}
88-
log.Printf("Running packages.Load%s.", testMessage)
96+
if includeVendor {
97+
modeNotifications = append(modeNotifications, "extracting vendor directories")
98+
}
99+
100+
modeMessage := strings.Join(modeNotifications, ", ")
101+
if modeMessage != "" {
102+
modeMessage = " (" + modeMessage + ")"
103+
}
104+
log.Printf("Running packages.Load%s.", modeMessage)
89105

90106
// This includes test packages if either we're tracing a `go test` command,
91107
// or if CODEQL_EXTRACTOR_GO_OPTION_EXTRACT_TESTS is set to "true".
@@ -233,9 +249,6 @@ func ExtractWithFlags(buildFlags []string, patterns []string, extractTests bool)
233249
// Construct a list of directory segments to exclude from extraction, starting with ".."
234250
excludedDirs := []string{`\.\.`}
235251

236-
// If CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS is "true", we extract `vendor` directories;
237-
// otherwise (the default) is to exclude them from extraction
238-
includeVendor := util.IsVendorDirExtractionEnabled()
239252
if !includeVendor {
240253
excludedDirs = append(excludedDirs, "vendor")
241254
}

go/extractor/util/extractvendordirs.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"os"
55
)
66

7-
func IsVendorDirExtractionEnabled() bool {
8-
return os.Getenv("CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS") == "true"
7+
func IsVendorDirExtractionEnabled() (bool, bool) {
8+
oldOptionVal := os.Getenv("CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS")
9+
return (oldOptionVal == "true" ||
10+
os.Getenv("CODEQL_EXTRACTOR_GO_OPTION_EXTRACT_VENDOR_DIRS") == "true"), oldOptionVal != ""
911
}

go/ql/integration-tests/extract-vendor/test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
def test(codeql, go):
55
os.environ["CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS"] = "true"
66
codeql.database.create(source_root="src")
7+
8+
def test_extractor_option(codeql, go):
9+
codeql.database.create(source_root="src", extractor_option = "extract_vendor_dirs=true")

0 commit comments

Comments
 (0)