Search code examples
govisual-studio-codemulti-modulegolandgopls

How to get GoLand's flawless multi module support on VSCode (no go work)


When I open my multi module project in GoLand (Jetbrains) it works amazingly, with Implementations, Type Declarations and other useful IDE features working fine out of the box.

But when I open that same project in VSCode, I cannot get intellisense to work (with gopls) it does not work and requires severe changes to the module structure with go work, (so it's not an option for me)

Is it because GoLand is using a different language server?

I tried experimenting with the Go extension settings which did not result in any benefit. I was able to disable the language server from turning everything red, but it is not a solution


Solution

  • go workspaces are part of the toolkit and should work on any IDE that supports the feature. I've been using it on VS Code on a daily basis.

    So basically you need a go.work file in the project root and VS Code will interpret it. Example:

    go 1.20
    
    use (
        .
        ./mymodule1
        ./mymodule2
    )
    

    The difference is that GoLand automatically adds the modules in a multi-module setup, and I'm not aware of VS Code having this capability.

    When you create a go.work file, GoLand automatically adds all the modules in the project.

    If you check Go's .gitignore you'll see that the go.work file is ignored by default, so I would assume that you don't have it versioned. I usually comment that line so that VS Code keeps track of it and it goes into my commits.

    # Go workspace file
    go.work
    

    To add new modules to go.work simply do go work use . from a new module root.

    Sometimes I have to reload VS Code to sync it, but usually it works right away.

    If you're still having issues, then probably it is worth trying troubleshooting the extension.