Skip to content

Commit ec90282

Browse files
committed
Go: Initialise go.mod for stray source files
1 parent b9e96e4 commit ec90282

File tree

2 files changed

+11
-6
lines changed
  • go
    • extractor/project
    • ql/integration-tests/all-platforms/go/single-go-mod-and-go-files-not-under-it

2 files changed

+11
-6
lines changed

go/extractor/project/project.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ func getBuildRoots(emitDiagnostics bool) (goWorkspaces []GoWorkspace, totalModul
301301
goWorkspaces = []GoWorkspace{{
302302
BaseDir: ".",
303303
DepMode: GoGetWithModules,
304+
Modules: loadGoModules([]string{"go.mod"}),
304305
ModMode: getModMode(GoGetWithModules, "."),
305306
}}
306307
totalModuleFiles = 1
@@ -324,12 +325,16 @@ func getBuildRoots(emitDiagnostics bool) (goWorkspaces []GoWorkspace, totalModul
324325
diagnostics.EmitGoFilesOutsideGoModules(goModPaths)
325326
}
326327

327-
goWorkspaces = []GoWorkspace{{
328+
// Try to initialize a `go.mod` file automatically for the stray source files.
329+
initGoModForLegacyProject(".")
330+
331+
goWorkspaces = append(goWorkspaces, GoWorkspace{
328332
BaseDir: ".",
329-
DepMode: GoGetNoModules,
333+
Modules: loadGoModules([]string{"go.mod"}),
334+
DepMode: GoGetWithModules,
330335
ModMode: ModUnset,
331-
}}
332-
totalModuleFiles = 0
336+
})
337+
totalModuleFiles += 1
333338
return
334339
}
335340

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extractedFiles
2+
| src/go.mod:0:0:0:0 | src/go.mod |
23
| src/main.go:0:0:0:0 | src/main.go |
4+
| src/subdir/go.mod:0:0:0:0 | src/subdir/go.mod |
35
| src/subdir/subsubdir/add.go:0:0:0:0 | src/subdir/subsubdir/add.go |
46
| src/subdir/test.go:0:0:0:0 | src/subdir/test.go |
57
#select
6-
| 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 |
7-
| Extraction failed in subdir/test.go with error could not import subdir/subsubdir (invalid package name: "") | 2 |

0 commit comments

Comments
 (0)