Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: nil pointer dereference when running "go mod tidy" in temp root #51992

Closed
nick-jones opened this issue Mar 28, 2022 · 6 comments
Closed
Labels
GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@nick-jones
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.18 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1179204110=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I went to create a temporary go.mod file in /tmp to test something. As per https://golang.org/cl/129063, /<temp-dir>/go.mod is generally ignored - not realising this, I ran go mod tidy and was slightly surprised to see a panic printed.

What did you expect to see?

With Go 1.17 a warning is displayed, which seems reasonable:

$ docker run -it --entrypoint=/bin/sh golang:1.17-alpine
/go # go version
go version go1.17.8 linux/amd64
/go # cd /tmp
/tmp # go mod init tmp
go: creating new go.mod: module tmp
/tmp # go mod tidy
go: warning: ignoring go.mod in system temp root /tmp
go: warning: "all" matched no packages
/tmp # echo $?
0
/tmp # 

What did you see instead?

With Go 1.18 the same warning is displayed, but there is also a panic relating to a nil pointer dereference:

$ docker run -it --entrypoint=/bin/sh golang:1.18-alpine
/go # go version
go version go1.18 linux/amd64
/go # cd /tmp
/tmp # go mod init tmp
go: creating new go.mod: module tmp
/tmp # go mod tidy
go: warning: ignoring go.mod in system temp root /tmp
go: warning: "all" matched no packages
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x78846d]

goroutine 1 [running]:
cmd/vendor/golang.org/x/mod/modfile.(*File).AddGoStmt(0x0, {0xc0000a2148, 0x4})
        /usr/local/go/src/cmd/vendor/golang.org/x/mod/modfile/rule.go:928 +0x4d
cmd/go/internal/modload.LoadPackages({0xadc718?, 0xc0000a2000}, {{0x0, 0x0}, 0xc00012dda0, 0x1, {0x0, 0x0}, 0x1, 0x1, ...}, ...)
        /usr/local/go/src/cmd/go/internal/modload/load.go:426 +0x74d
cmd/go/internal/modcmd.runTidy({0xadc718, 0xc0000a2000}, 0xc0000ce408?, {0xc00009c170?, 0x4?, 0x2?})
        /usr/local/go/src/cmd/go/internal/modcmd/tidy.go:114 +0x198
main.invoke(0xdb6380, {0xc00009c170, 0x1, 0x1})
        /usr/local/go/src/cmd/go/main.go:218 +0x2ee
main.main()
        /usr/local/go/src/cmd/go/main.go:175 +0x78e
/tmp # echo $?
2
/tmp #
@seankhliao seankhliao added NeedsFix The path to resolution is known, but the work has not been done. GoCommand cmd/go labels Mar 28, 2022
@seankhliao
Copy link
Member

cc @bcmills @matloob

@tenkoh
Copy link

tenkoh commented Apr 14, 2022

I have investigated this issue.

This issue would happen with Go 1.18 or higher.

In cmd/go/internal/modload/load.go, LoadPackages function fails into a panic due to nil pointer. Adding nil check would solve this issue.

Other mod commands like mod vendor or mod why also use the function, but as a result of if statement, no panics occur with them. This issue would only occur with mod tidy.

@gopherbot
Copy link

Change https://go.dev/cl/400554 mentions this issue: cmd/go: mod tidy returns proper error with /tmp/go.mod

@dmitshur dmitshur added this to the Go1.19 milestone May 6, 2022
@debugger24
Copy link

debugger24 commented Mar 9, 2023

Hi all, I am still having this issue (ignoring go.mod in system temp root /tmp) with golang 1.20

root@c2b53323c59c:/tmp# pwd
/tmp
root@c2b53323c59c:/tmp# ls
Dockerfile  README.md  conf  go.mod  go.sum  main.go  static
root@c2b53323c59c:/tmp# go version
go version go1.20.1 linux/arm64
root@c2b53323c59c:/tmp# go mod tidy
go: warning: ignoring go.mod in system temp root /tmp
go: warning: "all" matched no packages
go: go.mod file not found in current directory or any parent directory; see 'go help modules'

@tenkoh
Copy link

tenkoh commented Mar 9, 2023

Hi.

The error is as intended. This issue refers to the failure to generate the error as intended, resulting in panic.

@arjendevos
Copy link

arjendevos commented Jan 30, 2024

@tenkoh What is a fix for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

7 participants