|
5 | 5 | "os" |
6 | 6 | "path/filepath" |
7 | 7 | "regexp" |
| 8 | + "slices" |
8 | 9 | "sort" |
9 | 10 | "strings" |
10 | 11 |
|
@@ -411,16 +412,32 @@ func getBuildRoots(emitDiagnostics bool) (goWorkspaces []GoWorkspace, totalModul |
411 | 412 | diagnostics.EmitGoFilesOutsideGoModules(goModPaths) |
412 | 413 | } |
413 | 414 |
|
414 | | - // Try to initialize a `go.mod` file automatically for the stray source files. |
415 | | - initGoModForLegacyProject(".") |
| 415 | + // We need to initialise Go modules for the stray source files. Our goal is to initialise |
| 416 | + // as few Go modules as possible, in locations which do not overlap with existing Go |
| 417 | + // modules. |
| 418 | + for _, straySourceFile := range straySourceFiles { |
| 419 | + path := "." |
| 420 | + components := strings.Split(filepath.Dir(straySourceFile), string(os.PathSeparator)) |
| 421 | + |
| 422 | + for _, component := range components { |
| 423 | + path = filepath.Join(path, component) |
| 424 | + |
| 425 | + // Try to initialize a `go.mod` file automatically for the stray source files. |
| 426 | + if !slices.Contains(goModDirs, path) { |
| 427 | + initGoModForLegacyProject(path) |
| 428 | + goWorkspaces = append(goWorkspaces, GoWorkspace{ |
| 429 | + BaseDir: path, |
| 430 | + Modules: loadGoModules([]string{filepath.Join(path, "go.mod")}), |
| 431 | + DepMode: GoGetWithModules, |
| 432 | + ModMode: ModUnset, |
| 433 | + }) |
| 434 | + totalModuleFiles += 1 |
| 435 | + goModDirs = append(goModDirs, path) |
| 436 | + break |
| 437 | + } |
| 438 | + } |
| 439 | + } |
416 | 440 |
|
417 | | - goWorkspaces = append(goWorkspaces, GoWorkspace{ |
418 | | - BaseDir: ".", |
419 | | - Modules: loadGoModules([]string{"go.mod"}), |
420 | | - DepMode: GoGetWithModules, |
421 | | - ModMode: ModUnset, |
422 | | - }) |
423 | | - totalModuleFiles += 1 |
424 | 441 | return |
425 | 442 | } |
426 | 443 |
|
|
0 commit comments