]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/go: move version constants from modload to gover
authorRuss Cox <rsc@golang.org>
Thu, 1 Jun 2023 16:55:58 +0000 (12:55 -0400)
committerGopher Robot <gobot@golang.org>
Thu, 1 Jun 2023 21:12:04 +0000 (21:12 +0000)
For #57001.

Change-Id: Ia76478b8eaa934b7e1dc1e9cd7fe8a2428fc291a
Reviewed-on: https://go-review.googlesource.com/c/go/+/499978
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Russ Cox <rsc@golang.org>

src/cmd/go/internal/gover/version.go [new file with mode: 0644]
src/cmd/go/internal/modcmd/download.go
src/cmd/go/internal/modload/buildlist.go
src/cmd/go/internal/modload/init.go
src/cmd/go/internal/modload/load.go
src/cmd/go/internal/modload/modfile.go

diff --git a/src/cmd/go/internal/gover/version.go b/src/cmd/go/internal/gover/version.go
new file mode 100644 (file)
index 0000000..ca47021
--- /dev/null
@@ -0,0 +1,54 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package gover
+
+const (
+       // narrowAllVersion is the Go version at which the
+       // module-module "all" pattern no longer closes over the dependencies of
+       // tests outside of the main module.
+       NarrowAllVersion = "1.16"
+
+       // DefaultGoModVersion is the Go version to assume for go.mod files
+       // that do not declare a Go version. The go command has been
+       // writing go versions to modules since Go 1.12, so a go.mod
+       // without a version is either very old or recently hand-written.
+       // Since we can't tell which, we have to assume it's very old.
+       // The semantics of the go.mod changed at Go 1.17 to support
+       // graph pruning. If see a go.mod without a go line, we have to
+       // assume Go 1.16 so that we interpret the requirements correctly.
+       // Note that this default must stay at Go 1.16; it cannot be moved forward.
+       DefaultGoModVersion = "1.16"
+
+       // DefaultGoWorkVersion is the Go version to assume for go.work files
+       // that do not declare a Go version. Workspaces were added in Go 1.18,
+       // so use that.
+       DefaultGoWorkVersion = "1.18"
+
+       // ExplicitIndirectVersion is the Go version at which a
+       // module's go.mod file is expected to list explicit requirements on every
+       // module that provides any package transitively imported by that module.
+       //
+       // Other indirect dependencies of such a module can be safely pruned out of
+       // the module graph; see https://golang.org/ref/mod#graph-pruning.
+       ExplicitIndirectVersion = "1.17"
+
+       // separateIndirectVersion is the Go version at which
+       // "// indirect" dependencies are added in a block separate from the direct
+       // ones. See https://golang.org/issue/45965.
+       SeparateIndirectVersion = "1.17"
+
+       // tidyGoModSumVersion is the Go version at which
+       // 'go mod tidy' preserves go.mod checksums needed to build test dependencies
+       // of packages in "all", so that 'go test all' can be run without checksum
+       // errors.
+       // See https://go.dev/issue/56222.
+       TidyGoModSumVersion = "1.21"
+
+       // goStrictVersion is the Go version at which the Go versions
+       // became "strict" in the sense that, restricted to modules at this version
+       // or later, every module must have a go version line ≥ all its dependencies.
+       // It is also the version after which "too new" a version is considered a fatal error.
+       GoStrictVersion = "1.21"
+)
index 955f33650a0a8ac5d3d2caeb085222b57a9b6673..2fa85e0e219d9cba465ad16a0a05c2b1d387f697 100644 (file)
@@ -135,7 +135,7 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
                } else {
                        mainModule := modload.MainModules.Versions()[0]
                        modFile := modload.MainModules.ModFile(mainModule)
-                       if modFile.Go == nil || gover.Compare(modFile.Go.Version, modload.ExplicitIndirectVersion) < 0 {
+                       if modFile.Go == nil || gover.Compare(modFile.Go.Version, gover.ExplicitIndirectVersion) < 0 {
                                if len(modFile.Require) > 0 {
                                        args = []string{"all"}
                                }
index 0e4c7afb2346b60fec8d7d8c9b004c3d644dafdb..dd8e46eaad8fe89cb62f30933d7dd7c979df7884 100644 (file)
@@ -545,7 +545,7 @@ func LoadModGraph(ctx context.Context, goVersion string) (*ModuleGraph, error) {
 
        if goVersion != "" {
                v, _ := rs.rootSelected("go")
-               if gover.Compare(v, GoStrictVersion) >= 0 && gover.Compare(goVersion, v) < 0 {
+               if gover.Compare(v, gover.GoStrictVersion) >= 0 && gover.Compare(goVersion, v) < 0 {
                        return nil, fmt.Errorf("requested Go version %s cannot load module graph (requires Go >= %s)", goVersion, v)
                }
 
index 8840188c26f8b8d1b3bba8845708001ed0d2817e..5c942ffeb01c48697ba472db5392e390675c24a3 100644 (file)
@@ -225,7 +225,7 @@ func (mms *MainModuleSet) GoVersion() string {
                if mms.workFile != nil && mms.workFile.Go != nil {
                        return mms.workFile.Go.Version
                }
-               return defaultGoWorkVersion
+               return gover.DefaultGoWorkVersion
        }
        if mms != nil && len(mms.versions) == 1 {
                f := mms.ModFile(mms.mustGetSingleMainModule())
@@ -239,7 +239,7 @@ func (mms *MainModuleSet) GoVersion() string {
                        return f.Go.Version
                }
        }
-       return defaultGoModVersion
+       return gover.DefaultGoModVersion
 }
 
 // Toolchain returns the toolchain set on the single module, in module mode,
@@ -876,7 +876,7 @@ func loadModFile(ctx context.Context, opts *PackageOpts) *Requirements {
                        // Go 1.11 through 1.16 do not support graph pruning, but the latest Go
                        // version uses a pruned module graph — so we need to convert the
                        // requirements to support pruning.
-                       if gover.Compare(v, ExplicitIndirectVersion) >= 0 {
+                       if gover.Compare(v, gover.ExplicitIndirectVersion) >= 0 {
                                var err error
                                rs, err = convertPruning(ctx, rs, pruned)
                                if err != nil {
@@ -884,7 +884,7 @@ func loadModFile(ctx context.Context, opts *PackageOpts) *Requirements {
                                }
                        }
                } else {
-                       rawGoVersion.Store(mainModule, defaultGoModVersion)
+                       rawGoVersion.Store(mainModule, gover.DefaultGoModVersion)
                }
        }
 
@@ -1228,7 +1228,7 @@ func requirementsFromModFiles(ctx context.Context, workFile *modfile.WorkFile, m
                goVersion = opts.GoVersion
        }
        if goVersion == "" {
-               goVersion = defaultGoModVersion
+               goVersion = gover.DefaultGoModVersion
        }
        roots = append(roots, module.Version{Path: "go", Version: goVersion})
        direct["go"] = true
@@ -1618,7 +1618,7 @@ func commitRequirements(ctx context.Context, opts WriteOpts) (err error) {
        wroteGo := false
        if modFile.Go == nil || modFile.Go.Version != goVersion {
                alwaysUpdate := cfg.BuildMod == "mod" || cfg.CmdName == "mod tidy" || cfg.CmdName == "get"
-               if modFile.Go == nil && goVersion == defaultGoModVersion && !alwaysUpdate {
+               if modFile.Go == nil && goVersion == gover.DefaultGoModVersion && !alwaysUpdate {
                        // The go.mod has no go line, the implied default Go version matches
                        // what we've computed for the graph, and we're not in one of the
                        // traditional go.mod-updating programs, so leave it alone.
@@ -1648,7 +1648,7 @@ func commitRequirements(ctx context.Context, opts WriteOpts) (err error) {
        }
 
        // Update require blocks.
-       if gover.Compare(goVersion, separateIndirectVersion) < 0 {
+       if gover.Compare(goVersion, gover.SeparateIndirectVersion) < 0 {
                modFile.SetRequire(list)
        } else {
                modFile.SetRequireSeparateIndirect(list)
@@ -1764,7 +1764,7 @@ func keepSums(ctx context.Context, ld *loader, rs *Requirements, which whichSums
                        // However, we didn't do so before Go 1.21, and the bug is relatively
                        // minor, so we maintain the previous (buggy) behavior in 'go mod tidy' to
                        // avoid introducing unnecessary churn.
-                       if !ld.Tidy || gover.Compare(ld.GoVersion, tidyGoModSumVersion) >= 0 {
+                       if !ld.Tidy || gover.Compare(ld.GoVersion, gover.TidyGoModSumVersion) >= 0 {
                                r := resolveReplacement(pkg.mod)
                                keep[modkey(r)] = true
                        }
index b4cf736d75ec69d118e429dbc9778bea96cfc320..8f1eb1098bd4e53f984017cbd1da8d5ac43a0db5 100644 (file)
@@ -1020,12 +1020,12 @@ func loadFromRoots(ctx context.Context, params loaderParams) *loader {
                        ld.TidyCompatibleVersion = ld.GoVersion
                }
 
-               if gover.Compare(ld.GoVersion, tidyGoModSumVersion) < 0 {
+               if gover.Compare(ld.GoVersion, gover.TidyGoModSumVersion) < 0 {
                        ld.skipImportModFiles = true
                }
        }
 
-       if gover.Compare(ld.GoVersion, narrowAllVersion) < 0 && !ld.UseVendorAll {
+       if gover.Compare(ld.GoVersion, gover.NarrowAllVersion) < 0 && !ld.UseVendorAll {
                // The module's go version explicitly predates the change in "all" for graph
                // pruning, so continue to use the older interpretation.
                ld.allClosesOverTests = true
index b2bae6255bc96c5f4be2ea6ef51544fbb811ae03..e4a54869ed4858d7e2931ae10df5d493b3c58795 100644 (file)
@@ -27,55 +27,6 @@ import (
        "golang.org/x/mod/module"
 )
 
-const (
-       // narrowAllVersion is the Go version at which the
-       // module-module "all" pattern no longer closes over the dependencies of
-       // tests outside of the main module.
-       narrowAllVersion = "1.16"
-
-       // defaultGoModVersion is the Go version to assume for go.mod files
-       // that do not declare a Go version. The go command has been
-       // writing go versions to modules since Go 1.12, so a go.mod
-       // without a version is either very old or recently hand-written.
-       // Since we can't tell which, we have to assume it's very old.
-       // The semantics of the go.mod changed at Go 1.17 to support
-       // graph pruning. If see a go.mod without a go line, we have to
-       // assume Go 1.16 so that we interpret the requirements correctly.
-       // Note that this default must stay at Go 1.16; it cannot be moved forward.
-       defaultGoModVersion = "1.16"
-
-       // defaultGoWorkVersion is the Go version to assume for go.work files
-       // that do not declare a Go version. Workspaces were added in Go 1.18,
-       // so use that.
-       defaultGoWorkVersion = "1.18"
-
-       // ExplicitIndirectVersion is the Go version at which a
-       // module's go.mod file is expected to list explicit requirements on every
-       // module that provides any package transitively imported by that module.
-       //
-       // Other indirect dependencies of such a module can be safely pruned out of
-       // the module graph; see https://golang.org/ref/mod#graph-pruning.
-       ExplicitIndirectVersion = "1.17"
-
-       // separateIndirectVersion is the Go version at which
-       // "// indirect" dependencies are added in a block separate from the direct
-       // ones. See https://golang.org/issue/45965.
-       separateIndirectVersion = "1.17"
-
-       // tidyGoModSumVersion is the Go version at which
-       // 'go mod tidy' preserves go.mod checksums needed to build test dependencies
-       // of packages in "all", so that 'go test all' can be run without checksum
-       // errors.
-       // See https://go.dev/issue/56222.
-       tidyGoModSumVersion = "1.21"
-
-       // goStrictVersion is the Go version at which the Go versions
-       // became "strict" in the sense that, restricted to modules at this version
-       // or later, every module must have a go version line ≥ all its dependencies.
-       // It is also the version after which "too new" a version is considered a fatal error.
-       GoStrictVersion = "1.21"
-)
-
 // ReadModFile reads and parses the mod file at gomod. ReadModFile properly applies the
 // overlay, locks the file while reading, and applies fix, if applicable.
 func ReadModFile(gomod string, fix modfile.VersionFixer) (data []byte, f *modfile.File, err error) {
@@ -149,7 +100,7 @@ func (p modPruning) String() string {
 }
 
 func pruningForGoVersion(goVersion string) modPruning {
-       if gover.Compare(goVersion, ExplicitIndirectVersion) < 0 {
+       if gover.Compare(goVersion, gover.ExplicitIndirectVersion) < 0 {
                // The go.mod file does not duplicate relevant information about transitive
                // dependencies, so they cannot be pruned out.
                return unpruned