if q.pattern == "all" {
// If there is no main module, "all" is not meaningful.
if !modload.HasModRoot(loaderstate) {
- return fmt.Errorf(`cannot match "all": %v`, modload.ErrNoModRoot)
+ return fmt.Errorf(`cannot match "all": %v`, modload.NewNoMainModulesError(loaderstate))
}
if !versionOkForMainModule(q.version) {
// TODO(bcmills): "all@none" seems like a totally reasonable way to
}
return msg
}
- if e.QueryErr != nil && e.QueryErr != ErrNoModRoot {
+ 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) {
// requested package.
var queryErr error
if !HasModRoot(loaderstate) {
- queryErr = ErrNoModRoot
+ queryErr = NewNoMainModulesError(loaderstate)
}
return module.Version{}, "", "", nil, &ImportMissingError{Path: path, QueryErr: queryErr, isStd: pathIsStd}
}
base.Fatalf("go: cannot find main module, but -modfile was set.\n\t-modfile cannot be used to set the module root directory.")
}
if loaderstate.RootMode == NeedRoot {
- base.Fatal(ErrNoModRoot)
+ base.Fatal(NewNoMainModulesError(loaderstate))
}
if !mustUseModules {
// GO111MODULE is 'auto', and we can't find a module root.
// when it happens. See golang.org/issue/26708.
fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in system temp root %v\n", os.TempDir())
if loaderstate.RootMode == NeedRoot {
- base.Fatal(ErrNoModRoot)
+ base.Fatal(NewNoMainModulesError(loaderstate))
}
if !mustUseModules {
return
if _, err := fsys.Stat(filepath.Join(gopath, "go.mod")); err == nil {
fmt.Fprintf(os.Stderr, "go: warning: ignoring go.mod in $GOPATH %v\n", gopath)
if loaderstate.RootMode == NeedRoot {
- base.Fatal(ErrNoModRoot)
+ base.Fatal(NewNoMainModulesError(loaderstate))
}
if !mustUseModules {
return
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)
}
}
- base.Fatal(ErrNoModRoot)
+ base.Fatal(NewNoMainModulesError(loaderstate))
}
+var ErrNoModRoot = errors.New("no module root")
+
// noMainModulesError returns the appropriate error if there is no main module or
// main modules depending on whether the go command is in workspace mode.
-type noMainModulesError struct{}
+type noMainModulesError struct {
+ inWorkspaceMode bool
+}
func (e noMainModulesError) Error() string {
- if inWorkspaceMode(LoaderState) {
+ if e.inWorkspaceMode {
return "no modules were found in the current workspace; see 'go help work'"
}
return "go.mod file not found in current directory or any parent directory; see 'go help modules'"
}
-var ErrNoModRoot noMainModulesError
+func (e noMainModulesError) Unwrap() error {
+ return ErrNoModRoot
+}
+
+func NewNoMainModulesError(s *State) noMainModulesError {
+ return noMainModulesError{
+ inWorkspaceMode: inWorkspaceMode(s),
+ }
+}
type goModDirtyError struct{}
if arg == "all" || strings.Contains(arg, "...") {
needFullGraph = true
if !HasModRoot(loaderstate) {
- base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
+ base.Fatalf("go: cannot match %q: %v", arg, NewNoMainModulesError(loaderstate))
}
continue
}
if _, ok := rs.rootSelected(loaderstate, path); !ok || rs.pruning == unpruned {
needFullGraph = true
if !HasModRoot(loaderstate) {
- base.Fatalf("go: cannot match %q: %v", arg, ErrNoModRoot)
+ base.Fatalf("go: cannot match %q: %v", arg, NewNoMainModulesError(loaderstate))
}
}
}
if _, ok := rs.rootSelected(loaderstate, arg); !ok || rs.pruning == unpruned {
needFullGraph = true
if mode&ListVersions == 0 && !HasModRoot(loaderstate) {
- base.Fatalf("go: cannot match %q without -versions or an explicit version: %v", arg, ErrNoModRoot)
+ base.Fatalf("go: cannot match %q without -versions or an explicit version: %v", arg, NewNoMainModulesError(loaderstate))
}
}
}