3

I use a custom location for GOMODCACHE (~/.gomodcache/) and GOPATH ~/project/go. This is to handle the errors from GOPLS while using the mono-repo approach of workspace.

When I do go get, the modules are downloaded to GOMODCACHE but, unable to see them in GOPATH. I use VSCode’s Go plugin. The plugin just looks for the list of modules from GOPATH and GOROOT. So, the plugin is unable to find the modules from GOMODCACHE and unable to provide suggestions on autocomplete.

Is there a way to get the modules into GOPATH instead of GOMODCACHE? Or Is there a way to make vscode to read the modules from GOMODCACHE instead of GOPATH?

Ram
  • 103
  • 6
  • `go get` stores modules in `GOMODCACHE`, which you have set outside `GOPATH`. It doesn't actually matter where these are, since it's a cache which you are not supposed to modify or access these files directly. – JimB Jun 03 '21 at 19:47
  • @JimB, Golang team recommended me to move the cache from gopath due to my workspace structure. You can take a look at this for more details - https://github.com/golang/go/issues/45184#issuecomment-809886651. But, the issue is how to make vscode plugin to get the modules from GOMODCACHE? – Ram Jun 03 '21 at 20:24
  • I'm afraid I don't understand what you're trying to do here. If you are using modules correctly, then the tooling will not look in `GOPATH` (`GOPATH` is now only a location for the default `GOBIN` and `GOMODCACHE`, you don't need it for anything else). If you are using `GOPATH`, then you cannot use modules. `GOPATH` builds are deprecated, so I would figure out what you are doing wrong with modules rather than searching for a way to use both simultaneously. – JimB Jun 03 '21 at 20:29
  • Thanks @JimB. Got your point. I'm currently using modules. I'm able to run my code. But, The vscode's Go plugin is just looking for the modules at GOPATH and GOROOT. So, should it be fixed by plugin team? – Ram Jun 03 '21 at 20:39
  • I don’t often use vscode, but the few times I tried it recently I did no configuration and modules worked just fine. Perhaps you have some old config settings that need to be removed. – JimB Jun 03 '21 at 21:38
  • You should be able to change it by setting `go.gopath=$GOCACHE` in your project's `settings.json` – notorious.no Jun 04 '21 at 00:44
  • @notorious.no, your suggestion didn't work. – Ram Jun 04 '21 at 03:29
  • Did you literally use `$GOCACHE` or did you replace it with the path to your cache? – notorious.no Jun 05 '21 at 15:06
  • I replaced with the actual path. – Ram Jun 08 '21 at 19:52
  • Check if GOMODCACHE change is applied to the go language server. You can either check using "Go: Locate Configured Go tools" output (in particular, the go env section), or by checking the gopls log - that will print out the GOMODCACHE value the language server (gopls) is seeing. – Hana Aug 10 '21 at 14:36

2 Answers2

0

It worked for me with the below steps :

  1. run go mod init for main.go which will generate go.mod file
  2. run go get for required packages which will generate go.sum
benson23
  • 16,369
  • 9
  • 19
  • 38
RamaV
  • 1
0

Here is the answer: https://levelup.gitconnected.com/using-modules-and-packages-in-go-36a418960556 You should try to play with GO111MODULE env variable. In the article everything is written.

With GO111MODULE=on you use go install gthub.com/blabla/ and it installs mods in GOPATH/pkg/mod.

With GO111MODULE=off you use go get github.com/blabla and it puts mods in GOPATH/src directory.

raaa
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '23 at 12:55