Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: `undeclared name` when using function from another file when both files have build tags #42257

Closed
asankov opened this issue Oct 28, 2020 · 2 comments
Labels
Milestone

Comments

@asankov
Copy link

@asankov asankov commented Oct 28, 2020

What version of Go are you using (go version)?

$ go version
go version go1.15.3 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/asankov/.cache/go-build"
GOENV="/home/asankov/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/asankov/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/asankov/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/lib/golang"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/golang/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/asankov/git/asankov/gira/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build505007625=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I have a suite of integration tests files, all of which live in cmd/integrationtests folder, in a package named integrationtests.
I have a main file which includes functions for setting up the components like:

// integrationtests.go

// +build integration_tests

package integrationtests

import (...)

func setup(t *testing.T) *client.Client {
    // some implementation
    return nil
}

then I have different files with integration tests like users_test.go that use the setup function (without importing anything, because the function is in the same package) and it all works fine.

I created a new file in the same folder and package and when I tried to use the function I got the following error from VS Code:

undeclared name: setup compiler

Screenshot from the error:
Screenshot from 2020-10-28 19-48-24

NOTE: I do see that the error is coming from compile, not lsp, however when running go tool compile games_test.go integration_test.go I do not get any errors and I do not expect to get any errors, because what I am trying to do is valid, and I already do it in multiple files.

NOTE: The project I am working on is open source, so I can show you the package in question.

What did you expect to see?

I expected my code to be able to find the setup function.

What did you see instead?

Error: undeclared name: setup compiler

@gopherbot gopherbot added this to the Unreleased milestone Oct 28, 2020
@heschik
Copy link
Contributor

@heschik heschik commented Oct 28, 2020

Please follow https://github.com/golang/tools/blob/master/gopls/doc/vscode.md#build-tags to configure the build tags you want to use.

@stamblerre stamblerre modified the milestones: Unreleased, gopls/v0.5.3 Oct 28, 2020
@asankov
Copy link
Author

@asankov asankov commented Oct 28, 2020

Setting

"go.toolsEnvVars": {
        "GOFLAGS": "-tags=integration_tests"
    },

in settings.json resolved my problem. I guess what confused me was that it was working for some files, but not others. Closing now, thanks for the help.

@asankov asankov closed this Oct 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants