Hi, I have a test that will use `packages`to fetch...
# general
b
Hi, I have a test that will use `packages`to fetch defined structs in a local file. however, it can't find anything when I ran it with pants 2.14. it works fine with
go test
though
Copy code
import (
    "<http://golang.org/x/tools/go/packages|golang.org/x/tools/go/packages>"
    "go/token"
)
const loadMode = packages.NeedName |
	packages.NeedFiles |
	packages.NeedCompiledGoFiles |
	packages.NeedImports |
	packages.NeedDeps |
	packages.NeedTypes |
	packages.NeedSyntax |
	packages.NeedTypesInfo

loadConfig := packages.Config{
	Mode: loadMode,
	Fset: token.NewFileSet(),
}

pkgs, err := packages.Load(&loadConfig, packageName)
the packageName is any local go file with the full go module path
h
What does your BUILD file look like? Presumably you need to add an explicit dependency on the structs file
b
Copy code
go_package(
    dependencies=[":gofile"],
)

file(
    name="gofile",
    source="test_cases/models.go",
)
the packageName is like github.com/xxx/xxx/.../test_cases
f
change
file
to
resource
for the Go backend,
resource
is included in the package and
file
is made available as a loose file not in the package when running tests (present relative to the directory in which the test runner is running)
but if this for a test case, why not just use
file
and access the file in the local filesystem?
go
guarantees the current directory for the test runner is the package directory. and pants does the same (although in the execution sandbox)
for example. the cgo tests in the
go
sources do this. https://github.com/golang/go/tree/master/misc/cgo -- you will note test data is also put under a
testdata
directory which
go
generally uses for fixtures. Pants respects that convention and will ignore anything under
testdata
when running
./pants tailor
so the test
.go
file will never have a BUILD file written for it.
any way, can you provide more context for this particular use case so I may better understand the use of
<http://golang.org/x/tools/go/packages|golang.org/x/tools/go/packages>
?
b
I use this package to detect defined structs in another directory and generate code based on that. I am trying to write some unit tests for my code and the tests complains not able to find my structs defined under the go import path
i tried the resource but no luck.
f
would you be able to provide a minimal reproduction in a repo somewhere?
b
if you run
go test ./...
it would pass the test but if run with
./pants test ::
, it would fail
Hello, do you have a chance to take a look? Thanks
f
I have not yet had a chance.