Kurt Peek
Kurt Peek

Reputation: 57791

VS Code Go extension fails to load due to it being unable to change go.mod?

This question appears to have been asked in Go updates to go.mod needed, disabled by -mod=readonly : packages.Load error but was not answered there, so re-posting it. Running Go 1.15.7, I'm trying to load a Go project with a go.mod but VS Code is showing a notification with the following error:

Error loading workspace: err: exit status 1: stderr: go: updates to go.mod needed, disabled by -mod=readonly : packages.Load error

enter image description here

It seems that a read-only mode is enabled, but I'm not sure how to disable this. I've tried simply increasing the file permissions on go.mod,

chmod a+w go.mod

but to no avail. Any idea how to fix this error and allow the Go extension to load?

Upvotes: 8

Views: 20904

Answers (3)

Pablo Fior
Pablo Fior

Reputation: 71

You can easily fix this by just adding to your settings.json a gopls build parameter allowing imports from out-of-scope modules.

Something along the lines of:

{
    "gopls": {
    "build.allowModfileModifications": true
    }
}

For reference: https://github.com/golang/vscode-go/blob/master/docs/settings.md#buildallowmodfilemodifications

And a little more context:

In Go 1.16, the Go command will no longer modify user's go.mod and go.sum files automatically (https://tip.golang.org/doc/go1.16#tools). In order to match this behavior, gopls now also uses -mod=readonly when running the go command. Any errors reported by the go command will be presented with a suggested fix to make the necessary fixes to your go.mod or go.sum files. As a consequence, your workspace may be in a partially broken state while you have errors in your go.mod or go.sum file. golang/go#42266 will mitigate this, but it will likely not be resolved until February.

Not recommended: If you must opt out of this behavior, you can set the allowModfileModifications configuration to true.

Upvotes: 7

Hana
Hana

Reputation: 694

Check the PROBLEMS section or the corresponding go.mod file. When such problems are detected, gopls provides quickfix and hints on how to address them there. If not, that's a bug.

Upvotes: 1

Kurt Peek
Kurt Peek

Reputation: 57791

This error appears to have resolved itself by just running

go mod tidy

manually. I'm still curious what caused this, though, as the extension might still not be fully working.

Upvotes: 9

Related Questions