mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
cmd/go/internal/modload: improve ErrNoModRoot error text
Before this change, in several cases where HasModRoot() returned false, we'd return ErrNoModRoot. ErrNoModRoot would say that there was no go.mod file but would not mention workspaces. With this change, ErrNoModRoot will return error text that's different if we're in a workspace, saying that there are no modules in the workspace. Fixes #54419 Change-Id: I77c94d0011947bf8e33c066416ab3762502fd2e6 Reviewed-on: https://go-review.googlesource.com/c/go/+/660675 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
parent
44fef2aa4d
commit
87d1833c66
@ -610,7 +610,7 @@ func inWorkspaceMode() bool {
|
||||
return workFilePath != ""
|
||||
}
|
||||
|
||||
// HasModRoot reports whether a main module is present.
|
||||
// HasModRoot reports whether a main module or main modules are present.
|
||||
// HasModRoot may return false even if Enabled returns true: for example, 'get'
|
||||
// does not require a main module.
|
||||
func HasModRoot() bool {
|
||||
@ -646,24 +646,34 @@ func die() {
|
||||
if cfg.Getenv("GO111MODULE") == "off" {
|
||||
base.Fatalf("go: modules disabled by GO111MODULE=off; see 'go help modules'")
|
||||
}
|
||||
if inWorkspaceMode() {
|
||||
base.Fatalf("go: no modules were found in the current workspace; see 'go help work'")
|
||||
}
|
||||
if dir, name := findAltConfig(base.Cwd()); dir != "" {
|
||||
rel, err := filepath.Rel(base.Cwd(), dir)
|
||||
if err != nil {
|
||||
rel = dir
|
||||
if !inWorkspaceMode() {
|
||||
if dir, name := findAltConfig(base.Cwd()); dir != "" {
|
||||
rel, err := filepath.Rel(base.Cwd(), dir)
|
||||
if err != nil {
|
||||
rel = dir
|
||||
}
|
||||
cdCmd := ""
|
||||
if rel != "." {
|
||||
cdCmd = fmt.Sprintf("cd %s && ", rel)
|
||||
}
|
||||
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)
|
||||
}
|
||||
cdCmd := ""
|
||||
if rel != "." {
|
||||
cdCmd = fmt.Sprintf("cd %s && ", rel)
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
var ErrNoModRoot = errors.New("go.mod file not found in current directory or any parent directory; see 'go help modules'")
|
||||
// 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{}
|
||||
|
||||
func (e noMainModulesError) Error() string {
|
||||
if 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
|
||||
|
||||
type goModDirtyError struct{}
|
||||
|
||||
|
13
src/cmd/go/testdata/script/work_no_mod_root_issue54419.txt
vendored
Normal file
13
src/cmd/go/testdata/script/work_no_mod_root_issue54419.txt
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
cd m
|
||||
! go mod download
|
||||
stderr 'no modules were found in the current workspace'
|
||||
|
||||
! go list -m all
|
||||
stderr 'no modules were found in the current workspace'
|
||||
|
||||
-- go.work --
|
||||
go 1.25
|
||||
-- m/go.mod --
|
||||
module example.com/m
|
||||
|
||||
go 1.25
|
Loading…
x
Reference in New Issue
Block a user