8

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?

Gama11
  • 31,714
  • 9
  • 78
  • 100
Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • 2
    The error describes the reason, updating go.mod is disabled because of the `-mod=readonly` flag. Fix `go.mod` so the dependencies can be resolved. – JimB Feb 10 '21 at 17:33
  • 2
    Can you elaborate on what you mean by "fix go.mod"? Shouldn't the extension itself be able to make the necessary updates? – Kurt Peek Feb 10 '21 at 17:40

3 Answers3

9

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.

Kurt Peek
  • 52,165
  • 91
  • 301
  • 526
  • 2
    running `go mod tidy` simply made the required changes to `go.mod`, which the plugin was not allowed to do because it is run with `-mod=readonly` – JimB Feb 10 '21 at 17:43
  • 1
    Ok, and how can I make it such that the plugin is allowed to make those changes and doesn't error out because of a `-mod=readonly` mode? – Kurt Peek Feb 10 '21 at 17:44
  • 1
    You would have to consult the documentation for that particular plugin. Normally you do not want plugins to silently make changes to your module configuration. – JimB Feb 10 '21 at 17:45
5

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.

Pablo Fior
  • 51
  • 1
  • 1
1

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.

Hana
  • 565
  • 5
  • 9