Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: Add environment variable to include vendor directories in extraction #16925

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions go/extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,20 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error {
log.Println("Starting to extract packages.")

sep := regexp.QuoteMeta(string(filepath.Separator))
// if a path matches this regexp, we don't want to extract this package. Currently, it checks
// - that the path does not contain a `..` segment, and
// - the path does not contain a `vendor` directory.
noExtractRe := regexp.MustCompile(`.*(^|` + sep + `)(\.\.|vendor)($|` + sep + `).*`)

// Construct a list of directory segments to exclude from extraction, starting with ".."
excludedDirs := []string{`\.\.`}

// If CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS is "true", we extract `vendor` directories;
// otherwise (the default) is to exclude them from extraction
includeVendor := os.Getenv("CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS") == "true"
if !includeVendor {
excludedDirs = append(excludedDirs, "vendor")
}

// If a path matches this regexp, we don't extract this package. It checks whether the path
// contains one of the `excludedDirs`.
noExtractRe := regexp.MustCompile(`.*(^|` + sep + `)(` + strings.Join(excludedDirs, "|") + `)($|` + sep + `).*`)

// extract AST information for all packages
packages.Visit(pkgs, nil, func(pkg *packages.Package) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"configuration" : {
"go" : { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"markdownMessage": "A single `go.mod` file was found.\n\n`go.mod`",
"severity": "note",
"source": {
"extractorName": "go",
"id": "go/autobuilder/single-root-go-mod-found",
"name": "A single `go.mod` file was found in the root"
},
"visibility": {
"cliSummaryTable": false,
"statusPage": false,
"telemetry": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# go get has been observed to sometimes fail when multiple tests try to simultaneously fetch the same package.
goget
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go 1.14

require example.com/test v0.1.0

module test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example.com/test v0.1.0/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package test

import (
subdir "example.com/test"
)

func Test() {

foo := subdir.Add(2, 2)
println(foo)
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# example.com/test v0.1.0
## explicit; go 1.14
example.com/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extractedFiles
| src/go.mod:0:0:0:0 | src/go.mod |
| src/test.go:0:0:0:0 | src/test.go |
| src/vendor/example.com/test/add.go:0:0:0:0 | src/vendor/example.com/test/add.go |
#select
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from go_integration_test import *

os.environ['CODEQL_EXTRACTOR_GO_EXTRACT_VENDOR_DIRS'] = "true"
go_integration_test()
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import go
import semmle.go.DiagnosticsReporting

query predicate extractedFiles(File f) { any() }

from string msg, int sev
where reportableDiagnostics(_, msg, sev)
select msg, sev
Loading