aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/load.go
diff options
context:
space:
mode:
authortenkoh <tenkoh.go@gmail.com>2022-04-15 13:58:08 +0900
committerGopher Robot <gobot@golang.org>2022-05-06 21:51:47 +0000
commitd7a03da8edf37ee4fa9c1200040db001c6c3e56a (patch)
tree6f5e6ea6de20bf5ff0fa44cc88e5bdc7ea6c78e7 /src/cmd/go/internal/modload/load.go
parentac39dbdf58e50a2575b891675e7d2e1400b20cfe (diff)
downloadgo-d7a03da8edf37ee4fa9c1200040db001c6c3e56a.tar.gz
go-d7a03da8edf37ee4fa9c1200040db001c6c3e56a.zip
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>
Diffstat (limited to 'src/cmd/go/internal/modload/load.go')
-rw-r--r--src/cmd/go/internal/modload/load.go3
1 files changed, 1 insertions, 2 deletions
diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go
index 29c0a4280a9..5214a9e2d14 100644
--- a/src/cmd/go/internal/modload/load.go
+++ b/src/cmd/go/internal/modload/load.go
@@ -421,8 +421,7 @@ func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (ma
}
// Update the go.mod file's Go version if necessary.
- modFile := MainModules.ModFile(MainModules.mustGetSingleMainModule())
- if ld.GoVersion != "" {
+ if modFile := ModFile(); modFile != nil && ld.GoVersion != "" {
modFile.AddGoStmt(ld.GoVersion)
}
}