-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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/gopls: group imports together for the local module #32049
Comments
I think this is ultimately something that we would put behind a config (even if it is on by default). We intend to add different configurations to formatting and imports, but first we will need to fix #30843. |
@matloob: This is something that we could implement through a suggested fix through |
@stamblerre from a cursory glance through the code, it seems like it would be straightforward to plumb through the Would you accept a PR which adds a temporary config option for now? In the future it would be replaced by some other mechanism once #30843 is in. I ask because the |
That sounds like a reasonable temporary measure. I'll leave the final decision to @suzmue though, since she has much more expertise with |
That sounds good to me as well. |
Is it relevant to utilize the path in go module to determine the local path? |
I'm running into this pain point as well. @cespare Are you still working on a PR to add a config flag for this? |
@sushicw Not actively. It's on my to-do list. If you'd like to do it, go ahead. I expect I'll get to this in the next 1-2 months. |
Change https://golang.org/cl/204820 mentions this issue: |
The change above adds a LocalPrefix configuration, but I do think it would be worth considering a default value that uses the name of the main module. Let's start with this configuration though, and then consider the further improvements. |
Updates golang/go#32049 Change-Id: I64e5201170b5be8b470c436264e18e12ec8d12f2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/204820 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
I went to try this out, but couldn't get it to work (even added some logging where the ProcessEnv gets created to make sure it was plumbing it down). Turns out that the VS Code Go extension is the one doing the formatting, and not gopls. Is there some trick I've missed to make use of gopls as the formatter rather than an external tool? |
@zikaeroh: What are your VSCode settings ( |
Ah, that's it. I set the format flag to false, probably because format-on-save wasn't behaving the way I wanted the first time I used gopls a while ago. I'll retest when I get a chance later today. |
Can someone share the VSCode settings needed to get this to work, please? Here is what I currently have: "go.useLanguageServer": true,
"go.languageServerExperimentalFeatures": {
"documentLink": false,
"incrementalSync": true,
},
"gopls": {
"usePlaceholders": false,
"completionDocumentation": true,
},
"[go]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true,
},
"editor.codeActionsOnSaveTimeout": 3000,
// "editor.defaultFormatter": "ms-vscode.Go",
"editor.formatOnSave": true,
"editor.snippetSuggestions": "none",
},
"editor.formatOnSave": true I would like to use the new LocalPrefix config/flag. |
You will need to add |
Had some time so went to test it out... I remember why I disabled gopls's formatting, it's pretty broken for me. 🙁 I'll have to file a new bug for it, but doing something like moving some imports around and saving reformats my imports very wrong, with some disappearing and sometimes even quotes getting misplaced. That, and the local option doesn't appear to have done it for me. I'll try to do better testing tonight, but I foresee staying with EDIT: I got it to work, but only if I use "organize imports". Personally, I want the old goimports behavior of "save the file and everything just works out", so I hope the gopls scheme isn't separate actions for everything I have to remember to run... |
I get gopls version:
|
@zikaeroh: Can you file a new issue with a repro for the case where @alexanderbez: Sorry, forgot to mention that you will also need to install the latest pre-release of |
Opened #35388. I do have all of the recommended options set including the on-save options and an extended timeout, but no dice. |
got it working, but the importing is definitely not right. Probably what @zikaeroh is alluding to. It's not even grouping stdlib imports. |
@alexanderbez: Does it work if you remove an import? |
import (
"fmt"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
"os"
"sort"
"strings"
"syscall"
) what I expect: import (
"fmt"
"os"
"sort"
"strings"
"syscall"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
) assuming |
What is the content of the original file? |
@alexanderbez I think the issue you're seeing falls under the banner of "working as expected". I ran into exactly the same difference in behaviour with existing imports between
"That bug" is a reference to #20818 What's unclear to me is why |
I'm unsure on how to proceed with the gopls config. I would prefer to avoid having to call |
Actually I'm going to correct the comment I made in #32049 (comment) At least with This is demonstrated in the two tests we are adding as part of govim/govim#571 So my best guess would be that any issues we're seeing here are related to version problems that fall under the banner of #35114. Again, fixing those version problems will help us to see more clearly whether there other issues later. |
I'm going to go ahead and close this issue, since the local flag has now been added. Automatic detection of the local module can be tracked through #31999. @alexanderbez and anyone else who is having trouble with imports through |
@stamblerre what is the gopls documentation page where I could learn about the Edit: I think that the page I'm looking for is the Settings wiki page, so maybe the problem is just that that documentation page is missing a mention of this option. |
@cespare: You're right--it is missing from documentation. Just mailed https://golang.org/cl/224117 to add it. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?not relevant
What did you do?
Saved a file in vs code (configured to use gopls), letting gopls fix imports.
What did you expect to see?
Imports grouped into 3 sections: standard library imports, third party library imports, local module imports
What did you see instead?
Only two sections: standard library imports, all other imports
Maybe this is a bike-sheddy style question, but I would like to be able to use gopls but also stick to the old
goimports -local x
behaviour of splitting local dependencies out.If it's too controversial, I'd be happy with a flag, but maybe it's simple enough to make this behaviour default, especially now that go modules make it clear what's "local".
The text was updated successfully, but these errors were encountered: