Skip to content

Go: Deal with incorrect toolchain versions #15979

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

Merged
merged 12 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion go/extractor/cli/go-autobuilder/go-autobuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ func installDependencies(workspace project.GoWorkspace) {
} else {
if workspace.Modules == nil {
project.InitGoModForLegacyProject(workspace.BaseDir)
workspace.Modules = project.LoadGoModules([]string{filepath.Join(workspace.BaseDir, "go.mod")})
workspace.Modules = project.LoadGoModules(true, []string{filepath.Join(workspace.BaseDir, "go.mod")})
}

// get dependencies for all modules
Expand Down
15 changes: 15 additions & 0 deletions go/extractor/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,18 @@ func EmitExtractionFailedForProjects(path []string) {
noLocation,
)
}

func EmitInvalidToolchainVersion(goModPath string, version string) {
emitDiagnostic(
"go/autobuilder/invalid-go-toolchain-version",
fmt.Sprintf("`%s` is not a valid Go toolchain version", version),
strings.Join([]string{
"As of Go 1.21, toolchain versions [must use the 1.N.P syntax](https://tip.golang.org/doc/toolchain#version).",
fmt.Sprintf("`%s` in `%s` does not match this syntax and there is no additional `toolchain` directive, which may cause some `go` commands to fail.", version, goModPath),
},
"\n\n"),
severityWarning,
fullVisibility,
&locationStruct{File: goModPath},
)
}
28 changes: 20 additions & 8 deletions go/extractor/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,13 @@ func findGoModFiles(root string) []string {
return util.FindAllFilesWithName(root, "go.mod", "vendor")
}

// A regular expression for the Go toolchain version syntax.
var toolchainVersionRe *regexp.Regexp = regexp.MustCompile(`(?m)^([0-9]+\.[0-9]+\.[0-9]+)$`)

// Given a list of `go.mod` file paths, try to parse them all. The resulting array of `GoModule` objects
// will be the same length as the input array and the objects will contain at least the `go.mod` path.
// If parsing the corresponding file is successful, then the parsed contents will also be available.
func LoadGoModules(goModFilePaths []string) []*GoModule {
func LoadGoModules(emitDiagnostics bool, goModFilePaths []string) []*GoModule {
results := make([]*GoModule, len(goModFilePaths))

for i, goModFilePath := range goModFilePaths {
Expand All @@ -201,6 +204,15 @@ func LoadGoModules(goModFilePaths []string) []*GoModule {
}

results[i].Module = modFile

// If this `go.mod` file specifies a Go language version, that version is `1.21` or greater, and
// there is no `toolchain` directive, check that it is a valid Go toolchain version. Otherwise,
// `go` commands which try to download the right version of the Go toolchain will fail. We detect
// this situation and emit a diagnostic.
if modFile.Toolchain == nil && modFile.Go != nil &&
!toolchainVersionRe.Match([]byte(modFile.Go.Version)) && semver.Compare("v"+modFile.Go.Version, "v1.21.0") >= 0 {
diagnostics.EmitInvalidToolchainVersion(goModFilePath, modFile.Go.Version)
}
}

return results
Expand All @@ -209,7 +221,7 @@ func LoadGoModules(goModFilePaths []string) []*GoModule {
// Given a path to a `go.work` file, this function attempts to parse the `go.work` file. If unsuccessful,
// we attempt to discover `go.mod` files within subdirectories of the directory containing the `go.work`
// file ourselves.
func discoverWorkspace(workFilePath string) GoWorkspace {
func discoverWorkspace(emitDiagnostics bool, workFilePath string) GoWorkspace {
log.Printf("Loading %s...\n", workFilePath)
baseDir := filepath.Dir(workFilePath)
workFileSrc, err := os.ReadFile(workFilePath)
Expand All @@ -223,7 +235,7 @@ func discoverWorkspace(workFilePath string) GoWorkspace {

return GoWorkspace{
BaseDir: baseDir,
Modules: LoadGoModules(goModFilePaths),
Modules: LoadGoModules(emitDiagnostics, goModFilePaths),
DepMode: GoGetWithModules,
ModMode: getModMode(GoGetWithModules, baseDir),
}
Expand All @@ -240,7 +252,7 @@ func discoverWorkspace(workFilePath string) GoWorkspace {

return GoWorkspace{
BaseDir: baseDir,
Modules: LoadGoModules(goModFilePaths),
Modules: LoadGoModules(emitDiagnostics, goModFilePaths),
DepMode: GoGetWithModules,
ModMode: getModMode(GoGetWithModules, baseDir),
}
Expand All @@ -263,7 +275,7 @@ func discoverWorkspace(workFilePath string) GoWorkspace {
return GoWorkspace{
BaseDir: baseDir,
WorkspaceFile: workFile,
Modules: LoadGoModules(goModFilePaths),
Modules: LoadGoModules(emitDiagnostics, goModFilePaths),
DepMode: GoGetWithModules,
ModMode: ModReadonly, // Workspaces only support "readonly"
}
Expand All @@ -286,7 +298,7 @@ func discoverWorkspaces(emitDiagnostics bool) []GoWorkspace {
for i, goModFile := range goModFiles {
results[i] = GoWorkspace{
BaseDir: filepath.Dir(goModFile),
Modules: LoadGoModules([]string{goModFile}),
Modules: LoadGoModules(emitDiagnostics, []string{goModFile}),
DepMode: GoGetWithModules,
ModMode: getModMode(GoGetWithModules, filepath.Dir(goModFile)),
}
Expand All @@ -303,7 +315,7 @@ func discoverWorkspaces(emitDiagnostics bool) []GoWorkspace {

results := make([]GoWorkspace, len(goWorkFiles))
for i, workFilePath := range goWorkFiles {
results[i] = discoverWorkspace(workFilePath)
results[i] = discoverWorkspace(emitDiagnostics, workFilePath)
}

// Add all stray `go.mod` files (i.e. those not referenced by `go.work` files)
Expand Down Expand Up @@ -335,7 +347,7 @@ func discoverWorkspaces(emitDiagnostics bool) []GoWorkspace {
log.Printf("Module %s is not referenced by any go.work file; adding it separately.\n", goModFile)
results = append(results, GoWorkspace{
BaseDir: filepath.Dir(goModFile),
Modules: LoadGoModules([]string{goModFile}),
Modules: LoadGoModules(emitDiagnostics, []string{goModFile}),
DepMode: GoGetWithModules,
ModMode: getModMode(GoGetWithModules, filepath.Dir(goModFile)),
})
Expand Down