-
Notifications
You must be signed in to change notification settings - Fork 18k
x/tools/gopls: add mode/option/mechanism to allow automatic updates to go.mod #41397
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
Comments
I'm not sure if this will be necessary because the Go command's behavior is changing too: #40728. The BTW, regarding:
/cc @jayconrod |
We have previously discussed modifying |
Perhaps I'm misunderstanding something then. With |
It uses
The two are separate actually. Like @bcmills said, unimported completions don't have module paths and versions associated with them, so it only add imports to Go files. |
Thanks for the clarification on the implementation details, @stamblerre. I've edited my misunderstanding in the description, linking to your explanation in the previous comment.
So this issue is ultimately about auto-applying these diagnostics. I guess options here range from a But it sounds like we also need the functionality @bcmills describes. Is that covered elsewhere? |
This is what VS Code does. Any
I'm not sure about this. @heschik would probably know though. |
Interesting. So is there a manual step involved at all in VSCode? Are these diagnostics automatically applied, or is there some config option that enables/disables this? |
This is the configuration in the VS Code: "[go.mod]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true,
},
} Note that I actually have it disabled because I find it to be too slow, especially when you're typing in the |
Interesting, thank you very much. So it looks like exactly the same could/should be achieved in
Can you expand on this? Is |
Sure, but I just want to reiterate the default behavior of the Go command will be changing. So it may be worth matching that because otherwise you'll have the reverse issue--
I tend to save as I type a lot, which is why I was running into this. Probably much less of an issue with Vim. VS Code also has removed the codeActionOnSaveTimeout feature which used to prevent slow saves. |
For a default I think you're probably right. As I said above, I fall into the "using my editor means I'm implicitly giving the LSP the right to make changes to files I'm working with" camp. But I can quite see why others would prefer the more explicit behaviour this new default introduces, with manual accepts of suggested edits.
Thanks, that's useful context. And thanks again for taking the time to explain this. So, on the basis I think we have a solution here, I'm tempted to suggest we close this? Unless you want to leave it open, @stamblerre, perhaps to track your question to @heschik in #41397 (comment)? |
I think closing is fine--if there was additional work needed to improve |
I just tried this within
I then changed
With
All as expected. With
It seems I misunderstood/interpreted something in our exchange above. Because saving With What I was expecting is that instead of the diagnostic Re-opening because it doesn't appear the current setup supports the workflow detailed in the description. |
You need to request code actions for the diagnostic in the |
So it sounds like to automate this we (
Option 2 doesn't seem feasible because the Option 1 sounds a bit brittle, but better in that we are using the diagnostic edge as a trigger point for working out whether there is something to do. Can this option be refined in some way? |
We could use the diagnostic from option 1 to know where the It would still be fragile to do string matching on diagnostics. Even if we should go for string matching, the response from the The last part could be solved by adding |
They will be the "source.organizeImports" kind - all of the The VS Code config I shared earlier is doing effectively this - basically saying apply any organize import code actions to the |
Oh, sorry I missed that part. A I don't quite follow the part about diagnostics being of a certain kind, are you referring to the e.g.
|
Ah sorry, for kind, I was referring to the CodeActionKind that is part of the Only field in the request. |
I see. Then I can't really see a way to automatically update go.mod as things look today since:
Apologies if I missed why earlier in the conversation, but why can't the code action response for my In that case, should it do things automatically if the user specifies |
This should be the case. When a dependency is missing from the
I don't think that |
My assumption on opening this issue was that, even with |
Sorry, I was speaking about the state pre-1.16 and as of right now. With |
Ok, thanks for clarifying
At which point I think the I ask that only as an observation to clarify, because I think the better solution here is effectively the one we've arrived at in the course of this discussion (thanks for your patience). Namely: match diagnostics for a This has the added benefit of changes to a |
Thanks again for helping to talk this through, @stamblerre. I'll close this for now because I think we've reached agreement on a way forward. I've raised govim/govim#956 to capture actually implementing this change. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
n/a
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
By default
gopls
does not not automatically update a user'sgo.mod
file, a setting established via theTempModfile
approach:https://github.com/golang/tools/blob/64986481280e06c512dcd7674532f4f7f4cfc0c6/internal/lsp/source/options.go#L101
In this mode, in order to make changes to their
go.mod
file, a user either needs to:gopls
go mod tidy
go test
or similarAlternatively, with
TempModfile
turned off,go.mod
files are updated as a side effect ofcmd/go
's various actions, and bothgopls
andcmd/go
use and see the same source of truth.#40728 however moves us to a situation where
-mod=readonly
effectively becomes the default for manycmd/go
commands which appears to rule out us turning offTempModfile
. Therefore the only option is 1 or 2 from above, i.e. manually intervening.This makes the following workflow quite painful:
go test
which now fails under cmd/go: default to & improve -mod=readonly #40728go.mod
because(edit: see x/tools/gopls: add mode/option/mechanism to allow automatic updates to go.mod #41397 (comment))gopls
is maintaining a shadowgo.mod
where the change has been made but the realgo.mod
is as yet unaltered (this is effectively a variant of the concern raised in x/tools/gopls: support multi-module workspaces #32394 (comment))The first step, is, to my mind an implicit acceptance of the fact that a dependency has been added. Indeed, given the import statement is added to the file in question I would suggest the requirement should also also be added. But I can see that some people might want/prefer the explicit step.
Hence, this issue is a request that some mode/option/mechanism be maintained whereby the user can continue to opt in to automatically having changes applied to their
go.mod
file. The example presented involves accepting unimported completions, but generally speaking I do want/needgopls
to make changes to mygo.mod
file as a side effect of what I do viagopls
.cc @stamblerre @heschik @findleyr
FYI @leitzler
The text was updated successfully, but these errors were encountered: