bazel-contrib / rules_go

Go rules for Bazel
Apache License 2.0
1.38k stars 656 forks source link

gopackagesdriver does not handle relative queries correctly when run from a subdirectory of the workspace #4001

Closed waltercacau closed 1 month ago

waltercacau commented 2 months ago

What version of rules_go are you using?

Reproduced this on master of https://github.com/bazelbuild/rules_go which as of the time of this writing was at this commit: https://github.com/bazelbuild/rules_go/commit/86a7f025d2fc052ab6154603b481cb7e4419598f

What version of gazelle are you using?

0.34.0

What version of Bazel are you using?

7.2.1

Does this issue reproduce with the latest releases of all the above?

N/A, found this on master of rules_go

What operating system and processor architecture are you using?

Mac OS

Any other potentially useful information about your toolchain?

N/A

What did you do?

This was discovered while I was trying to setup rules_go gopackagedriver with VSCode IDE for a project I work on. In our usecase we work on a big monorepo and projects are subdirectories of that monorepo. The workspace structure looks like this:

WORKSPACE
project1/BUILD.bazel
project1/main.go
project2/BUILD.bazel
project2/main.go
...

The script I setup in vscode looked like this:

#!/usr/bin/env bash
exec bazel run --tool_tag=gopackagesdriver -- @io_bazel_rules_go//go/tools/gopackagesdriver "${@}"

When working on one of the projects, let's say project1, we setup VSCode to only open project1 folder (and therefore the script above would run with working directory being project1. While working on it, gopackagedriver had to respond to the following queries:

./... builtin
file=/the/absolute/path/to/project1/main.go

I expected the first query involving ./... to only return results from project1, but instead it returned stuff for @//:stdlib. Playing with it I also noticed that it did not work as expected for other relative queries like file=./main.go . While my VSCode setup does not send those queries I would have expected them to work and may impact people working on other tools/IDEs.

I was able to reproduce this within https://github.com/bazelbuild/rules_go itself. First to setup:

mkdir -p /tmp/tests &&
cd /tmp/tests &&
git clone https://github.com/bazelbuild/rules_go &&
cd rules_go &&
git checkout 86a7f025d2fc052ab6154603b481cb7e4419598f

Then ran the following test cases: A) (cd go/tools/gopackagesdriver; echo {} | bazel run //go/tools/gopackagesdriver -- ./...) B) (cd go/tools/gopackagesdriver; echo {} | bazel run //go/tools/gopackagesdriver -- file=main.go)

What did you expect to see?

A) Should return results based on packages in //go/tools/gopackagesdriver/... (which only has //go/tools/gopackagesdriver:gopackagesdriver_lib) B) Should return results based on the package that contains go/tools/gopackagesdriver/main.go (which is //go/tools/gopackagesdriver:gopackagesdriver_lib)

What did you see instead?

A) targets based on kind("^(go_library) rule$", ...)] which is all go_library in the repository rather the ones in the subdirectory go/tools/gopackagesdriver

% (cd go/tools/gopackagesdriver; echo {} | bazel run //go/tools/gopackagesdriver -- ./...)
Starting local Bazel server and connecting to it...
INFO: Analyzed target //go/tools/gopackagesdriver:gopackagesdriver (67 packages loaded, 9988 targets configured).
INFO: Found 1 target...
Target //go/tools/gopackagesdriver:gopackagesdriver up-to-date:
  bazel-bin/go/tools/gopackagesdriver/gopackagesdriver_/gopackagesdriver
INFO: Elapsed time: 35.072s, Critical Path: 19.81s
INFO: 10 processes: 6 internal, 4 darwin-sandbox.
INFO: Build completed successfully, 10 total actions
INFO: Running command line: bazel-bin/go/tools/gopackagesdriver/gopackagesdriver_/gopackagesdriver ./...
Running: [bazel info --tool_tag=gopackagesdriver --ui_actions_shown=0]
Running: [bazel query --tool_tag=gopackagesdriver --ui_actions_shown=0 --consistent_labels --ui_event_filters=-info,-stderr --noshow_progress --order_output=no --output=label --nodep_deps --noimplicit_deps --notool_deps kind("^(go_library) rule$", ...)]
Running: [bazel build --tool_tag=gopackagesdriver --ui_actions_shown=0 --show_result=0 --build_event_json_file=/var/folders/qx/6819xgwx62d9jkkh7nlzgc9m0000gp/T/gopackagesdriver_bep_1791540436 --build_event_json_file_path_conversion=no --experimental_convenience_symlinks=ignore --ui_event_filters=-info,-stderr --noshow_progress --aspects=@//go/tools/gopackagesdriver:aspect.bzl%go_pkg_info_aspect --output_groups=go_pkg_driver_json_file,go_pkg_driver_stdlib_json_file,go_pkg_driver_srcs --keep_going --target_pattern_file=/var/folders/qx/6819xgwx62d9jkkh7nlzgc9m0000gp/T/gopackagesdriver_targets_3746790315]
{"NotHandled":false,"Compiler":"gc","Arch":"arm64","Roots":["@//tests/core/go_test/x_defs:foo","@//tests/core/cross:platform_lib",

B) Targets based on the kind("^(go_library|go_test|go_binary) rule$", same_pkg_direct_rdeps("main.go")) query and an error.

% (cd go/tools/gopackagesdriver; echo {} | bazel run //go/tools/gopackagesdriver -- file=main.go)
INFO: Analyzed target //go/tools/gopackagesdriver:gopackagesdriver (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //go/tools/gopackagesdriver:gopackagesdriver up-to-date:
  bazel-bin/go/tools/gopackagesdriver/gopackagesdriver_/gopackagesdriver
INFO: Elapsed time: 0.285s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/go/tools/gopackagesdriver/gopackagesdriver_/gopackagesdriver 'file=main.go'
Running: [bazel info --tool_tag=gopackagesdriver --ui_actions_shown=0]
Running: [bazel query --tool_tag=gopackagesdriver --ui_actions_shown=0 --consistent_labels --ui_event_filters=-info,-stderr --noshow_progress --order_output=no --output=label --nodep_deps --noimplicit_deps --notool_deps kind("^(go_library|go_test|go_binary) rule$", same_pkg_direct_rdeps("main.go"))]
ERROR: no such target '//:main.go': target 'main.go' not declared in package '' defined by /private/tmp/tests/rules_go/BUILD.bazel
error: unable to lookup package: query failed: unable to query: bazel query failed: exit status 7%
waltercacau commented 2 months ago

I believe I have a working fix w/ tests in https://github.com/bazelbuild/rules_go/pull/4002 . Please take a look when you have a chance.

CCing as I saw them reviewing PRs in the affected code @fmeum @JamyDev . Please forward to other folks if I got this wrong :)