)
type ImportMissingError struct {
- Path string
- Module module.Version
- QueryErr error
- modContainingCWD module.Version
+ Path string
+ Module module.Version
+ QueryErr error
+ modContainingCWD module.Version
+ allowMissingModuleImports bool
// modRoot is dependent on the value of ImportingMainModule and should be
// kept in sync.
if e.QueryErr != nil && !errors.Is(e.QueryErr, ErrNoModRoot) {
return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr)
}
- if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && allowMissingModuleImports) {
+ if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && e.allowMissingModuleImports) {
return "cannot find module providing package " + e.Path
}
if len(mods) == 0 {
return module.Version{}, "", "", nil, &ImportMissingError{
- Path: path,
- modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ Path: path,
+ modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ allowMissingModuleImports: loaderstate.allowMissingModuleImports,
}
}
queryErr = NewNoMainModulesError(loaderstate)
}
return module.Version{}, "", "", nil, &ImportMissingError{
- Path: path,
- QueryErr: queryErr,
- isStd: pathIsStd,
- modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ Path: path,
+ QueryErr: queryErr,
+ isStd: pathIsStd,
+ modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ allowMissingModuleImports: loaderstate.allowMissingModuleImports,
}
}
} else if ok {
if cfg.BuildMod == "readonly" {
return module.Version{}, &ImportMissingError{
- Path: path,
- replaced: m,
- modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ Path: path,
+ replaced: m,
+ modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ allowMissingModuleImports: loaderstate.allowMissingModuleImports,
}
}
return m, nil
//
// Instead of trying QueryPattern, report an ImportMissingError immediately.
return module.Version{}, &ImportMissingError{
- Path: path,
- isStd: true,
- modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ Path: path,
+ isStd: true,
+ modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ allowMissingModuleImports: loaderstate.allowMissingModuleImports,
}
}
- if (cfg.BuildMod == "readonly" || cfg.BuildMod == "vendor") && !allowMissingModuleImports {
+ if (cfg.BuildMod == "readonly" || cfg.BuildMod == "vendor") && !loaderstate.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.
queryErr = fmt.Errorf("import lookup disabled by -mod=%s\n\t(%s)", cfg.BuildMod, cfg.BuildModReason)
}
return module.Version{}, &ImportMissingError{
- Path: path,
- QueryErr: queryErr,
- modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ Path: path,
+ QueryErr: queryErr,
+ modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ allowMissingModuleImports: loaderstate.allowMissingModuleImports,
}
}
// Return "cannot find module providing package […]" instead of whatever
// low-level error QueryPattern produced.
return module.Version{}, &ImportMissingError{
- Path: path,
- QueryErr: err,
- modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ Path: path,
+ QueryErr: err,
+ modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ allowMissingModuleImports: loaderstate.allowMissingModuleImports,
}
} else {
return module.Version{}, err
return c.Mod, nil
}
return module.Version{}, &ImportMissingError{
- Path: path,
- Module: candidates[0].Mod,
- newMissingVersion: candidate0MissingVersion,
- modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ Path: path,
+ Module: candidates[0].Mod,
+ newMissingVersion: candidate0MissingVersion,
+ modContainingCWD: loaderstate.MainModules.ModContainingCWD(),
+ allowMissingModuleImports: loaderstate.allowMissingModuleImports,
}
}
//
// TODO(#40775): See if these can be plumbed as explicit parameters.
var (
- allowMissingModuleImports bool
-
// ExplicitWriteGoMod prevents LoadPackages, ListModules, and other functions
// from updating go.mod and go.sum or reporting errors when updates are
// needed. A package should set this if it would cause go.mod to be written
}
type State struct {
- initialized bool
+ initialized bool
+ allowMissingModuleImports bool
// ForceUseModules may be set to force modules to be enabled when
// GO111MODULE=auto or to report an error when GO111MODULE=off.
//
// This function affects the default cfg.BuildMod when outside of a module,
// so it can only be called prior to Init.
-func AllowMissingModuleImports(loaderstate *State) {
- if loaderstate.initialized {
+func (s *State) AllowMissingModuleImports() {
+ if s.initialized {
panic("AllowMissingModuleImports after Init")
}
- allowMissingModuleImports = true
+ s.allowMissingModuleImports = true
}
// makeMainModules creates a MainModuleSet and associated variables according to
return
}
if loaderstate.modRoots == nil {
- if allowMissingModuleImports {
+ if loaderstate.allowMissingModuleImports {
cfg.BuildMod = "mod"
} else {
cfg.BuildMod = "readonly"