aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Anthony <johnanthony.contrib@gmail.com>2022-03-17 15:36:52 +0000
committerCherry Mui <cherryyz@google.com>2022-04-05 14:17:24 +0000
commitc6ba470316579d80f581ef44b3210b75b3436199 (patch)
tree8adf4c5e131b04c57862ca4dc61358cb7fe5c482
parent30d9077a3485b1ff7cfd877fcc16ecb64dc3a4ae (diff)
downloadgo-c6ba470316579d80f581ef44b3210b75b3436199.tar.gz
go-c6ba470316579d80f581ef44b3210b75b3436199.zip
[release-branch.go1.18] 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 #51764 Updates #51749 Change-Id: If8f69d233409e93fcf391a8774bace74c031c986 Reviewed-on: https://go-review.googlesource.com/c/go/+/393615 Reviewed-by: Bryan Mills <bcmills@google.com> Trust: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Trust: Ian Lance Taylor <iant@golang.org> (cherry picked from commit a84ef500213ef6c2a0e4bfd82253e9fcd28f1f62) Reviewed-on: https://go-review.googlesource.com/c/go/+/397994 Reviewed-by: Cherry Mui <cherryyz@google.com>
-rw-r--r--src/cmd/go/internal/workcmd/use.go13
-rw-r--r--src/cmd/go/testdata/script/work_use_file.txt12
2 files changed, 21 insertions, 4 deletions
diff --git a/src/cmd/go/internal/workcmd/use.go b/src/cmd/go/internal/workcmd/use.go
index e20041f79f..07bc9b0500 100644
--- a/src/cmd/go/internal/workcmd/use.go
+++ b/src/cmd/go/internal/workcmd/use.go
@@ -85,13 +85,14 @@ func runUse(ctx context.Context, cmd *base.Command, args []string) {
lookDir := func(dir string) {
absDir, dir := pathRel(workDir, dir)
- fi, err := os.Stat(filepath.Join(absDir, "go.mod"))
+ fi, err := fsys.Stat(filepath.Join(absDir, "go.mod"))
if err != nil {
if os.IsNotExist(err) {
keepDirs[absDir] = ""
- return
+ } else {
+ base.Errorf("go: %v", err)
}
- base.Errorf("go: %v", err)
+ return
}
if !fi.Mode().IsRegular() {
@@ -109,7 +110,11 @@ func runUse(ctx context.Context, cmd *base.Command, args []string) {
}
for _, useDir := range args {
if !*useR {
- lookDir(useDir)
+ if target, err := fsys.Stat(useDir); err == nil && !target.IsDir() {
+ base.Errorf(`go: argument "%s" is not a directory`, useDir)
+ } else {
+ lookDir(useDir)
+ }
continue
}
diff --git a/src/cmd/go/testdata/script/work_use_file.txt b/src/cmd/go/testdata/script/work_use_file.txt
new file mode 100644
index 0000000000..807dd96c72
--- /dev/null
+++ b/src/cmd/go/testdata/script/work_use_file.txt
@@ -0,0 +1,12 @@
+cp go.work go.work.orig
+
+# If an argument to 'go work use' is a file it should be handled gracefully as
+# an error and go.work should not be modified
+! go work use foo.txt
+stderr '^go: argument "foo\.txt" is not a directory$'
+cmp go.work go.work.orig
+
+
+-- go.work --
+go 1.18
+-- foo.txt --