2

I have added multiple root packages in Visual Studio Code and defined the dependencies and packages properly. When I build my code I am not getting any compiler errors and I am able to run the application. There is no issue in compiling and running the application. But Visual studio code is giving me the following error in all the imports in the go file and also in the package declaration of go.mod files. I tried many suggestions from different sites but the issue is still there. Could you please help me resolve this issue?

Error on import statements:

could not import pdi.com/optctrl/controllers/states (no required module provides package "pdi.com/optctrl/controllers/states"(workspace configuration error: err: exit status 1: stderr: go: updates to go.mod needed, disabled by -mod=readonly; to update it: go mod tidy

Error on package declaration of go.mod:

err: exit status 1: stderr: go: updates to go.mod needed, disabled by -mod=readonly; to update it: go mod tidy

Multiple packages added to VSC

[![Multiple packages added to VSC][1]][1]

Madhusudhan
  • 103
  • 1
  • 1
  • 9
  • 2
    you can resolve this by running `go mod tidy` https://stackoverflow.com/questions/66141927/vs-code-go-extension-fails-to-load-due-to-it-being-unable-to-change-go-mod – whitespace Jul 22 '22 at 05:32
  • I updated Settings.json with ```"build.allowModfileModifications": true``` (one of the suggestions from the link you have given ) and also ran ```go mod tidy```, But it removed errors from import statements. running ```go mod tidy``` didn't help in removing the error on package declaration of go.mod – Madhusudhan Jul 22 '22 at 07:32
  • after running `go mod tidy` did you try restarting VS Code. – whitespace Jul 22 '22 at 08:42
  • Yes, I restarted VS code multiple times. What my observation is, after restarting if I open a do.mod any files I don't see any error but the moment I open a go source file the errors automatically prop up. I mean until I open a go file I don't see any error. That's kind of strange. @whitespace – Madhusudhan Jul 22 '22 at 08:48
  • Having observed similar issues: Depending on the project folder structure vscode seems to have issues with certain projects always, or just some of the time. This seems to be a bug (or to be updated feature) in vscode or the go plugin used in vscode – Norbert Jul 22 '22 at 23:10
  • Where is your main `go.mod`? I can not find it in the vscode explorer in the picture you show. Do you just have added multiple packages into the workspace, without a main `go.mod`? – rustyhu Jul 24 '22 at 03:42
  • Consider each package shown in the image as code for separate microservices and each main package has go.mod defined. For Ex: gvrservice will have an ***src*** folder inside along with go.mod – Madhusudhan Jul 25 '22 at 07:30

0 Answers0