diff --git a/internal/imports/fix.go b/internal/imports/fix.go index ac8f6b153d..f95d0f4409 100644 --- a/internal/imports/fix.go +++ b/internal/imports/fix.go @@ -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 // complete. We can't add any imports yet, because we don't know // 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 { 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 - // the naive algorithm. This is the first step that will use the - // environment, so we provide it here for the first time. + // the naive algorithm. p = &pass{fset: fset, f: f, srcDir: srcDir, env: env} p.loadRealPackageNames = true p.otherFiles = otherFiles @@ -853,6 +852,10 @@ func cmdDebugStr(cmd *exec.Cmd) string { func addStdlibCandidates(pass *pass, refs references) { 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]) pass.addCandidate( &ImportInfo{ImportPath: pkg}, diff --git a/internal/imports/fix_test.go b/internal/imports/fix_test.go index 6e83527d31..f2bc5b639a 100644 --- a/internal/imports/fix_test.go +++ b/internal/imports/fix_test.go @@ -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 { gopathOnly bool module packagestest.Module