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

Gazelle support for multiple src/ repositories and a src/vendor/ #895

Closed
razvanm opened this issue Oct 11, 2017 · 2 comments
Closed

Gazelle support for multiple src/ repositories and a src/vendor/ #895

razvanm opened this issue Oct 11, 2017 · 2 comments

Comments

@razvanm
Copy link

razvanm commented Oct 11, 2017

I'm trying to use gazelle for a repo that has multiple repositories in the src/ dir and also a src/vendor/. I suspect the solution for this will be #859 but I'm filing this as a separate issue because I want to describe in more detail the scenario I'm struggling with. Feel free to close it if doesn't really value.

Demo repo: https://github.com/razvanm/gazelle-vendor-bug

Scenario: 4 packages (2 in vendored repos, 2 local repos) and one command in one of the local repos that use all 4 packages.

From the view of the go workspace looks like this:

src/vendor/repo.1/pkg1/lib.go
src/vendor/repo.2/pkg2/lib.go
src/repo.3/pkg3/lib.go
src/repo.4/pkg4/lib.go
src/repo.4/cmd/cmd4/main.go

The go flow works as expected:

$ export GOPATH=$PWD
$ go install repo.4/cmd/cmd4
$ ./bin/cmd4
pkg1
pkg2
pkg3
pkg4
$

The repo.4/cmd/cmd4/BUILD is not quite correct. The broken target is the following:

go_library(
    name = "go_default_library",
    srcs = ["main.go"],
    importpath = "repo.4/cmd/cmd4",
    visibility = ["//visibility:private"],
    deps = [
        "//vendor/repo.1/pkg1:go_default_library",
        "//vendor/repo.2/pkg2:go_default_library",
        "//vendor/repo.3/pkg3:go_default_library",
        "//vendor/repo.4/pkg4:go_default_library",
    ],
)

The expected form is the following:

go_library(
    name = "go_default_library",
    srcs = ["main.go"],
    importpath = "repo.4/cmd/cmd4",
    visibility = ["//visibility:private"],
    deps = [
        "//vendor/repo.1/pkg1:go_default_library",
        "//vendor/repo.2/pkg2:go_default_library",
        "//repo.3/pkg3:go_default_library",
        "//repo.4/pkg4:go_default_library",
    ],
)

I use prefix = "." for the gazelle rule in the top BUILD files because an empty value would cause an error.

The full gazelle rule from the top-level BUILD file:

gazelle(
    name = "gazelle",
    prefix = ".",
    args = [
        "-build_file_name=BUILD",
    ],
    external = "vendored",
)
@jayconrod
Copy link
Contributor

Hi @razvanm, thanks for describing this problem. There are a couple odd things here.

First, as you mentioned, is Gazelle's treatment of prefix = ".". At the moment, Gazelle doesn't handle empty prefixes well (#870), and "." has some of the same problems (possibly more if it's considered a relative path). I plan to fix that soon. However, since Gazelle uses the prefix to determine whether an import path is local or external, all import paths will be considered local, so vendored libraries and external repositories will not be used in repositories with empty prefixes. #859 is the ultimate fix for this, but it's a complicated change, and it will take some time.

One other thing to watch out for: your WORKSPACE file is not in your repository root directory. Bazel is fine with this for builds within your repository, but if your repository is used as an external dependency for something else it will ignore your WORKSPACE file and expect all your labels to be rooted at the repository root. If you never intend to use this repository as an external dependency, no change needed, at least from Bazel's perspective.

I'll close this issue because I think everything here is covered by #870 and #859. Please reopen though if there's something that won't work when those are implemented though.

@razvanm
Copy link
Author

razvanm commented Oct 11, 2017

Thanks for the comment. I really hope that the "it will take some time" will not mean a year. :-)

I am aware of the WORKSPACE is not in the repository root directory. I did that because Gazelle places the vendor/ in a fixed place and I wanted this repository to look like a normal Go workspace. There was no intention to make this usable as an external dependency.

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

No branches or pull requests

2 participants