zed icon indicating copy to clipboard operation
zed copied to clipboard

Saving doesn't import or remove Go modules

Open mislavperi opened this issue 2 years ago • 8 comments
trafficstars

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

mislavperi avatar Mar 20 '23 18:03 mislavperi

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.

JacobWithACapitalJ avatar Mar 21 '23 13:03 JacobWithACapitalJ

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.

aybabtme avatar Mar 28 '23 19:03 aybabtme

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.

curtisknudson avatar Jul 10 '23 03:07 curtisknudson

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": []
                }
            }
        }
    },

hueys avatar Aug 01 '23 21:08 hueys

@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": []
        }
      }
    }
  

inianv avatar Nov 29 '23 09:11 inianv

Hello friends, what’s going on now? My side is still not working (unused packages will not be deleted)

good in vscode 😵‍💫 fix_go_remove

d1y avatar Jan 26 '24 06:01 d1y

@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

regexPattern avatar Jan 26 '24 13:01 regexPattern

@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.

Screen.Recording.2024-01-26.at.10.57.04.mov

it worked! Thank you

d1y avatar Jan 26 '24 14:01 d1y

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

derekperkins avatar Feb 13 '24 01:02 derekperkins

This has landed in v0.124.0.

JosephTLyons avatar Feb 21 '24 20:02 JosephTLyons