Skip to content

Commit f95db21

Browse files
matloobgopherbot
authored andcommitted
cmd/go: add a better error message when in a module outside workspace
When the user is trying to list or build a package in a module that's outside of the workspace provide a more clear message hinting to the user that they can add the module to the workspace using go work use. Fixes #51604 Change-Id: I1202ecb2f22fd6351bfdec88ed613b8167687fb7 Reviewed-on: https://go-review.googlesource.com/c/go/+/400014 Run-TryBot: Michael Matloob <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Auto-Submit: Michael Matloob <[email protected]> Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 5b32a6f commit f95db21

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/cmd/go/internal/modload/load.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,13 @@ func resolveLocalPackage(ctx context.Context, dir string, rs *Requirements) (str
605605

606606
pkg := pathInModuleCache(ctx, absDir, rs)
607607
if pkg == "" {
608-
scope := "main module or its selected dependencies"
609608
if inWorkspaceMode() {
610-
scope = "modules listed in go.work or their selected dependencies"
609+
if mr := findModuleRoot(absDir); mr != "" {
610+
return "", fmt.Errorf("directory %s is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using go work use %s", base.ShortPath(absDir), base.ShortPath(mr))
611+
}
612+
return "", fmt.Errorf("directory %s outside modules listed in go.work or their selected dependencies", base.ShortPath(absDir))
611613
}
612-
return "", fmt.Errorf("directory %s outside %s", base.ShortPath(absDir), scope)
614+
return "", fmt.Errorf("directory %s outside main module or its selected dependencies", base.ShortPath(absDir))
613615
}
614616
return pkg, nil
615617
}

src/cmd/go/testdata/script/work_module_not_in_go_work.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
! go list ./...
77
stderr 'pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies'
88

9-
! go list ./a
10-
stderr 'directory a outside modules listed in go.work'
9+
! go list ./a/c
10+
stderr 'directory a[\\/]c is contained in a module that is not one of the workspace modules listed in go.work. You can add the module to the workspace using go work use a'
1111

1212
-- go.work --
1313
go 1.18
@@ -19,6 +19,8 @@ module example.com/a
1919
go 1.18
2020
-- a/a.go --
2121
package a
22+
-- a/c/c.go --
23+
package c
2224
-- b/go.mod --
2325
module example.com/b
2426

0 commit comments

Comments
 (0)