]> Cypherpunks.ru repositories - gostls13.git/commit
cmd/go: mod tidy returns proper error with /tmp/go.mod
authortenkoh <tenkoh.go@gmail.com>
Fri, 15 Apr 2022 04:58:08 +0000 (13:58 +0900)
committerGopher Robot <gobot@golang.org>
Fri, 6 May 2022 21:51:47 +0000 (21:51 +0000)
commitd7a03da8edf37ee4fa9c1200040db001c6c3e56a
tree6f5e6ea6de20bf5ff0fa44cc88e5bdc7ea6c78e7
parentac39dbdf58e50a2575b891675e7d2e1400b20cfe
cmd/go: mod tidy returns proper error with /tmp/go.mod

`go mod tidy` results in panic due to nil pointer dereference with the
current implementation. Though the panic occurs only in a limited situation
described as below, we had better fix it.

Situation:
- go.mod is in the exactly system's temporary directory (i.e. temp root)
- `go mod tidy` in temp root or in the child directory not having go.mod

No go.mod are found in the situation (i.e. *modFile is nil), however,
*modFile is referred without nil check.

Although just adding nil check works well, the better solution is using
ModFile() function. It works as same as the current implementation and,
in addition, it has either nil check and user friendly error indication.
With using it, users can get a proper error message like "go.mod file not
found in current directory or any parent directory" instead of a panic.

Fixes #51992

Change-Id: I2ba26762778acca6cd637c8eb8c615fb747063f4
Reviewed-on: https://go-review.googlesource.com/c/go/+/400554
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/go/internal/modload/load.go
src/cmd/go/testdata/script/mod_tidy_temp.txt [new file with mode: 0644]