Source file src/cmd/go/internal/gover/version.go

     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  	// tidyGoModSumVersion is the Go version at which
    45  	// 'go mod tidy' preserves go.mod checksums needed to build test dependencies
    46  	// of packages in "all", so that 'go test all' can be run without checksum
    47  	// errors.
    48  	// See https://go.dev/issue/56222.
    49  	TidyGoModSumVersion = "1.21"
    50  
    51  	// goStrictVersion is the Go version at which the Go versions
    52  	// became "strict" in the sense that, restricted to modules at this version
    53  	// or later, every module must have a go version line ≥ all its dependencies.
    54  	// It is also the version after which "too new" a version is considered a fatal error.
    55  	GoStrictVersion = "1.21"
    56  
    57  	// ExplicitModulesTxtImportVersion is the Go version at which vendored packages need to be present
    58  	// in modules.txt to be imported.
    59  	ExplicitModulesTxtImportVersion = "1.23"
    60  )
    61  
    62  // FromGoMod returns the go version from the go.mod file.
    63  // It returns DefaultGoModVersion if the go.mod file does not contain a go line or if mf is nil.
    64  func FromGoMod(mf *modfile.File) string {
    65  	if mf == nil || mf.Go == nil {
    66  		return DefaultGoModVersion
    67  	}
    68  	return mf.Go.Version
    69  }
    70  
    71  // FromGoWork returns the go version from the go.mod file.
    72  // It returns DefaultGoWorkVersion if the go.mod file does not contain a go line or if wf is nil.
    73  func FromGoWork(wf *modfile.WorkFile) string {
    74  	if wf == nil || wf.Go == nil {
    75  		return DefaultGoWorkVersion
    76  	}
    77  	return wf.Go.Version
    78  }
    79  

View as plain text