Skip to content

Commit a84ef50

Browse files
John AnthonyBryan Mills
John Anthony
authored and
Bryan Mills
committed
cmd/go: prevent go work use panic when given a file
The current implementation fails to identify that an argument to go work use is a file when expecting a directory, and panics when attempting to access it as a directory. This change checks arguments are directories and generates an error otherwise. Fixes #51749 Change-Id: If8f69d233409e93fcf391a8774bace74c031c986 Reviewed-on: https://go-review.googlesource.com/c/go/+/393615 Reviewed-by: Bryan Mills <[email protected]> Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Trust: Ian Lance Taylor <[email protected]>
1 parent 31ee4bb commit a84ef50

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/cmd/go/internal/workcmd/use.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ func runUse(ctx context.Context, cmd *base.Command, args []string) {
8585
lookDir := func(dir string) {
8686
absDir, dir := pathRel(workDir, dir)
8787

88-
fi, err := os.Stat(filepath.Join(absDir, "go.mod"))
88+
fi, err := fsys.Stat(filepath.Join(absDir, "go.mod"))
8989
if err != nil {
9090
if os.IsNotExist(err) {
9191
keepDirs[absDir] = ""
92-
return
92+
} else {
93+
base.Errorf("go: %v", err)
9394
}
94-
base.Errorf("go: %v", err)
95+
return
9596
}
9697

9798
if !fi.Mode().IsRegular() {
@@ -109,7 +110,11 @@ func runUse(ctx context.Context, cmd *base.Command, args []string) {
109110
}
110111
for _, useDir := range args {
111112
if !*useR {
112-
lookDir(useDir)
113+
if target, err := fsys.Stat(useDir); err == nil && !target.IsDir() {
114+
base.Errorf(`go: argument "%s" is not a directory`, useDir)
115+
} else {
116+
lookDir(useDir)
117+
}
113118
continue
114119
}
115120

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cp go.work go.work.orig
2+
3+
# If an argument to 'go work use' is a file it should be handled gracefully as
4+
# an error and go.work should not be modified
5+
! go work use foo.txt
6+
stderr '^go: argument "foo\.txt" is not a directory$'
7+
cmp go.work go.work.orig
8+
9+
10+
-- go.work --
11+
go 1.18
12+
-- foo.txt --

0 commit comments

Comments
 (0)