Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f013d9d

Browse files
committed
Go: Use new workspace/module discovery
1 parent fc75e44 commit f013d9d

19 files changed

Lines changed: 101 additions & 75 deletions

File tree

go/extractor/project/project.go

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,34 @@ func discoverWorkspaces(emitDiagnostics bool) []GoWorkspace {
186186
// Returns the directory to run the go build in and whether to use a go.mod
187187
// file.
188188
func getBuildRoot(emitDiagnostics bool) (baseDirs []string, useGoMod bool) {
189-
goModPaths := util.FindAllFilesWithName(".", "go.mod", "vendor")
190-
if len(goModPaths) == 0 {
189+
goWorkspaces := discoverWorkspaces(emitDiagnostics)
190+
191+
// Determine the total number of `go.mod` files that we discovered.
192+
totalModuleFiles := 0
193+
194+
for _, goWorkspace := range goWorkspaces {
195+
totalModuleFiles += len(goWorkspace.Modules)
196+
}
197+
198+
// If there are no `go.mod` files at all, try to build the project without Go modules.
199+
if totalModuleFiles == 0 {
200+
log.Println("Found no go.mod files, not using Go modules.")
191201
baseDirs = []string{"."}
192202
useGoMod = false
193203
return
194204
}
205+
206+
// Get the paths to all `go.mod` files
207+
i := 0
208+
goModPaths := make([]string, totalModuleFiles)
209+
210+
for _, goWorkspace := range goWorkspaces {
211+
for _, goModule := range goWorkspace.Modules {
212+
goModPaths[i] = goModule.Path
213+
i++
214+
}
215+
}
216+
195217
goModDirs := util.GetParentDirs(goModPaths)
196218
if util.AnyGoFilesOutsideDirs(".", goModDirs...) {
197219
if emitDiagnostics {
@@ -201,21 +223,26 @@ func getBuildRoot(emitDiagnostics bool) (baseDirs []string, useGoMod bool) {
201223
useGoMod = false
202224
return
203225
}
204-
if len(goModPaths) > 1 {
205-
// currently not supported
206-
baseDirs = []string{"."}
207-
commonRoot, nested := checkDirsNested(goModDirs)
208-
if nested && commonRoot == "" {
209-
useGoMod = true
210-
} else {
211-
useGoMod = false
212-
}
213-
if emitDiagnostics {
226+
227+
// If we are emitted diagnostics, report some details about the workspace structure.
228+
if emitDiagnostics {
229+
if totalModuleFiles > 1 {
230+
_, nested := checkDirsNested(goModDirs)
231+
214232
if nested {
215233
diagnostics.EmitMultipleGoModFoundNested(goModPaths)
216234
} else {
217235
diagnostics.EmitMultipleGoModFoundNotNested(goModPaths)
218236
}
237+
} else if totalModuleFiles == 1 {
238+
if goModDirs[0] == "." {
239+
diagnostics.EmitSingleRootGoModFound(goModPaths[0])
240+
} else {
241+
diagnostics.EmitSingleNonRootGoModFound(goModPaths[0])
242+
}
243+
}
244+
}
245+
219246
baseDirs = goModDirs
220247
useGoMod = true
221248
return
@@ -246,14 +273,6 @@ func getDepMode(emitDiagnostics bool) (DependencyInstallerMode, []string) {
246273
}
247274
}
248275

249-
goWorkPaths := util.FindAllFilesWithName(".", "go.work", "vendor")
250-
if len(goWorkPaths) > 0 {
251-
// currently not supported
252-
if emitDiagnostics {
253-
diagnostics.EmitGoWorkFound(goWorkPaths)
254-
}
255-
}
256-
257276
baseDirs, useGoMod := getBuildRoot(emitDiagnostics)
258277
if useGoMod {
259278
log.Printf("Found go.mod files in %s: enabling go modules.\n", strings.Join(baseDirs, ", "))

go/ql/integration-tests/all-platforms/go/diagnostics/go-files-found-not-processed/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
goPath = os.path.join(os.path.abspath(os.getcwd()), ".go")
1111
os.environ['GOPATH'] = goPath
1212
os.environ['LGTM_INDEX_IMPORT_PATH'] = "test"
13-
run_codeql_database_create([], lang="go", source="work", db=None, runFunction=runUnsuccessfully)
13+
run_codeql_database_create([], lang="go", source="work", db=None)
1414

1515
check_diagnostics()
1616

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
extractedFiles
2+
| src/main.go:0:0:0:0 | src/main.go |
3+
| src/subdir/subsubdir/add.go:0:0:0:0 | src/subdir/subsubdir/add.go |
4+
| src/subdir/test.go:0:0:0:0 | src/subdir/test.go |
5+
#select
16
| Extraction failed in subdir/test.go with error cannot find package "subdir/subsubdir" in any of:\n\t(absolute path) (from $GOROOT)\n\t(absolute path) (from $GOPATH) | 2 |
27
| Extraction failed in subdir/test.go with error could not import subdir/subsubdir (invalid package name: "") | 2 |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import go
22
import semmle.go.DiagnosticsReporting
33

4+
query predicate extractedFiles(File f) { any() }
5+
46
from string msg, int sev
57
where reportableDiagnostics(_, msg, sev)
68
select msg, sev
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/subdir/add.go:0:0:0:0 | src/subdir/add.go |
4+
| src/test.go:0:0:0:0 | src/test.go |
5+
#select
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import go
22
import semmle.go.DiagnosticsReporting
33

4+
query predicate extractedFiles(File f) { any() }
5+
46
from string msg, int sev
57
where reportableDiagnostics(_, msg, sev)
68
select msg, sev
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extractedFiles
2+
| src/subdir/go.mod:0:0:0:0 | src/subdir/go.mod |
3+
| src/subdir/subsubdir/add.go:0:0:0:0 | src/subdir/subsubdir/add.go |
4+
| src/subdir/test.go:0:0:0:0 | src/subdir/test.go |
5+
#select
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import go
22
import semmle.go.DiagnosticsReporting
33

4+
query predicate extractedFiles(File f) { any() }
5+
46
from string msg, int sev
57
where reportableDiagnostics(_, msg, sev)
68
select msg, sev

go/ql/integration-tests/all-platforms/go/single-go-work-not-in-root/diagnostics.expected

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,3 @@
2626
"telemetry": true
2727
}
2828
}
29-
{
30-
"markdownMessage": "2 packages could not be found:\n\n`subdir1/subsubdir1`, `subdir2/subsubdir2`.\n\nDefinitions in those packages may not be recognized by CodeQL, and files that use them may only be partially analyzed.\n\nCheck that the paths are correct and make sure any private packages can be accessed. If any of the packages are present in the repository then you may need a [custom build command](https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-the-codeql-workflow-for-compiled-languages).",
31-
"severity": "warning",
32-
"source": {
33-
"extractorName": "go",
34-
"id": "go/autobuilder/package-not-found",
35-
"name": "Some packages could not be found"
36-
},
37-
"visibility": {
38-
"cliSummaryTable": true,
39-
"statusPage": true,
40-
"telemetry": true
41-
}
42-
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
| Extraction failed in modules/subdir1/test.go with error cannot find package "subdir1/subsubdir1" in any of:\n\t(absolute path) (from $GOROOT)\n\t(absolute path) (from $GOPATH) | 2 |
2-
| Extraction failed in modules/subdir1/test.go with error could not import subdir1/subsubdir1 (invalid package name: "") | 2 |
3-
| Extraction failed in modules/subdir2/test.go with error cannot find package "subdir2/subsubdir2" in any of:\n\t(absolute path) (from $GOROOT)\n\t(absolute path) (from $GOPATH) | 2 |
4-
| Extraction failed in modules/subdir2/test.go with error could not import subdir2/subsubdir2 (invalid package name: "") | 2 |
1+
extractedFiles
2+
| src/modules/subdir1/go.mod:0:0:0:0 | src/modules/subdir1/go.mod |
3+
| src/modules/subdir1/subsubdir1/add.go:0:0:0:0 | src/modules/subdir1/subsubdir1/add.go |
4+
| src/modules/subdir1/test.go:0:0:0:0 | src/modules/subdir1/test.go |
5+
| src/modules/subdir2/go.mod:0:0:0:0 | src/modules/subdir2/go.mod |
6+
| src/modules/subdir2/subsubdir2/add.go:0:0:0:0 | src/modules/subdir2/subsubdir2/add.go |
7+
| src/modules/subdir2/test.go:0:0:0:0 | src/modules/subdir2/test.go |
8+
#select

0 commit comments

Comments
 (0)