// Variables set in Init.
var (
-
- // These are primarily used to initialize the MainModules, and should be
- // eventually superseded by them but are still used in cases where the module
- // roots are required but MainModules hasn't been initialized yet. Set to
- // the modRoots of the main modules.
- // modRoots != nil implies len(modRoots) > 0
- modRoots []string
- gopath string
+ gopath string
)
// EnterModule resets MainModules and requirements to refer to just this one module.
workFilePath = "" // Force module mode
modfetch.Reset()
- modRoots = []string{enterModroot}
+ LoaderState.modRoots = []string{enterModroot}
LoadModFile(ctx)
}
initialized: LoaderState.initialized,
ForceUseModules: LoaderState.ForceUseModules,
RootMode: LoaderState.RootMode,
- modRoots: modRoots,
+ modRoots: LoaderState.modRoots,
modulesEnabled: cfg.ModulesEnabled,
mainModules: MainModules,
requirements: requirements,
LoaderState.initialized = s.initialized
LoaderState.ForceUseModules = s.ForceUseModules
LoaderState.RootMode = s.RootMode
- modRoots = s.modRoots
+ LoaderState.modRoots = s.modRoots
cfg.ModulesEnabled = s.modulesEnabled
MainModules = s.mainModules
requirements = s.requirements
ForceUseModules bool
// RootMode determines whether a module root is needed.
- RootMode Root
+ RootMode Root
+
+ // These are primarily used to initialize the MainModules, and should
+ // be eventually superseded by them but are still used in cases where
+ // the module roots are required but MainModules has not been
+ // initialized yet. Set to the modRoots of the main modules.
+ // modRoots != nil implies len(modRoots) > 0
modRoots []string
modulesEnabled bool
mainModules *MainModuleSet
if os.Getenv("GCM_INTERACTIVE") == "" {
os.Setenv("GCM_INTERACTIVE", "never")
}
- if modRoots != nil {
+ if LoaderState.modRoots != nil {
// modRoot set before Init was called ("go mod init" does this).
// No need to search for go.mod.
} else if LoaderState.RootMode == NoRoot {
if cfg.ModFile != "" && !base.InGOFLAGS("-modfile") {
base.Fatalf("go: -modfile cannot be used with commands that ignore the current module")
}
- modRoots = nil
+ LoaderState.modRoots = nil
} else if workFilePath != "" {
// We're in workspace mode, which implies module mode.
if cfg.ModFile != "" {
return
}
} else {
- modRoots = []string{modRoot}
+ LoaderState.modRoots = []string{modRoot}
}
}
if cfg.ModFile != "" && !strings.HasSuffix(cfg.ModFile, ".mod") {
// be called until the command is installed and flags are parsed. Instead of
// calling Init and Enabled, the main package can call this function.
func WillBeEnabled() bool {
- if modRoots != nil || cfg.ModulesEnabled {
+ if LoaderState.modRoots != nil || cfg.ModulesEnabled {
// Already enabled.
return true
}
// (usually through MustModRoot).
func Enabled() bool {
Init()
- return modRoots != nil || cfg.ModulesEnabled
+ return LoaderState.modRoots != nil || cfg.ModulesEnabled
}
func VendorDir() string {
// does not require a main module.
func HasModRoot() bool {
Init()
- return modRoots != nil
+ return LoaderState.modRoots != nil
}
// MustHaveModRoot checks that a main module or main modules are present,
var workFile *modfile.WorkFile
if inWorkspaceMode() {
var err error
- workFile, modRoots, err = LoadWorkFile(workFilePath)
+ workFile, LoaderState.modRoots, err = LoadWorkFile(workFilePath)
if err != nil {
return nil, err
}
- for _, modRoot := range modRoots {
+ for _, modRoot := range LoaderState.modRoots {
sumFile := strings.TrimSuffix(modFilePath(modRoot), ".mod") + ".sum"
modfetch.WorkspaceGoSumFiles = append(modfetch.WorkspaceGoSumFiles, sumFile)
}
modfetch.GoSumFile = workFilePath + ".sum"
- } else if len(modRoots) == 0 {
+ } else if len(LoaderState.modRoots) == 0 {
// We're in module mode, but not inside a module.
//
// Commands like 'go build', 'go run', 'go list' have no go.mod file to
//
// See golang.org/issue/32027.
} else {
- modfetch.GoSumFile = strings.TrimSuffix(modFilePath(modRoots[0]), ".mod") + ".sum"
+ modfetch.GoSumFile = strings.TrimSuffix(modFilePath(LoaderState.modRoots[0]), ".mod") + ".sum"
}
- if len(modRoots) == 0 {
+ if len(LoaderState.modRoots) == 0 {
// TODO(#49228): Instead of creating a fake module with an empty modroot,
// make MainModules.Len() == 0 mean that we're in module mode but not inside
// any module.
var mainModules []module.Version
var indices []*modFileIndex
var errs []error
- for _, modroot := range modRoots {
+ for _, modroot := range LoaderState.modRoots {
gomod := modFilePath(modroot)
var fixed bool
data, f, err := ReadModFile(gomod, fixVersion(ctx, &fixed))
return nil, errors.Join(errs...)
}
- MainModules = makeMainModules(mainModules, modRoots, modFiles, indices, workFile)
+ MainModules = makeMainModules(mainModules, LoaderState.modRoots, modFiles, indices, workFile)
setDefaultBuildMod() // possibly enable automatic vendoring
rs := requirementsFromModFiles(ctx, workFile, modFiles, opts)
// packages at multiple versions from the same module).
func CreateModFile(ctx context.Context, modPath string) {
modRoot := base.Cwd()
- modRoots = []string{modRoot}
+ LoaderState.modRoots = []string{modRoot}
Init()
modFilePath := modFilePath(modRoot)
if _, err := fsys.Stat(modFilePath); err == nil {
cfg.BuildMod = "readonly"
return
}
- if modRoots == nil {
+ if LoaderState.modRoots == nil {
if allowMissingModuleImports {
cfg.BuildMod = "mod"
} else {
return
}
- if len(modRoots) >= 1 {
+ if len(LoaderState.modRoots) >= 1 {
var goVersion string
var versionSource string
if inWorkspaceMode() {
if workFilePath != "" {
vendorDir = filepath.Join(filepath.Dir(workFilePath), "vendor")
} else {
- if len(modRoots) != 1 {
- panic(fmt.Errorf("outside workspace mode, but have %v modRoots", modRoots))
+ if len(LoaderState.modRoots) != 1 {
+ panic(fmt.Errorf("outside workspace mode, but have %v modRoots", LoaderState.modRoots))
}
- vendorDir = filepath.Join(modRoots[0], "vendor")
+ vendorDir = filepath.Join(LoaderState.modRoots[0], "vendor")
}
if fi, err := fsys.Stat(vendorDir); err == nil && fi.IsDir() {
if goVersion != "" {