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

x/playground: goimports removes a needed import #46491

Open
harrybarrer opened this issue Jun 1, 2021 · 2 comments
Open

x/playground: goimports removes a needed import #46491

harrybarrer opened this issue Jun 1, 2021 · 2 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@harrybarrer
Copy link

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

Latest.

The playground uses the latest stable release of Go.
The current version is go1.16.4.

Does this issue reproduce with the latest release?

Yes

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

Playground

What did you do?

Tested in go playground https://play.golang.org/

  1. Select multiple files
  2. Change folder name from -- foo/foo.go -- to -- foo_test/foo.go --
  3. Change import in main from "play.ground/foo" to "play.ground/foo_test"
  4. Run. It works. Result: "This function lives in an another file!"
  5. Check imports button to update imports
  6. Run Format (this will remove import)
  7. Run again. You will see the error ./prog.go:4:2: undefined: foo

Playground link: https://play.golang.org/p/Fq83n0Gc2YC

What did you expect to see?

goimports ideally should add the package name before the import as an alias. That was the previous behaviour. Or at least, not remove the import because it will break the code.

What did you see instead?

goimports removes the needed import and breaks the code.

error ./prog.go:4:2: undefined: foo

Related discussion

#29557

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Jun 1, 2021
@gopherbot gopherbot added this to the Unreleased milestone Jun 1, 2021
@heschi
Copy link
Contributor

heschi commented Jun 1, 2021

I believe this is a bug specific to the playground. Without investigating, I'd guess that it's only passing the main file to goimports, so there's no way to account for surprisingly-named packages. Per the bug you linked, it's intended that unknown imports are removed when they don't obviously satisfy a requirement.

@heschi heschi changed the title x/tools/cmd/goimports: goimports removes a needed import x/playground: goimports removes a needed import Jun 1, 2021
@heschi heschi added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 1, 2021
@heschi
Copy link
Contributor

heschi commented Jun 1, 2021

cc @toothrot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants