-
Notifications
You must be signed in to change notification settings - Fork 18k
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/tools/cmd/goimports: doesn't add package if already imported for side effects (underscore import) #16411
Comments
Interesting case. |
CL https://golang.org/cl/37090 mentions this issue. |
/cc @heschik From our conversation, it sounded like behavior 2 seemed like the most reasonable option. I don't think CL 37090 is the right solution: we should not be modifying the low-level |
Either CL 37090 is the right solution or goimports should not use the helpers, because I think @davidrjenni correctly identified the problem. |
Ok. If we're going to consider changing the Edit:
Example Usageimport ()
AddNamedImport(..., "", "path") -> added: true
import (
"path"
)
AddNamedImport(..., "pathpkg", "path") -> added: true
import (
"path"
pathpkg "path"
)
AddNamedImport(..., "_", "path") -> added: true
import (
"path"
pathpkg "path"
_ "path"
)
DeleteNamedImport(..., "", "path") -> deleted: true
import (
pathpkg "path"
_ "path"
)
// calling it again has no effect, the unnamed "path" import is already gone
DeleteNamedImport(..., "", "path") -> deleted: false
import (
pathpkg "path"
_ "path"
)
DeleteNamedImport(..., "pathpkg", "path") -> deleted: true
import (
_ "path"
)
DeleteNamedImport(..., "_", "path") -> deleted: true
import () That way, it's possible to use these helpers to manipulate the AST with full precision (which is what I would've expected from them in the first place). Does that sound reasonable? |
Change https://golang.org/cl/147448 mentions this issue: |
I'm not sure if this is a bug or a feature request or known accepted behavior in
goimports
- I'll let @bradfitz decide, but consider the following unfinished Go program:One would normally expect the
png.Encode
usage to causegoimports
to add theimage/png
import.However, it does nothing. It's because that package is already imported for side effects.
This was slightly unexpected. But I'm not sure what ideal behavior would be.
image/png
, keeping the underscore import alone? Maybe?pathpkg "path"
import and someone writespath.Join
, shouldgoimports
add a second import forpath
package? Or should underscore imports be treated differently than other renamed packages?I'm only reporting this because I ran into it and it didn't seem to be reported already.
The text was updated successfully, but these errors were encountered: