Repository: go
Issue: x/tools/gopls: applied suggested fix in go.mod doesn't remove diagnostic
Labels: gopls,Tools,gopls/metadata

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.16 linux/amd64
</pre>

### Does this issue reproduce with the latest release?
Yes

### What did you do?

With the following scenario:
```
$ txtar -x <<EOF
-- go.mod --
module x

go 1.17
-- main.go --
package main

import _ "rsc.io/quote"

func main() {}
EOF
```

I open **both** `go.mod` and `main.go` (so that applying suggested fixes to `go.mod` are sent as `workspace/applyEdit`). 
Then I choose to apply the suggested edit `go get rsc.io/quote` for the "BrokenImport" diagnostic.

### What did you expect to see?
The diagnostic removed directly (the same way applying fixes to a `.go` file does).

### What did you see instead?
The diagnostic is still there.

Applying the suggested fix makes `gopls` to send `workspace/applyEdit` back that updates `go.mod` into an unsaved state.
I also noticed that `gopls` creates a `go.sum` file on the file system.

Saving `go.mod` do make the diagnostic go away.

gopls.log:
<details>

```
serve.go:414: debug server listening at http://localhost:40415
[Trace - 22:53:24.703 PM] Sending request 'initialize - (1)'.
Params: {"processId":0,"clientInfo":{"name":""},"rootUri":"file:///tmp/tmp.Qf2Bx3OXte","capabilities":{"Workspace":{"workspaceEdit":{"changeAnnotationSupport":{}},"didChangeConfiguration":{"dynamicRegistration":true},"didChangeWatchedFiles":{"dynamicRegistration":true},"symbol":{"symbolKind":{},"tagSupport":{"valueSet":null}},"executeCommand":{},"semanticTokens":{},"codeLens":{},"fileOperations":{},"configuration":true},"textDocument":{"synchronization":{},"completion":{"completionItem":{"tagSupport":{"valueSet":null},"resolveSupport":{"properties":null},"insertTextModeSupport":{"valueSet":null}},"completionItemKind":{}},"hover":{"contentFormat":["plaintext"]},"signatureHelp":{"signatureInformation":{"parameterInformation":{}}},"declaration":{},"definition":{},"typeDefinition":{},"implementation":{},"references":{},"documentHighlight":{},"documentSymbol":{"symbolKind":{},"tagSupport":{"valueSet":null}},"codeAction":{"codeActionLiteralSupport":{"codeActionKind":{"valueSet":null}},"resolveSupport":{"properties":null}},"codeLens":{},"documentLink":{},"colorProvider":{},"formatting":{},"rangeFormatting":{},"onTypeFormatting":{},"rename":{},"foldingRange":{},"selectionRange":{},"publishDiagnostics":{"tagSupport":{"valueSet":null}},"callHierarchy":{},"semanticTokens":{"requests":{},"tokenTypes":null,"tokenModifiers":null,"formats":null},"linkedEditingRange":{},"moniker":{}},"Window":{"showMessage":{"messageActionItem":{}},"showDocument":{"support":false},"workDoneProgress":true},"general":{"staleRequestSupport":{"cancel":false,"retryOnContentModified":null},"regularExpressions":{"engine":""},"markdown":{"parser":""}}},"initializationOptions":{"allowModfileModifications":false,"symbolMatcher":"fuzzy","symbolStyle":"full"},"workspaceFolders":null}


[Trace - 22:53:24.706 PM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2021/02/19 22:53:24 Handshake session update\n\tupdate_session=8\n\tdebug_address=\"127.0.0.1:40415\"\n\tlogfile=\"/tmp/gopls_20210219_2253_24_878196270.log\"\n\tserver=\"1\"\n\tgopls_path=\"/home/leitzler/.vim/plugged/govim/cmd/govim/.bin/a8b8ff21e5a8e67a4f251e7d4d6d67c7a49b5a43/gopls\"\n"}


[Trace - 22:53:24.759 PM] Received response 'initialize - (1)' in 56ms.
Result: {"capabilities":{"textDocumentSync":{"change":2,"openClose":true,"save":{}},"completionProvider":{"triggerCharacters":["."]},"hoverProvider":true,"signatureHelpProvider":{"triggerCharacters":["(",","]},"definitionProvider":true,"typeDefinitionProvider":true,"implementationProvider":true,"referencesProvider":true,"documentHighlightProvider":true,"documentSymbolProvider":true,"codeActionProvider":true,"codeLensProvider":{},"documentLinkProvider":{},"workspaceSymbolProvider":true,"documentFormattingProvider":true,"documentOnTypeFormattingProvider":{"firstTriggerCharacter":""},"renameProvider":true,"foldingRangeProvider":true,"executeCommandProvider":{"commands":["gopls.add_dependency","gopls.add_import","gopls.apply_fix","gopls.check_upgrades","gopls.gc_details","gopls.generate","gopls.generate_gopls_mod","gopls.go_get_package","gopls.list_known_packages","gopls.regenerate_cgo","gopls.remove_dependency","gopls.run_tests","gopls.test","gopls.tidy","gopls.toggle_gc_details","gopls.update_go_sum","gopls.upgrade_dependency","gopls.vendor"]},"callHierarchyProvider":true},"serverInfo":{"name":"gopls","version":"{\"path\":\"golang.org/x/tools/gopls\",\"version\":\"v0.0.0-20210219012152-f3748ed8ec89\",\"sum\":\"h1:gv7t9S5r+UFeolH4lCH46k4fF0kVo1iQJMQ5/kM5BzA=\",\"deps\":[{\"path\":\"github.com/BurntSushi/toml\",\"version\":\"v0.3.1\",\"sum\":\"h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=\"},{\"path\":\"github.com/google/go-cmp\",\"version\":\"v0.5.4\",\"sum\":\"h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=\"},{\"path\":\"github.com/sergi/go-diff\",\"version\":\"v1.1.0\",\"sum\":\"h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=\"},{\"path\":\"golang.org/x/mod\",\"version\":\"v0.4.1\",\"sum\":\"h1:Kvvh58BN8Y9/lBi7hTekvtMpm07eUZ0ck5pRHpsMWrY=\"},{\"path\":\"golang.org/x/sync\",\"version\":\"v0.0.0-20201020160332-67f06af15bc9\",\"sum\":\"h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=\"},{\"path\":\"golang.org/x/sys\",\"version\":\"v0.0.0-20210124154548-22da62e12c0c\",\"sum\":\"h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk=\"},{\"path\":\"golang.org/x/tools\",\"version\":\"v0.1.1-0.20210219012152-f3748ed8ec89\",\"sum\":\"h1:CEQ9B2ID7kyvD17eBaAu+yYOtnawoOFcUxxtCVRBPzo=\"},{\"path\":\"golang.org/x/xerrors\",\"version\":\"v0.0.0-20200804184101-5ec99f83aff1\",\"sum\":\"h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=\"},{\"path\":\"honnef.co/go/tools\",\"version\":\"v0.1.1\",\"sum\":\"h1:EVDuO03OCZwpV2t/tLLxPmPiomagMoBOgfPt0FM+4IY=\"},{\"path\":\"mvdan.cc/gofumpt\",\"version\":\"v0.1.0\",\"sum\":\"h1:hsVv+Y9UsZ/mFZTxJZuHVI6shSQCtzZ11h1JEFPAZLw=\"},{\"path\":\"mvdan.cc/xurls/v2\",\"version\":\"v2.2.0\",\"sum\":\"h1:NSZPykBXJFCetGZykLAxaL6SIpvbVy/UFEniIfHAa8A=\"}]}"}}


[Trace - 22:53:24.761 PM] Sending notification 'initialized'.
Params: {}


[Trace - 22:53:24.762 PM] Received request 'window/workDoneProgress/create - (1)'.
Params: {"token":"894385949183117216"}


[Trace - 22:53:24.770 PM] Sending response 'window/workDoneProgress/create - (1)' in 8ms.
Result:


[Trace - 22:53:24.771 PM] Received notification '$/progress'.
Params: {"token":"894385949183117216","value":{"kind":"begin","message":"Loading packages...","title":"Setting up workspace"}}


[Trace - 22:53:24.771 PM] Received request 'workspace/configuration - (2)'.
Params: {"items":[{"scopeUri":"file:///tmp/tmp.Qf2Bx3OXte","section":"gopls"}]}


[Trace - 22:53:24.773 PM] Sending notification 'textDocument/didOpen'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.Qf2Bx3OXte/go.mod","languageId":"go.mod","version":1,"text":"module x\n\ngo 1.17\n"}}


[Trace - 22:53:24.778 PM] Sending response 'workspace/configuration - (2)' in 6ms.
Result: [{"analyses":{"fieldalignment":true,"fillstruct":true},"codelenses":{"gc_details":true},"hoverKind":"FullDocumentation","staticcheck":true,"tempModfile":true}]


[Trace - 22:53:24.851 PM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2021/02/19 22:53:24 go env for /tmp/tmp.Qf2Bx3OXte\n(root /tmp/tmp.Qf2Bx3OXte)\n(go version go version go1.16 linux/amd64)\n(valid build configuration = true)\n(build flags: [])\nGOPRIVATE=\nGOPROXY=https://proxy.golang.org,direct\nGOROOT=/usr/local/go\nGOMODCACHE=/home/leitzler/go/pkg/mod\nGOPATH=/home/leitzler/go\nGOSUMDB=sum.golang.org\nGONOPROXY=\nGONOSUMDB=\nGOINSECURE=\nGOMOD=/tmp/tmp.Qf2Bx3OXte/go.mod\nGO111MODULE=\nGOCACHE=/home/leitzler/.cache/go-build\nGOFLAGS=\n\n"}


[Trace - 22:53:24.948 PM] Received notification '$/progress'.
Params: {"token":"894385949183117216","value":{"kind":"end","message":"Finished loading packages."}}


[Trace - 22:53:24.956 PM] Received request 'client/registerCapability - (3)'.
Params: {"registrations":[{"id":"workspace/didChangeWatchedFiles-0","method":"workspace/didChangeWatchedFiles","registerOptions":{"watchers":[{"globPattern":"**/*.{go,mod,sum}","kind":7}]}}]}


[Trace - 22:53:24.957 PM] Sending response 'client/registerCapability - (3)' in 0ms.
Result:


[Trace - 22:53:24.957 PM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2021/02/19 22:53:24 go/packages.Load\n\tsnapshot=0\n\tdirectory=/tmp/tmp.Qf2Bx3OXte\n\tquery=[builtin x/...]\n\tpackages=2\n"}


[Trace - 22:53:24.957 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/02/19 22:53:24 x: no dep handle for rsc.io/quote: no metadata for rsc.io/quote\n\tsnapshot=0\n"}


[Trace - 22:53:24.958 PM] Received request 'client/registerCapability - (4)'.
Params: {"registrations":[{"id":"workspace/didChangeConfiguration","method":"workspace/didChangeConfiguration"},{"id":"workspace/didChangeWorkspaceFolders","method":"workspace/didChangeWorkspaceFolders"}]}


[Trace - 22:53:24.958 PM] Sending response 'client/registerCapability - (4)' in 0ms.
Result:


[Trace - 22:53:24.966 PM] Received notification 'textDocument/publishDiagnostics'.
Params: {"uri":"file:///tmp/tmp.Qf2Bx3OXte/main.go","diagnostics":[{"range":{"start":{"line":2,"character":9},"end":{"line":2,"character":23}},"severity":1,"code":"BrokenImport","codeDescription":{"href":"pkg.go.dev/golang.org/x/tools/internal/typesinternal#BrokenImport"},"source":"compiler","message":"could not import rsc.io/quote (no required module provides package \"rsc.io/quote\")"}]}


[Trace - 22:53:24.966 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/02/19 22:53:24 diagnosing go.mod: err: exit status 1: stderr: go: finding module for package rsc.io/quote\nx imports\n\trsc.io/quote: cannot find module providing package rsc.io/quote: module lookup disabled by GOPROXY=off\n\n"}


[Trace - 22:53:25.312 PM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2021/02/19 22:53:25 go/packages.Load\n\tsnapshot=1\n\tdirectory=/tmp/tmp.Qf2Bx3OXte\n\tquery=[builtin x/...]\n\tpackages=2\n"}


[Trace - 22:53:25.313 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/02/19 22:53:25 x: no dep handle for rsc.io/quote: no metadata for rsc.io/quote\n\tsnapshot=1\n"}


[Trace - 22:53:25.320 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/02/19 22:53:25 diagnosing go.mod: err: exit status 1: stderr: go: finding module for package rsc.io/quote\nx imports\n\trsc.io/quote: cannot find module providing package rsc.io/quote: module lookup disabled by GOPROXY=off\n\n"}


[Trace - 22:53:27.867 PM] Sending notification 'textDocument/didOpen'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.Qf2Bx3OXte/main.go","languageId":"go","version":1,"text":"package main\n\nimport _ \"rsc.io/quote\"\n\nfunc main() {}\n"}}


[Trace - 22:53:27.869 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/02/19 22:53:27 x: no dep handle for rsc.io/quote: no metadata for rsc.io/quote\n\tsnapshot=2\n"}


[Trace - 22:53:28.128 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/02/19 22:53:28 diagnosing go.mod: err: exit status 1: stderr: go: finding module for package rsc.io/quote\nx imports\n\trsc.io/quote: cannot find module providing package rsc.io/quote: module lookup disabled by GOPROXY=off\n\n"}


[Trace - 22:53:28.133 PM] Sending request 'textDocument/documentHighlight - (2)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.Qf2Bx3OXte/main.go"},"position":{"line":2,"character":9}}


[Trace - 22:53:28.135 PM] Received response 'textDocument/documentHighlight - (2)' in 2ms.
Result: [{"range":{"start":{"line":2,"character":7},"end":{"line":2,"character":23}},"kind":1}]


[Trace - 22:53:31.171 PM] Sending request 'textDocument/codeAction - (3)'.
Params: {"textDocument":{"uri":"file:///tmp/tmp.Qf2Bx3OXte/main.go"},"range":{"start":{"line":2,"character":9},"end":{"line":2,"character":9}},"context":{"diagnostics":[{"range":{"start":{"line":2,"character":9},"end":{"line":2,"character":23}},"severity":1,"code":"BrokenImport","codeDescription":{"href":"pkg.go.dev/golang.org/x/tools/internal/typesinternal#BrokenImport"},"source":"compiler","message":"could not import rsc.io/quote (no required module provides package \"rsc.io/quote\")"}],"only":["quickfix"]}}


[Trace - 22:53:31.174 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/02/19 22:53:31 diagnosing go.mod: err: exit status 1: stderr: go: finding module for package rsc.io/quote\nx imports\n\trsc.io/quote: cannot find module providing package rsc.io/quote: module lookup disabled by GOPROXY=off\n\n"}


[Trace - 22:53:31.174 PM] Received response 'textDocument/codeAction - (3)' in 2ms.
Result: [{"title":"go get package rsc.io/quote","kind":"quickfix","diagnostics":[{"range":{"start":{"line":2,"character":9},"end":{"line":2,"character":23}},"severity":1,"code":"BrokenImport","codeDescription":{"href":"pkg.go.dev/golang.org/x/tools/internal/typesinternal#BrokenImport"},"source":"compiler","message":"could not import rsc.io/quote (no required module provides package \"rsc.io/quote\")"}],"edit":{},"command":{"title":"go get package rsc.io/quote","command":"gopls.go_get_package","arguments":[{"URI":"file:///tmp/tmp.Qf2Bx3OXte/main.go","Pkg":"rsc.io/quote","AddRequire":true}]}}]


[Trace - 22:53:32.480 PM] Sending request 'workspace/executeCommand - (4)'.
Params: {"command":"gopls.go_get_package","arguments":[{"URI":"file:///tmp/tmp.Qf2Bx3OXte/main.go","Pkg":"rsc.io/quote","AddRequire":true}]}


[Trace - 22:53:32.481 PM] Received request 'window/workDoneProgress/create - (5)'.
Params: {"token":"2775422040480279449"}


[Trace - 22:53:32.482 PM] Sending response 'window/workDoneProgress/create - (5)' in 0ms.
Result:


[Trace - 22:53:32.483 PM] Received notification '$/progress'.
Params: {"token":"2775422040480279449","value":{"cancellable":true,"kind":"begin","message":"Running...","title":"Running go get"}}


[Trace - 22:53:32.812 PM] Received request 'workspace/applyEdit - (6)'.
Params: {"edit":{"documentChanges":[{"textDocument":{"version":1,"uri":"file:///tmp/tmp.Qf2Bx3OXte/go.mod"},"edits":[{"range":{"start":{"line":3,"character":0},"end":{"line":3,"character":0}},"newText":"\nrequire rsc.io/quote v1.5.2\n"}]}]}}


[Trace - 22:53:32.813 PM] Sending notification 'workspace/didChangeWatchedFiles'.
Params: {"changes":[{"uri":"file:///tmp/tmp.Qf2Bx3OXte/go.sum","type":1}]}


[Trace - 22:53:32.813 PM] Sending notification 'workspace/didChangeWatchedFiles'.
Params: {"changes":[{"uri":"file:///tmp/tmp.Qf2Bx3OXte/go.sum","type":2}]}


[Trace - 22:53:32.816 PM] Sending notification 'textDocument/didChange'.
Params: {"textDocument":{"version":2,"uri":"file:///tmp/tmp.Qf2Bx3OXte/go.mod"},"contentChanges":[{"text":"module x\n\ngo 1.17\n\nrequire rsc.io/quote v1.5.2\n"}]}


[Trace - 22:53:32.817 PM] Sending response 'workspace/applyEdit - (6)' in 5ms.
Result: {"applied":true}


[Trace - 22:53:32.818 PM] Received response 'workspace/executeCommand - (4)' in 337ms.
Result: null


[Trace - 22:53:32.818 PM] Received notification '$/progress'.
Params: {"token":"2775422040480279449","value":{"kind":"end","message":"completed"}}


[Trace - 22:53:32.939 PM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2021/02/19 22:53:32 go/packages.Load\n\tsnapshot=3\n\tdirectory=/tmp/tmp.Qf2Bx3OXte\n\tquery=[builtin x/...]\n\tpackages=2\n"}


[Trace - 22:53:32.940 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/02/19 22:53:32 x: no dep handle for rsc.io/quote: no metadata for rsc.io/quote\n\tsnapshot=3\n"}


[Trace - 22:53:33.042 PM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2021/02/19 22:53:33 go/packages.Load\n\tsnapshot=4\n\tdirectory=/tmp/tmp.Qf2Bx3OXte\n\tquery=[builtin x/...]\n\tpackages=2\n"}


[Trace - 22:53:33.042 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/02/19 22:53:33 x: no dep handle for rsc.io/quote: no metadata for rsc.io/quote\n\tsnapshot=4\n"}


[Trace - 22:53:33.296 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/02/19 22:53:33 x: no dep handle for rsc.io/quote: no metadata for rsc.io/quote\n\tsnapshot=5\n"}
```

</details>