Closed kevinburke1 closed 4 years ago
I'm using Go tip
as well but I can also reproduce if I rebuild goimports using Go 1.14 latest.
/cc @heschik
Please run goimports -v
and attach the logs. Something is probably wrong with your go installation.
$ GOROOT=~/go goimports -v .
2020/08/10 12:03:17.471712 fixImports(filename="filename.go"), abs="/Users/kevin/src/github.com/kevinburke/zip-goimports-repro/filename.go", srcDir="/Users/kevin/src/github.com/kevinburke/zip-goimports-repro" ...
panic: GOROOT not set in evaluated environment
goroutine 1 [running]:
golang.org/x/tools/internal/imports.(*ProcessEnv).mustGetEnv(...)
/Users/kevin/src/golang.org/x/tools/internal/imports/fix.go:796
golang.org/x/tools/internal/imports.(*ProcessEnv).goroot(0x1425880, 0xc00001a2d0, 0x3)
/Users/kevin/src/golang.org/x/tools/internal/imports/fix.go:786 +0x119
golang.org/x/tools/internal/imports.addStdlibCandidates.func1(0x12466cb, 0xb)
/Users/kevin/src/golang.org/x/tools/internal/imports/fix.go:898 +0x21c
golang.org/x/tools/internal/imports.addStdlibCandidates(0xc0001465a0, 0xc000010ae0)
/Users/kevin/src/golang.org/x/tools/internal/imports/fix.go:915 +0x131
golang.org/x/tools/internal/imports.getFixes(0xc0000209c0, 0xc00012a480, 0xc00001a290, 0xb, 0x1425880, 0x0, 0x0, 0x1, 0x40, 0x121e540)
/Users/kevin/src/golang.org/x/tools/internal/imports/fix.go:562 +0x1f8
golang.org/x/tools/internal/imports.fixImportsDefault(0xc0000209c0, 0xc00012a480, 0xc00001a290, 0xb, 0x1425880, 0x600, 0x1421d40)
/Users/kevin/src/golang.org/x/tools/internal/imports/fix.go:522 +0x60
golang.org/x/tools/internal/imports.Process(0xc00001a290, 0xb, 0xc000186000, 0xd4, 0x600, 0x1421d40, 0x0, 0x0, 0xc00010fae8, 0x104de0f, ...)
/Users/kevin/src/golang.org/x/tools/internal/imports/imports.go:56 +0x19e
main.processFile(0xc00001a290, 0xb, 0x0, 0x0, 0x12a1540, 0xc00000e018, 0x2, 0x0, 0x0)
/Users/kevin/src/golang.org/x/tools/cmd/goimports/goimports.go:142 +0x1a6
main.visitFile(0xc00001a290, 0xb, 0x12a6200, 0xc000061450, 0x0, 0x0, 0xb, 0xc00010fcf0)
/Users/kevin/src/golang.org/x/tools/cmd/goimports/goimports.go:189 +0xcc
path/filepath.walk(0xc00001a290, 0xb, 0x12a6200, 0xc000061450, 0x1265580, 0x0, 0x0)
/Users/kevin/go1.14/src/path/filepath/path.go:360 +0x425
path/filepath.walk(0x7ffeefbff62d, 0x1, 0x12a6200, 0xc000061380, 0x1265580, 0x0, 0x12a6200)
/Users/kevin/go1.14/src/path/filepath/path.go:384 +0x2ff
path/filepath.Walk(0x7ffeefbff62d, 0x1, 0x1265580, 0xc0000612b0, 0x0)
/Users/kevin/go1.14/src/path/filepath/path.go:406 +0xff
main.walkDir(...)
/Users/kevin/src/golang.org/x/tools/cmd/goimports/goimports.go:198
main.gofmtMain()
/Users/kevin/src/golang.org/x/tools/cmd/goimports/goimports.go:289 +0x2b8
main.main()
/Users/kevin/src/golang.org/x/tools/cmd/goimports/goimports.go:207 +0x32
Huh, I really expected that to be more useful. Okay.
Don't bother setting GOROOT, that's only confusing the situation. Please run go env -json GO111MODULE GOFLAGS GOINSECURE GOMOD GOMODCACHE GONOPROXY GONOSUMDB GOPATH GOPROXY GOROOT GOSUMDB
and show the results.
$ go env -json GO111MODULE GOFLAGS GOINSECURE GOMOD GOMODCACHE GONOPROXY GONOSUMDB GOPATH GOPROXY GOROOT GOSUMDB
{
"GO111MODULE": "off",
"GOFLAGS": "",
"GOINSECURE": "",
"GOMOD": "",
"GOMODCACHE": "/Users/kevin/pkg/mod",
"GONOPROXY": "github.com/meterup",
"GONOSUMDB": "github.com/meterup",
"GOPATH": "/Users/kevin",
"GOPROXY": "direct",
"GOROOT": "/Users/kevin/go",
"GOSUMDB": "sum.golang.org"
}
Did you try to reproduce it using the example above? Can you reproduce it?
Change https://golang.org/cl/247797 mentions this issue: internal/imports: fix crash when adding stdlib imports
Apologies, I didn't give much credit to the idea that goimports had been broken for over a month or however long it's been. Fix above.
Well, I could only trigger it if the package name matched the stdlib package I was trying to import, if I named the package something else there wouldn't be a panic.
I put the following in a file named
filename.go
(filename doesn't matter):I then run
GOROOT=/Users/kevin/go goimports .
I expect that the"archive/zip"
import will be added to the import section. Instead I get a panic:Note I have explicitly set GOROOT in the environment. Adding a print statement to the relevant line in
fix.go
, it looks likee.Env
is nil at the point where it expects to be readingGOROOT
.The package name must match the imports, I think, you can also reproduce if you replace
zip
withio
in all three places, but you can't reproduce if you replaceio
in one place and keepzip
in the other place.I'm able to reproduce this on
x/tools
tip as well as on the commit taggedgopls/v0.4.4
.