if e.QueryErr != nil {
return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr)
}
- if cfg.BuildMod == "mod" {
+ if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && allowMissingModuleImports) {
return "cannot find module providing package " + e.Path
}
return module.Version{}, &ImportMissingError{Path: path, isStd: true}
}
- if cfg.BuildMod == "readonly" {
+ if cfg.BuildMod == "readonly" && !allowMissingModuleImports {
// In readonly mode, we can't write go.mod, so we shouldn't try to look up
// the module. If readonly mode was enabled explicitly, include that in
// the error message.
mod = r
}
- if cfg.BuildMod == "readonly" && needSum && !modfetch.HaveSum(mod) {
+ if HasModRoot() && cfg.BuildMod == "readonly" && needSum && !modfetch.HaveSum(mod) {
return "", false, module.VersionError(mod, &sumMissingError{})
}
func TestQueryImport(t *testing.T) {
testenv.MustHaveExternalNetwork(t)
testenv.MustHaveExecPath(t, "git")
- defer func(old bool) {
- allowMissingModuleImports = old
- }(allowMissingModuleImports)
- AllowMissingModuleImports()
+
+ oldAllowMissingModuleImports := allowMissingModuleImports
+ oldRootMode := RootMode
+ defer func() {
+ allowMissingModuleImports = oldAllowMissingModuleImports
+ RootMode = oldRootMode
+ }()
+ allowMissingModuleImports = true
+ RootMode = NoRoot
ctx := context.Background()
}
// We're in module mode. Set any global variables that need to be set.
+ cfg.ModulesEnabled = true
+ setDefaultBuildMod()
list := filepath.SplitList(cfg.BuildContext.GOPATH)
if len(list) == 0 || list[0] == "" {
base.Fatalf("missing $GOPATH")
base.Fatalf("$GOPATH/go.mod exists but should not")
}
- cfg.ModulesEnabled = true
-
if modRoot == "" {
// We're in module mode, but not inside a module.
//
// ensuring requirements are consistent. WriteGoMod should be called later to
// write changes out to disk or report errors in readonly mode.
//
-// As a side-effect, LoadModFile sets a default for cfg.BuildMod if it does not
-// already have an explicit value.
+// As a side-effect, LoadModFile may change cfg.BuildMod to "vendor" if
+// -mod wasn't set explicitly and automatic vendoring should be enabled.
func LoadModFile(ctx context.Context) {
if len(buildList) > 0 {
return
base.Fatalf("go: %v", err)
}
- setDefaultBuildMod()
+ setDefaultBuildMod() // possibly enable automatic vendoring
modFileToBuildList()
if cfg.BuildMod == "vendor" {
readVendorList()
buildList = list
}
-// setDefaultBuildMod sets a default value for cfg.BuildMod
-// if it is currently empty.
+// setDefaultBuildMod sets a default value for cfg.BuildMod if the -mod flag
+// wasn't provided. setDefaultBuildMod may be called multiple times.
func setDefaultBuildMod() {
if cfg.BuildModExplicit {
// Don't override an explicit '-mod=' argument.
if fi, err := fsys.Stat(filepath.Join(modRoot, "vendor")); err == nil && fi.IsDir() {
modGo := "unspecified"
- if index.goVersionV != "" {
+ if index != nil && index.goVersionV != "" {
if semver.Compare(index.goVersionV, "v1.14") >= 0 {
// The Go version is at least 1.14, and a vendor directory exists.
// Set -mod=vendor by default.