1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package gover 6 7 import "golang.org/x/mod/modfile" 8 9 const ( 10 // narrowAllVersion is the Go version at which the 11 // module-module "all" pattern no longer closes over the dependencies of 12 // tests outside of the main module. 13 NarrowAllVersion = "1.16" 14 15 // DefaultGoModVersion is the Go version to assume for go.mod files 16 // that do not declare a Go version. The go command has been 17 // writing go versions to modules since Go 1.12, so a go.mod 18 // without a version is either very old or recently hand-written. 19 // Since we can't tell which, we have to assume it's very old. 20 // The semantics of the go.mod changed at Go 1.17 to support 21 // graph pruning. If see a go.mod without a go line, we have to 22 // assume Go 1.16 so that we interpret the requirements correctly. 23 // Note that this default must stay at Go 1.16; it cannot be moved forward. 24 DefaultGoModVersion = "1.16" 25 26 // DefaultGoWorkVersion is the Go version to assume for go.work files 27 // that do not declare a Go version. Workspaces were added in Go 1.18, 28 // so use that. 29 DefaultGoWorkVersion = "1.18" 30 31 // ExplicitIndirectVersion is the Go version at which a 32 // module's go.mod file is expected to list explicit requirements on every 33 // module that provides any package transitively imported by that module. 34 // 35 // Other indirect dependencies of such a module can be safely pruned out of 36 // the module graph; see https://golang.org/ref/mod#graph-pruning. 37 ExplicitIndirectVersion = "1.17" 38 39 // SeparateIndirectVersion is the Go version at which 40 // "// indirect" dependencies are added in a block separate from the direct 41 // ones. See https://golang.org/issue/45965. 42 SeparateIndirectVersion = "1.17" 43 44 // SimplifyRequireVersion is the Go version at which 45 // require blocks are simplified to always have at most two sections 46 // (one direct, one indirect). See https://go.dev/issue/56471. 47 SimplifyRequireVersion = "1.27" 48 49 // tidyGoModSumVersion is the Go version at which 50 // 'go mod tidy' preserves go.mod checksums needed to build test dependencies 51 // of packages in "all", so that 'go test all' can be run without checksum 52 // errors. 53 // See https://go.dev/issue/56222. 54 TidyGoModSumVersion = "1.21" 55 56 // goStrictVersion is the Go version at which the Go versions 57 // became "strict" in the sense that, restricted to modules at this version 58 // or later, every module must have a go version line ≥ all its dependencies. 59 // It is also the version after which "too new" a version is considered a fatal error. 60 GoStrictVersion = "1.21" 61 62 // ExplicitModulesTxtImportVersion is the Go version at which vendored packages need to be present 63 // in modules.txt to be imported. 64 ExplicitModulesTxtImportVersion = "1.23" 65 66 // GoModToolVersion is the Go version required for the tool 67 // directive in go.mod. 68 GoModToolVersion = "1.24" 69 ) 70 71 // FromGoMod returns the go version from the go.mod file. 72 // It returns DefaultGoModVersion if the go.mod file does not contain a go line or if mf is nil. 73 func FromGoMod(mf *modfile.File) string { 74 if mf == nil || mf.Go == nil { 75 return DefaultGoModVersion 76 } 77 return mf.Go.Version 78 } 79 80 // FromGoWork returns the go version from the go.mod file. 81 // It returns DefaultGoWorkVersion if the go.mod file does not contain a go line or if wf is nil. 82 func FromGoWork(wf *modfile.WorkFile) string { 83 if wf == nil || wf.Go == nil { 84 return DefaultGoWorkVersion 85 } 86 return wf.Go.Version 87 } 88