Skip to content

Commit 44a16ce

Browse files
committed
Go: Use Toolchain directives in go.work files, if available
1 parent ea3a3db commit 44a16ce

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

go/extractor/project/project.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@ type GoVersionInfo = util.SemVer
5454
// 1. The Go version specified in the `go.work` file, if any.
5555
// 2. The greatest Go version specified in any `go.mod` file, if any.
5656
func (workspace *GoWorkspace) RequiredGoVersion() util.SemVer {
57-
if workspace.WorkspaceFile != nil && workspace.WorkspaceFile.Go != nil {
58-
// If we have parsed a `go.work` file, return the version number from it.
57+
// If we have parsed a `go.work` file, we prioritise versions from it over those in individual `go.mod`
58+
// files. We are interested in toolchain versions, so if there is an explicit toolchain declaration in
59+
// a `go.work` file, we use that. Otherwise, we fall back to the language version in the `go.work` file
60+
// and use that as toolchain version. If we didn't parse a `go.work` file, then we try to find the
61+
// greatest version contained in `go.mod` files.
62+
if workspace.WorkspaceFile != nil && workspace.WorkspaceFile.Toolchain != nil {
63+
return util.NewSemVer(workspace.WorkspaceFile.Toolchain.Name)
64+
} else if workspace.WorkspaceFile != nil && workspace.WorkspaceFile.Go != nil {
5965
return util.NewSemVer(workspace.WorkspaceFile.Go.Version)
6066
} else if workspace.Modules != nil && len(workspace.Modules) > 0 {
6167
// Otherwise, if we have `go.work` files, find the greatest Go version in those.

0 commit comments

Comments
 (0)