Skip to content

Commit c18fa69

Browse files
committed
cmd/go: make ErrNoModRoot work with local state
This change reworks the sentinel error value ErrNoModRoot and the type noMainModulesError so that we can determine the appropriate error message to display based on the loader state without depending on the state directly. This commit is part of the overall effort to eliminate global modloader state. Change-Id: I64de433faca96ed90fad4c153766c50575a72157 Reviewed-on: https://go-review.googlesource.com/c/go/+/711120 Reviewed-by: Michael Matloob <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 296ecc9 commit c18fa69

File tree

4 files changed

+25
-13
lines changed

4 files changed

+25
-13
lines changed

src/cmd/go/internal/modget/query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func (q *query) validate(loaderstate *modload.State) error {
185185
if q.pattern == "all" {
186186
// If there is no main module, "all" is not meaningful.
187187
if !modload.HasModRoot(loaderstate) {
188-
return fmt.Errorf(`cannot match "all": %v`, modload.ErrNoModRoot)
188+
return fmt.Errorf(`cannot match "all": %v`, modload.NewNoMainModulesError(loaderstate))
189189
}
190190
if !versionOkForMainModule(q.version) {
191191
// TODO(bcmills): "all@none" seems like a totally reasonable way to

src/cmd/go/internal/modload/import.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (e *ImportMissingError) Error() string {
6363
}
6464
return msg
6565
}
66-
if e.QueryErr != nil && e.QueryErr != ErrNoModRoot {
66+
if e.QueryErr != nil && !errors.Is(e.QueryErr, ErrNoModRoot) {
6767
return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr)
6868
}
6969
if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && allowMissingModuleImports) {
@@ -484,7 +484,7 @@ func importFromModules(loaderstate *State, ctx context.Context, path string, rs
484484
// requested package.
485485
var queryErr error
486486
if !HasModRoot(loaderstate) {
487-
queryErr = ErrNoModRoot
487+
queryErr = NewNoMainModulesError(loaderstate)
488488
}
489489
return module.Version{}, "", "", nil, &ImportMissingError{Path: path, QueryErr: queryErr, isStd: pathIsStd}
490490
}

src/cmd/go/internal/modload/init.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ func Init(loaderstate *State) {
523523
base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.")
524524
}
525525
if loaderstate.RootMode == NeedRoot {
526-
base.Fatal(ErrNoModRoot)
526+
base.Fatal(NewNoMainModulesError(loaderstate))
527527
}
528528
if !mustUseModules {
529529
// GO111MODULE is 'auto', and we can't find a module root.
@@ -538,7 +538,7 @@ func Init(loaderstate *State) {
538538
// when it happens. See golang.org/issue/26708.
539539
fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in system temp root %v\n", os.TempDir())
540540
if loaderstate.RootMode == NeedRoot {
541-
base.Fatal(ErrNoModRoot)
541+
base.Fatal(NewNoMainModulesError(loaderstate))
542542
}
543543
if !mustUseModules {
544544
return
@@ -560,7 +560,7 @@ func Init(loaderstate *State) {
560560
if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
561561
fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in $GOPATH %v\n", gopath)
562562
if loaderstate.RootMode == NeedRoot {
563-
base.Fatal(ErrNoModRoot)
563+
base.Fatal(NewNoMainModulesError(loaderstate))
564564
}
565565
if !mustUseModules {
566566
return
@@ -731,21 +731,33 @@ func die(loaderstate *State) {
731731
base.Fatalf("go: cannot find main module, but found %s in %s\n\tto create a module there, run:\n\t%sgo mod init", name, dir, cdCmd)
732732
}
733733
}
734-
base.Fatal(ErrNoModRoot)
734+
base.Fatal(NewNoMainModulesError(loaderstate))
735735
}
736736

737+
var ErrNoModRoot = errors.New("no module root")
738+
737739
// noMainModulesError returns the appropriate error if there is no main module or
738740
// main modules depending on whether the go command is in workspace mode.
739-
type noMainModulesError struct{}
741+
type noMainModulesError struct {
742+
inWorkspaceMode bool
743+
}
740744

741745
func (e noMainModulesError) Error() string {
742-
if inWorkspaceMode(LoaderState) {
746+
if e.inWorkspaceMode {
743747
return "no modules were found in the current workspace; see 'go help work'"
744748
}
745749
return "go.mod file not found in current directory or any parent directory; see 'go help modules'"
746750
}
747751

748-
var ErrNoModRoot noMainModulesError
752+
func (e noMainModulesError) Unwrap() error {
753+
return ErrNoModRoot
754+
}
755+
756+
func NewNoMainModulesError(s *State) noMainModulesError {
757+
return noMainModulesError{
758+
inWorkspaceMode: inWorkspaceMode(s),
759+
}
760+
}
749761

750762
type goModDirtyError struct{}
751763

src/cmd/go/internal/modload/list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func listModules(loaderstate *State, ctx context.Context, rs *Requirements, args
146146
if arg == "all" || strings.Contains(arg, "...") {
147147
needFullGraph = true
148148
if !HasModRoot(loaderstate) {
149-
base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
149+
base.Fatalf("go: cannot match %q: %v", arg, NewNoMainModulesError(loaderstate))
150150
}
151151
continue
152152
}
@@ -155,7 +155,7 @@ func listModules(loaderstate *State, ctx context.Context, rs *Requirements, args
155155
if _, ok := rs.rootSelected(loaderstate, path); !ok || rs.pruning == unpruned {
156156
needFullGraph = true
157157
if !HasModRoot(loaderstate) {
158-
base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
158+
base.Fatalf("go: cannot match %q: %v", arg, NewNoMainModulesError(loaderstate))
159159
}
160160
}
161161
}
@@ -164,7 +164,7 @@ func listModules(loaderstate *State, ctx context.Context, rs *Requirements, args
164164
if _, ok := rs.rootSelected(loaderstate, arg); !ok || rs.pruning == unpruned {
165165
needFullGraph = true
166166
if mode&ListVersions == 0 && !HasModRoot(loaderstate) {
167-
base.Fatalf("go: cannot match %q without -versions or an explicit version: %v", arg, ErrNoModRoot)
167+
base.Fatalf("go: cannot match %q without -versions or an explicit version: %v", arg, NewNoMainModulesError(loaderstate))
168168
}
169169
}
170170
}

0 commit comments

Comments
 (0)