zed
zed copied to clipboard
Saving doesn't import or remove Go modules
Check for existing issues
- [X] Completed
Describe the bug / provide steps to reproduce it
Typing in code some struct from a go package and hitting save won't import it. No longer using a package in code (removing fmt prints for example) and saving the file will not remove the import from the project.
Environment
Zed: v0.77.3 (stable) OS: macOS 13.1.0 Memory: 16 GiB Architecture: aarch64
If applicable, add mockups / screenshots to help explain present your vision of the feature
No response
If applicable, attach your ~/Library/Logs/Zed/Zed.log file to this issue.
If you only need the most recent lines, you can run the zed: open log command palette action to see the last 1000.
No response
Just to expand on this, I don't believe the go language server should resolve imports automatically. That instead should be the role of the goimports tool.
However, a slightly related bug... when trying to use an external tool to format on save as per the docs:
"language_overrides": {
"Go": {
"format_on_save": {
"external": {
"command": "gofmt",
"arguments": [
"-w",
"{buffer_path}"
]
}
}
}
it results in the entire file being wiped and some error logs being created. I'll create a separate issue for this as using goimports to format the code will resolve the auto import issue, but appears blocked by a bug.
Similarly in VSCode, it seems like the auto-complete is "aware" of not-yet imported packages, as I can start typing ioutil. and the autocomplete will know to look for symbols in ioutil even though it's not yet imported in the file. Not sure how they do this, but it's extremely useful.
it results in the entire file being wiped and some error logs being created. I'll create a separate issue for this as using goimports to format the code will resolve the auto import issue, but appears blocked by a bug.
Having this exact same issue. Good to know that it is a known bug.
I'm using goimports to format and automatically import modules with the following in my settings, and haven't seen any issues so far.
"language_overrides": {
"Go": {
"formatter": {
"external": {
"command": "goimports",
"arguments": []
}
}
}
},
@JacobWithACapitalJ @curtisknudson I also ran into the same issue, but the issue is not related to the editor itself. As indicated in this comment, the formatter, needs to write the content to stdout and not do an in-place replacement, i.e. not overwrite the buffer text.
When using gofmt with -w, and the file argument, it does the formatting in-place. To fix this, use the same command without the -w flag (as below), which should auto-format on save now as the formatter now writes the formatted content to stdout. The same also applies to gofumpt, if anyone is interested.
"language_overrides": {
"Go": {
"format_on_save": {
"external": {
"command": "gofmt",
"arguments": []
}
}
}
Hello friends, what’s going on now? My side is still not working (unused packages will not be deleted)
good in vscode 😵💫
@d1y Works fine for me (the clip only shows adding missing packages but removing unused ones works as well). Make sure you have goimports installed globally on your system and that it is in your path.
https://github.com/zed-industries/zed/assets/47466248/193a6617-384f-4d74-87fa-eaedf8faa11e
@d1y Works fine for me (the clip only shows adding missing packages but removing unused ones works as well). Make sure you have
goimportsinstalled globally on your system and that it is in your path.Screen.Recording.2024-01-26.at.10.57.04.mov
it worked! Thank you
This suggestion resolves it how I would prefer. goimports can be slow (6-8s in our repo), and gopls is already running, so a setting to trigger Organize Imports on save is how I would prefer to do it.
- https://github.com/zed-industries/zed/issues/5232
This has landed in v0.124.0.