Skip to content

Commit 1b2d842

Browse files
authored
Merge pull request #17900 from github/mbg/go/fix/project-files-in-vendor
2 parents 32e4c74 + 11e3a08 commit 1b2d842

File tree

10 files changed

+48
-71
lines changed

10 files changed

+48
-71
lines changed

go/extractor/configurebaseline/configurebaseline.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,12 @@ package configurebaseline
33
import (
44
"encoding/json"
55
"io/fs"
6-
"os"
76
"path"
87
"path/filepath"
98

109
"github.com/github/codeql-go/extractor/util"
1110
)
1211

13-
func fileExists(path string) bool {
14-
stat, err := os.Stat(path)
15-
return err == nil && stat.Mode().IsRegular()
16-
}
17-
18-
// Decides if `dirPath` is a vendor directory by testing whether it is called `vendor`
19-
// and contains a `modules.txt` file.
20-
func isGolangVendorDirectory(dirPath string) bool {
21-
return filepath.Base(dirPath) == "vendor" && fileExists(filepath.Join(dirPath, "modules.txt"))
22-
}
23-
2412
type BaselineConfig struct {
2513
PathsIgnore []string `json:"paths-ignore"`
2614
}
@@ -38,7 +26,7 @@ func GetConfigBaselineAsJSON(rootDir string) ([]byte, error) {
3826
// it will not be extracted either.
3927
return nil
4028
}
41-
if isGolangVendorDirectory(dirPath) {
29+
if util.IsGolangVendorDirectory(dirPath) {
4230
// Note that CodeQL expects a forward-slash-separated path, even on Windows.
4331
vendorDirs = append(vendorDirs, path.Join(filepath.ToSlash(dirPath), "**"))
4432
return filepath.SkipDir

go/extractor/project/project.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ func RemoveTemporaryExtractorFiles() {
184184

185185
// Find all go.work files in the working directory and its subdirectories
186186
func findGoWorkFiles() []string {
187-
return util.FindAllFilesWithName(".", "go.work", "vendor")
187+
return util.FindAllFilesWithName(".", "go.work", util.SkipVendorChecks...)
188188
}
189189

190190
// Find all go.mod files in the specified directory and its subdirectories
191191
func findGoModFiles(root string) []string {
192-
return util.FindAllFilesWithName(root, "go.mod", "vendor")
192+
return util.FindAllFilesWithName(root, "go.mod", util.SkipVendorChecks...)
193193
}
194194

195195
// A regular expression for the Go toolchain version syntax.
@@ -315,6 +315,11 @@ func discoverWorkspaces(emitDiagnostics bool) []GoWorkspace {
315315
goModFiles := findGoModFiles(".")
316316

317317
// Return a separate workspace for each `go.mod` file that we found.
318+
if len(goModFiles) > 0 {
319+
log.Printf("Found %d go.mod files in: %s.\n", len(goModFiles), strings.Join(goModFiles, ", "))
320+
} else {
321+
log.Println("Found no go.mod files in the workspace.")
322+
}
318323
results := make([]GoWorkspace, len(goModFiles))
319324

320325
for i, goModFile := range goModFiles {
@@ -547,8 +552,8 @@ func startsWithAnyOf(str string, prefixes []string) bool {
547552
// Finds Go workspaces in the current working directory.
548553
func GetWorkspaceInfo(emitDiagnostics bool) []GoWorkspace {
549554
bazelPaths := slices.Concat(
550-
util.FindAllFilesWithName(".", "BUILD", "vendor"),
551-
util.FindAllFilesWithName(".", "BUILD.bazel", "vendor"),
555+
util.FindAllFilesWithName(".", "BUILD", util.SkipVendorChecks...),
556+
util.FindAllFilesWithName(".", "BUILD.bazel", util.SkipVendorChecks...),
552557
)
553558
if len(bazelPaths) > 0 {
554559
// currently not supported

go/extractor/util/util.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,24 @@ func FindGoFiles(root string) bool {
152152
return found
153153
}
154154

155-
func FindAllFilesWithName(root string, name string, dirsToSkip ...string) []string {
155+
// The type of check function used by `FindAllFilesWithName` to decide whether to skip the directory named by `path`.
156+
type FindAllFilesWithNameSkipCheck func(path string) bool
157+
158+
// Commonly we only want to skip `vendor` directories in `FindAllFilesWithName`. This array is a suitable
159+
// argument for `dirsToSkip` which skips `vendor` directories.
160+
var SkipVendorChecks = []FindAllFilesWithNameSkipCheck{IsGolangVendorDirectory}
161+
162+
// Returns an array of all files matching `name` within the path at `root`.
163+
// The `dirsToSkip` array contains check functions used to decide which directories to skip.
164+
func FindAllFilesWithName(root string, name string, dirsToSkip ...FindAllFilesWithNameSkipCheck) []string {
156165
paths := make([]string, 0, 1)
157166
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
158167
if err != nil {
159168
return err
160169
}
161170
if d.IsDir() {
162171
for _, dirToSkip := range dirsToSkip {
163-
if path == dirToSkip {
172+
if dirToSkip(path) {
164173
return filepath.SkipDir
165174
}
166175
}
@@ -287,3 +296,16 @@ func getImportPathFromRepoURL(repourl string) string {
287296
path = regexp.MustCompile(`^/+|\.git$`).ReplaceAllString(path, "")
288297
return host + "/" + path
289298
}
299+
300+
// Decides if `path` refers to a file that exists.
301+
func fileExists(path string) bool {
302+
stat, err := os.Stat(path)
303+
return err == nil && stat.Mode().IsRegular()
304+
}
305+
306+
// Decides if `dirPath` is a vendor directory by testing whether it is called `vendor`
307+
// and contains a `modules.txt` file.
308+
func IsGolangVendorDirectory(dirPath string) bool {
309+
return filepath.Base(dirPath) == "vendor" &&
310+
(fileExists(filepath.Join(dirPath, "modules.txt")) || fileExists(filepath.Join(dirPath, "../glide.yaml")))
311+
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
go 1.14
22

3-
require golang.org/x/net v0.23.0
4-
53
module module
Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +0,0 @@
1-
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
2-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
4-
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
5-
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
6-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
7-
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
8-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
9-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
10-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
11-
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
12-
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
13-
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
14-
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
15-
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
16-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
17-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
18-
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
19-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
20-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
21-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
22-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
23-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
24-
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
25-
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
26-
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
27-
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
28-
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
29-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
30-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
31-
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
32-
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
33-
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
34-
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
35-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
36-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
37-
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
38-
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
39-
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
40-
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
41-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
42-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
43-
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
44-
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
45-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

go/ql/integration-tests/mixed-layout/src/module/test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ package subdir
22

33
import (
44
"fmt"
5-
6-
"golang.org/x/net/ipv4"
75
)
86

97
func test() {
10-
11-
header := ipv4.Header{}
12-
fmt.Print(header.String())
8+
fmt.Print("Hello world")
139
}

go/ql/integration-tests/mixed-layout/src/module/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.

go/ql/integration-tests/mixed-layout/src/module/vendor/example.com/test/go.work

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# example.com/test v0.1.0
2+
example.com/test
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
2+
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
3+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=

0 commit comments

Comments
 (0)