govisual-studio-codegopls

fatal error: 'dirent.h' file not found when trying to install gopls/Go tools via Visual Studio Code


I am trying to install the Go tools in Visual Studio Code and am getting the following error when installing gopls:

Tools environment: GOPATH=/Users/jal/go
Installing 1 tool at /Users/jal/go/bin in module mode.
  gopls

Installing golang.org/x/tools/gopls@latest FAILED
{
 "killed": false,
 "code": 2,
 "signal": null,
 "cmd": "/opt/brew/bin/go install -v golang.org/x/tools/gopls@latest",
 "stdout": "",
 "stderr": "golang.org/x/tools/internal/fastwalk\n# golang.org/x/tools/internal/fastwalk\n../../go/pkg/mod/golang.org/x/tools@v0.2.1-0.20221101170700-b5bc717366b2/internal/fastwalk/fastwalk_darwin.go:11:10: fatal error: 'dirent.h' file not found\n#include <dirent.h>\n         ^~~~~~~~~~\n1 error generated.\n"
}

1 tools failed to install.

gopls: failed to install gopls(golang.org/x/tools/gopls@latest): Error: Command failed: /opt/brew/bin/go install -v golang.org/x/tools/gopls@latest
golang.org/x/tools/internal/fastwalk
# golang.org/x/tools/internal/fastwalk
../../go/pkg/mod/golang.org/x/tools@v0.2.1-0.20221101170700-b5bc717366b2/internal/fastwalk/fastwalk_darwin.go:11:10: fatal error: 'dirent.h' file not found
#include <dirent.h>
         ^~~~~~~~~~
1 error generated.

I know I have dirent.h because I see results in my local Xcode SDKs when running find / -iregex ".*/dirent.h$" 2>/dev/null.

I am following the steps here: https://learn.microsoft.com/en-us/azure/developer/go/configure-visual-studio-code

My setup is:

All tools are installed successfully except for gopls. I have installed go via Homebrew and have also installed gopls via Homebrew but am not sure how to configure VSCode to point to this alternative gopls path.


Solution

  • The issue seemed to be due to a misconfigured or corrupted Xcode and command line tools.

    I uninstalled and re-installed the latest version of Xcode, as well as the Command Line Tools package (this one is especially important) from here https://developer.apple.com/download/all/.

    I had to do this despite xcode-select --install telling me everything was installed correctly and up to date.

    Future issue discussion available here: https://github.com/golang/go/issues/56663