Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Editor breaks import path when Go module replacements are used #2572

Closed
andig opened this issue Jun 13, 2019 · 14 comments
Closed

Editor breaks import path when Go module replacements are used #2572

andig opened this issue Jun 13, 2019 · 14 comments
Labels
go-modules Related to Go modules

Comments

@andig
Copy link

andig commented Jun 13, 2019

My code contains various imports:

import (
    "fmt"
    "log"
    "os"
    "strconv"

    "github.com/crabmusket/gosunspec"
    bus "github.com/crabmusket/gosunspec/modbus"
    _ "github.com/crabmusket/gosunspec/models" // import models

    "github.com/grid-x/modbus"
)

I'm also using the types imported from gosunspec in main.go:

in.Do(func(d sunspec.Device) {
	d.Do(func(m sunspec.Model) {
        ...
	})
})

Using go.mod, I have to temporarily replace crabmusket/gosunspec with a custom fork until an upstream issue is resolved:

module github.com/volkszaehler/mbmd

require (
    github.com/crabmusket/gosunspec v0.0.0-20170310004250-4c2adf6161ca
    github.com/eclipse/paho.mqtt.golang v1.2.0
    github.com/goburrow/modbus v0.1.0 // indirect
    github.com/goburrow/serial v0.1.0 // indirect
    ...
)

replace github.com/crabmusket/gosunspec => github.com/andig/gosunspec v0.0.0-20190613063246-3c54c2ccb77a

Now, when editing main.go, two things are happening:

  1. vscode modifies the "github.com/crabmusket/gosunspec" import into the "github.com/andig/gosunspec" replacement
  2. subsequently "github.com/andig/gosunspec" is added to go.mod (I assume rather by go than by vscode)

Synopsis if types are part of a replacement package, import path must match the replaced package instead of the replacement package.

Full source is available in https://github.com/andig/mbmd/blob/scan-sunspec/cmd/scan/main.go

@ramya-rao-a
Copy link
Contributor

Please share any Go related settings you might have added/edited

@andig
Copy link
Author

andig commented Jun 14, 2019

There is only settings.json, nothing I have specifically tweaked:

{
    "go.formatTool": "goimports"
}

If you need anything else please let me know.

@chgeuer
Copy link
Member

chgeuer commented Jun 28, 2019

I believe I'm experiencing the same problem. Check out this sample repro solution:

https://github.com/chgeuer/repo-go-mod-vs-code

There are two VS Code workspaces, called "good" and "bad". The "good" one only shows the main folder, while "bad" shows everything. I would love to open "bad", but then the whole F12 navigation etc doesn't work any longer.

  • The VS Code version is "Version 1.35.1 (user setup) commit c7d83e...".
  • The version of ms-vscode.go plugin is 0.11.0.

My global VS Code settings (%appdata%\Code\User\settings.json) contain these Go-specifics:

{
   "go.useLanguageServer": true,
    "go.vetOnSave": "workspace",
    "go.docsTool": "gogetdoc",
    "go.enableCodeLens": {
        "references": false,
        "runtest": true
    }
}

@andig
Copy link
Author

andig commented Jun 28, 2019

ping @ramya-rao-a is there anything else we could contribute?

@ramya-rao-a
Copy link
Contributor

Hi @andig, apologies for the late reply.

Your settings show that you are not using the language server. @chgeuer Can you share your settings as well?

In the absence of the language feature, the tool specified in go.formatTool setting is used for formatting. In @andig's case, this is goimports which does edit import statements.

@andig Can you add the below in your settings to disable the format on save feature and see if you still see the issue? This can confirm if the formatting is indeed making this change

"[go]": {
   "editor.formatOnSave": false
}

@andig
Copy link
Author

andig commented Jun 29, 2019

Thank you @ramya-rao-a. I wasn‘t aware that I needed to manually enable the language server istead of this being the default (I haven‘t touched the config at all). Reading https://github.com/Microsoft/vscode-go/wiki/Go-modules-support-in-Visual-Studio-Code shows I‘ll need to add a setting. I will test both your suggestion and using the language server.

Update regarding 1) just realized that the language server apparently can't be used inside workspaces as the setting can't be applied.

@chgeuer
Copy link
Member

chgeuer commented Jul 1, 2019

Your settings show that you are not using the language server. @chgeuer Can you share your settings as well?

Hi @ramya-rao-a I updated by comment above

@andig
Copy link
Author

andig commented Jul 4, 2019

I can no longer reproduce this when not using the language server (i.e. inside a workspace). Instead now, my import "github.com/crabmusket/gosunspec" is being rewritten to sunspec "github.com/crabmusket/gosunspec" which is correct but not necessary. The replacement module is no longer added go.mod.

@ramya-rao-a
Copy link
Contributor

The language server doesnt work with workspaces. There is a known issue tracking that.

@stamblerre
Copy link
Contributor

gopls requires you to manually add workspace folders. Here is the issue: golang/go#32394.

@andig
Copy link
Author

andig commented Jul 10, 2019

gopls requires you to manually add workspace folders. Here is the issue: golang/go#32394.

At least in my case thats the setup I have. Above issue has been fixed for me by getting rid of the replace statement.

@ramya-rao-a ramya-rao-a added the go-modules Related to Go modules label Sep 20, 2019
@ramya-rao-a
Copy link
Contributor

@stamblerre Is there anything that can be done for this issue?

From what I understand @andig resolved the situation by removing the replace statement entirely

@stamblerre
Copy link
Contributor

What @andig is describing sounds like an issue with an old version of goimports. It shouldn't be occurring anymore, but if you still see it with the latest version of gopls we can investigate further.

@ramya-rao-a
Copy link
Contributor

Thanks @stamblerre

Closing this issue.

If the problem still persists with the new version of the Go tools, please log a new issue

@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
go-modules Related to Go modules
Projects
None yet
Development

No branches or pull requests

4 participants