cmd/go/internal/modload: when outside a module, set cfg.BuildMod based on allowMissingModuleImports

For #36460

Change-Id: I50c7018a6841bba1a8c6f8f8eca150df1f685526
Reviewed-on: https://go-review.googlesource.com/c/go/+/310789
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Bryan C. Mills 2021-04-16 12:17:27 -04:00
parent c1e8a9a8c6
commit 2fc0ebb623

View file

@ -617,7 +617,13 @@ func fixVersion(ctx context.Context, fixed *bool) modfile.VersionFixer {
// when there is no module root. Normally, this is forbidden because it's slow // when there is no module root. Normally, this is forbidden because it's slow
// and there's no way to make the result reproducible, but some commands // and there's no way to make the result reproducible, but some commands
// like 'go get' are expected to do this. // like 'go get' are expected to do this.
//
// This function affects the default cfg.BuildMod when outside of a module,
// so it can only be called prior to Init.
func AllowMissingModuleImports() { func AllowMissingModuleImports() {
if initialized {
panic("AllowMissingModuleImports after Init")
}
allowMissingModuleImports = true allowMissingModuleImports = true
} }
@ -699,7 +705,11 @@ func setDefaultBuildMod() {
return return
} }
if modRoot == "" { if modRoot == "" {
cfg.BuildMod = "readonly" if allowMissingModuleImports {
cfg.BuildMod = "mod"
} else {
cfg.BuildMod = "readonly"
}
return return
} }