6060func EnterModule (ctx context.Context , enterModroot string ) {
6161 LoaderState .MainModules = nil // reset MainModules
6262 LoaderState .requirements = nil
63- workFilePath = "" // Force module mode
63+ LoaderState . workFilePath = "" // Force module mode
6464 modfetch .Reset ()
6565
6666 LoaderState .modRoots = []string {enterModroot }
@@ -97,12 +97,6 @@ func EnterWorkspace(ctx context.Context) (exit func(), err error) {
9797 }, nil
9898}
9999
100- // Variable set in InitWorkfile
101- var (
102- // Set to the path to the go.work file, or "" if workspace mode is disabled.
103- workFilePath string
104- )
105-
106100type MainModuleSet struct {
107101 // versions are the module.Version values of each of the main modules.
108102 // For each of them, the Path fields are ordinary module paths and the Version
@@ -349,7 +343,7 @@ func InitWorkfile() {
349343 if err := fsys .Init (); err != nil {
350344 base .Fatal (err )
351345 }
352- workFilePath = FindGoWork (base .Cwd ())
346+ LoaderState . workFilePath = FindGoWork (base .Cwd ())
353347}
354348
355349// FindGoWork returns the name of the go.work file for this command,
@@ -378,7 +372,7 @@ func FindGoWork(wd string) string {
378372// WorkFilePath returns the absolute path of the go.work file, or "" if not in
379373// workspace mode. WorkFilePath must be called after InitWorkfile.
380374func WorkFilePath () string {
381- return workFilePath
375+ return LoaderState . workFilePath
382376}
383377
384378// Reset clears all the initialized, cached state about the use of modules,
@@ -404,7 +398,7 @@ func setState(s State) State {
404398 cfg .ModulesEnabled = s .modulesEnabled
405399 LoaderState .MainModules = s .MainModules
406400 LoaderState .requirements = s .requirements
407- workFilePath = s .workFilePath
401+ LoaderState . workFilePath = s .workFilePath
408402 // The modfetch package's global state is used to compute
409403 // the go.sum file, so save and restore it along with the
410404 // modload state.
@@ -441,7 +435,10 @@ type State struct {
441435 // commitRequirements functions. All other functions that need or
442436 // produce a *Requirements should accept and/or return an explicit
443437 // parameter.
444- requirements * Requirements
438+ requirements * Requirements
439+
440+ // Set to the path to the go.work file, or "" if workspace mode is
441+ // disabled
445442 workFilePath string
446443 modfetchState modfetch.State
447444}
@@ -507,7 +504,7 @@ func Init() {
507504 base .Fatalf ("go: -modfile cannot be used with commands that ignore the current module" )
508505 }
509506 LoaderState .modRoots = nil
510- } else if workFilePath != "" {
507+ } else if LoaderState . workFilePath != "" {
511508 // We're in workspace mode, which implies module mode.
512509 if cfg .ModFile != "" {
513510 base .Fatalf ("go: -modfile cannot be used in workspace mode" )
@@ -651,7 +648,7 @@ func inWorkspaceMode() bool {
651648 if ! Enabled () {
652649 return false
653650 }
654- return workFilePath != ""
651+ return LoaderState . workFilePath != ""
655652}
656653
657654// HasModRoot reports whether a main module or main modules are present.
@@ -888,15 +885,15 @@ func loadModFile(ctx context.Context, opts *PackageOpts) (*Requirements, error)
888885 var workFile * modfile.WorkFile
889886 if inWorkspaceMode () {
890887 var err error
891- workFile , LoaderState .modRoots , err = LoadWorkFile (workFilePath )
888+ workFile , LoaderState .modRoots , err = LoadWorkFile (LoaderState . workFilePath )
892889 if err != nil {
893890 return nil , err
894891 }
895892 for _ , modRoot := range LoaderState .modRoots {
896893 sumFile := strings .TrimSuffix (modFilePath (modRoot ), ".mod" ) + ".sum"
897894 modfetch .WorkspaceGoSumFiles = append (modfetch .WorkspaceGoSumFiles , sumFile )
898895 }
899- modfetch .GoSumFile = workFilePath + ".sum"
896+ modfetch .GoSumFile = LoaderState . workFilePath + ".sum"
900897 } else if len (LoaderState .modRoots ) == 0 {
901898 // We're in module mode, but not inside a module.
902899 //
@@ -1542,8 +1539,8 @@ func setDefaultBuildMod() {
15421539 }
15431540 }
15441541 vendorDir := ""
1545- if workFilePath != "" {
1546- vendorDir = filepath .Join (filepath .Dir (workFilePath ), "vendor" )
1542+ if LoaderState . workFilePath != "" {
1543+ vendorDir = filepath .Join (filepath .Dir (LoaderState . workFilePath ), "vendor" )
15471544 } else {
15481545 if len (LoaderState .modRoots ) != 1 {
15491546 panic (fmt .Errorf ("outside workspace mode, but have %v modRoots" , LoaderState .modRoots ))
0 commit comments