1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:44:42 -07:00

internal/imports: prevent self-imports in the stdlib

goimports has a fast path for stdlib imports that was not checking for
import cycles. Add the check.

Fixes golang/go#37063.

Change-Id: I46c98c317d8f06f83018fef9ef7edf9222e6b3f3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/217958
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2020-02-05 16:34:39 -05:00
parent aa017ee804
commit 9f574694fd
2 changed files with 27 additions and 3 deletions

View File

@ -537,7 +537,7 @@ func getFixes(fset *token.FileSet, f *ast.File, filename string, env *ProcessEnv
// derive package names from import paths, see if the file is already // derive package names from import paths, see if the file is already
// complete. We can't add any imports yet, because we don't know // complete. We can't add any imports yet, because we don't know
// if missing references are actually package vars. // if missing references are actually package vars.
p := &pass{fset: fset, f: f, srcDir: srcDir} p := &pass{fset: fset, f: f, srcDir: srcDir, env: env}
if fixes, done := p.load(); done { if fixes, done := p.load(); done {
return fixes, nil return fixes, nil
} }
@ -559,8 +559,7 @@ func getFixes(fset *token.FileSet, f *ast.File, filename string, env *ProcessEnv
} }
// Third pass: get real package names where we had previously used // Third pass: get real package names where we had previously used
// the naive algorithm. This is the first step that will use the // the naive algorithm.
// environment, so we provide it here for the first time.
p = &pass{fset: fset, f: f, srcDir: srcDir, env: env} p = &pass{fset: fset, f: f, srcDir: srcDir, env: env}
p.loadRealPackageNames = true p.loadRealPackageNames = true
p.otherFiles = otherFiles p.otherFiles = otherFiles
@ -853,6 +852,10 @@ func cmdDebugStr(cmd *exec.Cmd) string {
func addStdlibCandidates(pass *pass, refs references) { func addStdlibCandidates(pass *pass, refs references) {
add := func(pkg string) { add := func(pkg string) {
// Prevent self-imports.
if path.Base(pkg) == pass.f.Name.Name && filepath.Join(pass.env.GOROOT, "src", pkg) == pass.srcDir {
return
}
exports := copyExports(stdlib[pkg]) exports := copyExports(stdlib[pkg])
pass.addCandidate( pass.addCandidate(
&ImportInfo{ImportPath: pkg}, &ImportInfo{ImportPath: pkg},

View File

@ -1577,6 +1577,27 @@ var _ = bytes.Buffer
}) })
} }
func TestStdlibSelfImports(t *testing.T) {
const input = `package ecdsa
var _ = ecdsa.GenerateKey
`
testConfig{
module: packagestest.Module{
Name: "ignored.com",
},
}.test(t, func(t *goimportTest) {
got, err := t.processNonModule(filepath.Join(t.env.GOROOT, "src/crypto/ecdsa/foo.go"), []byte(input), nil)
if err != nil {
t.Fatalf("Process() = %v", err)
}
if string(got) != input {
t.Errorf("Got:\n%s\nWant:\n%s", got, input)
}
})
}
type testConfig struct { type testConfig struct {
gopathOnly bool gopathOnly bool
module packagestest.Module module packagestest.Module