// Reset resets globals in the modfetch package, so previous loads don't affect
// contents of go.sum files.
func Reset() {
- SetState(State{})
+ SetState(NewState())
}
// SetState sets the global state of the modfetch package to the newState, and returns the previous
// global state. newState should have been returned by SetState, or be an empty State.
// There should be no concurrent calls to any of the exported functions of this package with
// a call to SetState because it will modify the global state in a non-thread-safe way.
-func SetState(newState State) (oldState State) {
+func SetState(newState *State) (oldState *State) {
if newState.lookupCache == nil {
newState.lookupCache = new(par.Cache[lookupCacheKey, Repo])
}
goSum.mu.Lock()
defer goSum.mu.Unlock()
- oldState = State{
+ oldState = &State{
goSumFile: ModuleFetchState.goSumFile,
workspaceGoSumFiles: ModuleFetchState.workspaceGoSumFiles,
lookupCache: ModuleFetchState.lookupCache,
}
// Reset the state to a clean state.
- oldstate := loaderstate.setState(State{})
+ oldstate := loaderstate.setState(NewState())
loaderstate.ForceUseModules = true
// Load in workspace mode.
// Reset clears all the initialized, cached state about the use of modules,
// so that we can start over.
func (s *State) Reset() {
- s.setState(State{})
+ s.setState(NewState())
}
-func (s *State) setState(new State) State {
- oldState := State{
+func (s *State) setState(new *State) (old *State) {
+ old = &State{
initialized: s.initialized,
ForceUseModules: s.ForceUseModules,
RootMode: s.RootMode,
modulesEnabled: cfg.ModulesEnabled,
MainModules: s.MainModules,
requirements: s.requirements,
+ workFilePath: s.workFilePath,
+ modfetchState: s.modfetchState,
}
s.initialized = new.initialized
s.ForceUseModules = new.ForceUseModules
// The modfetch package's global state is used to compute
// the go.sum file, so save and restore it along with the
// modload state.
- oldState.modfetchState = modfetch.SetState(new.modfetchState)
- return oldState
+ s.modfetchState = new.modfetchState
+ old.modfetchState = modfetch.SetState(s.modfetchState) // TODO(jitsu): remove after completing global state elimination
+
+ return old
}
type State struct {
// Set to the path to the go.work file, or "" if workspace mode is
// disabled
workFilePath string
- modfetchState modfetch.State
+ modfetchState *modfetch.State
}
-func NewState() *State { return &State{} }
+func NewState() *State {
+ s := new(State)
+ s.modfetchState = modfetch.NewState()
+ return s
+}
// Init determines whether module mode is enabled, locates the root of the
// current module (if any), sets environment variables for Git subprocesses, and