Skip to content

Commit 7ca57e1

Browse files
committed
Go: Add CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS env var
If set to `true`, this allows `vendor` directories to be extracted
1 parent bc61a58 commit 7ca57e1

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

go/extractor/extractor.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,20 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
193193
log.Println("Starting to extract packages.")
194194

195195
sep := regexp.QuoteMeta(string(filepath.Separator))
196-
// if a path matches this regexp, we don't want to extract this package. Currently, it checks
197-
// - that the path does not contain a `..` segment, and
198-
// - the path does not contain a `vendor` directory.
199-
noExtractRe := regexp.MustCompile(`.*(^|` + sep + `)(\.\.|vendor)($|` + sep + `).*`)
196+
197+
// Construct a list of directory segments to exclude from extraction, starting with ".."
198+
excludedDirs := []string{`\.\.`}
199+
200+
// If CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS is "true", we extract `vendor` directories;
201+
// otherwise (the default) is to exclude them from extraction
202+
includeVendor := os.Getenv("CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS") == "true"
203+
if !includeVendor {
204+
excludedDirs = append(excludedDirs, "vendor")
205+
}
206+
207+
// If a path matches this regexp, we don't extract this package. It checks whether the path
208+
// contains one of the `excludedDirs`.
209+
noExtractRe := regexp.MustCompile(`.*(^|` + sep + `)(` + strings.Join(excludedDirs, "|") + `)($|` + sep + `).*`)
200210

201211
// extract AST information for all packages
202212
packages.Visit(pkgs, nil, func(pkg *packages.Package) {

0 commit comments

Comments
 (0)