Search code examples
cgovisual-studio-codecgo

VSCode import "C" fails on modules


Please see the following screenshot:

vscode with golang import "C" error

It says:

could not import C (no package data for import path C)

I have attached an example project that reproduces the failure, here: https://github.com/microsoft/vscode/files/3783446/example-project.zip

I am using Go 1.13 with the latest versions of each extension for Go and C/C++. There is no compiler error, and this is specifically seemingly isolated as a "vscode problem".

Is there a way to fix this vscode problem?


Solution

  • This is a bug in gopls tool distributed in the official golang packages.

    Here are the issue links:


    info from issue report:

    internal/lsp: use Go/cgo source files instead of generated files
    
    Using CompiledGoFiles was causing metadata lookups to fail for cgo
    packages because it includes generated files in the Go build cache
    when the built-in 'go list' driver is used.  GoFiles includes both
    Go and cgo original file names, allowing metadata lookups to
    succeed.