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

Optionally disable generation of go_test rules for vendor and dependencies #37

Open
jayconrod opened this issue Dec 18, 2017 · 10 comments

Comments

@jayconrod
Copy link
Contributor

(Migrated from bazel-contrib/rules_go#725)

In some situations, it may be desirable to ignore tests in dependencies (both in vendor directories and in external repositories). Test rules may introduce additional dependencies and slow down the build.

It should be possible to disable generation of go_test rules via a directive or a command line option (to be passed through go_repository).

@mpassell
Copy link

As a workaround for this, I'm running buildozer delete '//vendor/...:%go_test' after running gazelle.

@ixdy
Copy link
Contributor

ixdy commented Apr 25, 2018

That works. Also, if you're using dep, you can tell it to prune out test files.

@AustinSchuh
Copy link

This works today with

# gazelle:exclude foo_test.go
# gazelle:exclude bar_test.go
...

It would be nice if *_test.go worked, but it's functional as is.

@seh
Copy link
Contributor

seh commented Aug 2, 2019

Is it possible to exclude a path in an external dependency?

My project depends on kubernetes-sigs/controller-runtime, which has test files pkg/webhook/conversion/testdata/main.go and pkg/webhook/conversion/conversion_test.go that import several packages that don't exist.

Whenever I run bazel build, it prints the following warnings:

DEBUG: /private/var/tmp/_bazel_seh/5f212e1971286a055146b31916a9b621/external/bazel_gazelle/internal/go_repository.bzl:184:13: gazelle:
gazelle: finding module path for import testdata.kb.io/api/v1: exit status 1: can't load package: package testdata.kb.io/api/v1: unknown import path "testdata.kb.io/api/v1": cannot find module providing package testdata.kb.io/api/v1
gazelle: finding module path for import testdata.kb.io/api/v2: exit status 1: can't load package: package testdata.kb.io/api/v2: unknown import path "testdata.kb.io/api/v2": cannot find module providing package testdata.kb.io/api/v2
gazelle: finding module path for import testdata.kb.io/api/v3: exit status 1: can't load package: package testdata.kb.io/api/v3: unknown import path "testdata.kb.io/api/v3": cannot find module providing package testdata.kb.io/api/v3

I've tried various paths with gazelle:exclude to try to trap those files, but none of the directives I've tried so far make any difference.

Here are a few of the unsuccessful attempts:

# gazelle:exclude sigs.k8s.io/controller-runtime@v0.2.0-beta.4/pkg/webhook/conversion/testdata/main.go
# gazelle:exclude external/io_k8s_sigs_controller_runtime/pkg/webhook/conversion/testdata/main.go
# gazelle:exclude main.go
# gazelle:exclude sigs.k8s.io/controller-runtime@v0.2.0-beta.4/pkg/webhook/conversion/conversion_test.go
# gazelle:exclude external/io_k8s_sigs_controller_runtime/pkg/webhook/conversion/conversion_test.go
# gazelle:exclude conversion_test.go

@jayconrod
Copy link
Contributor Author

@seh # gazelle:exclude pkg/webhook/conversion/testdata would probably work. That should prevent Gazelle from visiting that directory. But since this is in a go_repository rule, you should probably add build_extra_args = ["-exclude=pkg/webhook/conversion/testdata"] to the go_repository rule instead.

@seh
Copy link
Contributor

seh commented Aug 2, 2019

Adding the build_extra_args parameter as you suggested worked! There ends a dogged pursuit that I confess must have consumed about four hours of my life. Thank you so much.

@jayconrod
Copy link
Contributor Author

Ah, sorry for the trouble. It will be a little simpler in the next version. Since #603, you're able to add directives to the root build file of a go_repository with build_directives. So figuring out the equivalent command-line arguments won't be necessary in most cases.

@bogatuadrian
Copy link

In some situations, it may be desirable to ignore tests in dependencies (both in vendor directories and in external repositories). Test rules may introduce additional dependencies and slow down the build.

#603 doesn't seem to directly address that.

I'm running bazel test with GOPROXY=off (also having GO_REPOSITORY_USE_HOST_CACHE=1) with go modules and I get tens of errors like

gazelle: finding module path for import <package>: exit status 1: can't load package: package <package>: unknown import path "<package>": cannot find module providing package <package>

It seems that gazelle tries to go list packages that are not in my local modules cache (because go modules doesn't cache tests of dependencies) and I get this error.

If I understand correctly I have to manually exclude the test files of my dependencies, but that doesn't scale, having potentially hundreds of files that need to be excluded.

Although I think this is related with this feature request, I can create a separate issue if you'd like.

@ash2k
Copy link
Contributor

ash2k commented May 26, 2020

I'm using update-repos to import external dependencies from go.mod into a macro. It works great, thank you for it! I came to make a suggestion and found this issue.

Would be good to be able to tell generated go_repository invocations to not create go_test targets for all the reasons stated in the comments above. It might make sense to make it the default behavior as most projects likely don't need those targets.

@holycheater
Copy link

just in case someone hit the same problem:
gazelle:exclude some_dir/**/*_test.go seems to do the trick of excluding test files from being generated into BUILD files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants