Skip to content

Commit 45b7825

Browse files
authored
Merge pull request github#16925 from github/mbg/go/add-vendor-env-var
Go: Add environment variable to include `vendor` directories in extraction
2 parents a452ead + 7ca57e1 commit 45b7825

File tree

12 files changed

+77
-4
lines changed

12 files changed

+77
-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) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"configuration" : {
3+
"go" : { }
4+
}
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"markdownMessage": "A single `go.mod` file was found.\n\n`go.mod`",
3+
"severity": "note",
4+
"source": {
5+
"extractorName": "go",
6+
"id": "go/autobuilder/single-root-go-mod-found",
7+
"name": "A single `go.mod` file was found in the root"
8+
},
9+
"visibility": {
10+
"cliSummaryTable": false,
11+
"statusPage": false,
12+
"telemetry": true
13+
}
14+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# go get has been observed to sometimes fail when multiple tests try to simultaneously fetch the same package.
2+
goget
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
go 1.14
2+
3+
require example.com/test v0.1.0
4+
5+
module test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
example.com/test v0.1.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package test
2+
3+
import (
4+
subdir "example.com/test"
5+
)
6+
7+
func Test() {
8+
9+
foo := subdir.Add(2, 2)
10+
println(foo)
11+
}

go/ql/integration-tests/all-platforms/go/extract-vendor/src/vendor/example.com/test/add.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# example.com/test v0.1.0
2+
## explicit; go 1.14
3+
example.com/test
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extractedFiles
2+
| src/go.mod:0:0:0:0 | src/go.mod |
3+
| src/test.go:0:0:0:0 | src/test.go |
4+
| src/vendor/example.com/test/add.go:0:0:0:0 | src/vendor/example.com/test/add.go |
5+
#select

0 commit comments

Comments
 (0)