From 8555789dd239a5ac229c1d9cee80b2a9f30b3bf7 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 7 Dec 2020 02:38:17 -0800 Subject: [PATCH] Fix the classic query package-loading cutoff optimization with external workspaces. A package's path fragment is not unambiguous when external workspaces are involved. Fixes https://github.com/bazelbuild/bazel/issues/12497. Closes #12595. PiperOrigin-RevId: 346048937 --- .../lib/query2/query/BlazeQueryEnvironment.java | 6 +++--- src/test/shell/integration/bazel_query_test.sh | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/query2/query/BlazeQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/query/BlazeQueryEnvironment.java index dada1ea9cd4788..05fc3bd43fab9d 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/query/BlazeQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/query/BlazeQueryEnvironment.java @@ -184,9 +184,9 @@ private void getTargetsMatchingPatternImpl(String pattern, Callback call } } - Set packages = CompactHashSet.create(); + Set packages = CompactHashSet.create(); for (Target target : targets) { - packages.add(target.getLabel().getPackageFragment()); + packages.add(target.getLabel().getPackageIdentifier()); } for (Target target : targets) { @@ -205,7 +205,7 @@ private void getTargetsMatchingPatternImpl(String pattern, Callback call } else if (target instanceof Rule) { Rule rule = (Rule) target; for (Label label : rule.getLabels(dependencyFilter)) { - if (!packages.contains(label.getPackageFragment())) { + if (!packages.contains(label.getPackageIdentifier())) { continue; // don't cause additional package loading } try { diff --git a/src/test/shell/integration/bazel_query_test.sh b/src/test/shell/integration/bazel_query_test.sh index 932fbaec3f8f2f..dc2cf063dd61e9 100755 --- a/src/test/shell/integration/bazel_query_test.sh +++ b/src/test/shell/integration/bazel_query_test.sh @@ -853,4 +853,20 @@ function test_query_failure_exit_code_behavior() { assert_equals 7 "$exit_code" } +function test_unnecessary_external_workspaces_not_loaded() { + cat > WORKSPACE <<'EOF' +local_repository( + name = "notthere", + path = "/nope", +) +EOF + cat > BUILD <<'EOF' +filegroup( + name = "something", + srcs = ["@notthere"], +) +EOF + bazel query '//:*' || fail "Expected success" +} + run_suite "${PRODUCT_NAME} query tests"