Skip to content

Commit

Permalink
[8.0.0] Let genquery honor --consistent_labels (#24377)
Browse files Browse the repository at this point in the history
Fixes #24325

Closes #24326.

PiperOrigin-RevId: 697842646
Change-Id: Ied25448861c5133aae6f266000fe983625f48963

Commit
5141c06

Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
  • Loading branch information
bazel-io and fmeum authored Nov 19, 2024
1 parent 8b0b0f8 commit 986823e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ public LabelPrinter getLabelPrinter(
: LabelPrinter.displayForm(mainRepoMapping);
}

public LabelPrinter getLabelPrinterLegacy(StarlarkSemantics starlarkSemantics) {
return emitConsistentLabels ? LabelPrinter.starlark(starlarkSemantics) : LabelPrinter.LEGACY;
}

///////////////////////////////////////////////////////////
// PROTO OUTPUT FORMATTER OPTIONS //
///////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/concurrent",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/main/java/com/google/devtools/build/lib/packages:label_printer",
"//src/main/java/com/google/devtools/build/lib/pkgcache",
"//src/main/java/com/google/devtools/build/lib/profiler",
"//src/main/java/com/google/devtools/build/lib/query2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.LabelPrinter;
import com.google.devtools.build.lib.packages.NoSuchPackageException;
import com.google.devtools.build.lib.packages.NoSuchTargetException;
import com.google.devtools.build.lib.packages.Package;
Expand Down Expand Up @@ -328,7 +327,8 @@ private static GenQueryResult doQuery(
/* extraFunctions= */ ImmutableList.of(),
/* packagePath= */ null,
/* useGraphlessQuery= */ graphlessQuery,
LabelPrinter.legacy());
queryOptions.getLabelPrinterLegacy(
ruleContext.getAnalysisEnvironment().getStarlarkSemantics()));
QueryExpression expr = QueryExpression.parse(query, queryEnvironment);
formatter.verifyCompatible(queryEnvironment, expr);
targets =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,29 @@ public void testGenQueryOutputCompressed() throws Exception {
assertThat(decompressedOut.toString(UTF_8)).isEqualTo("//fruits:melon\n//fruits:papaya\n");
}

@Test
public void testConsistentLabels() throws Exception {
write(
"fruits/BUILD",
"""
load('//test_defs:foo_library.bzl', 'foo_library')
foo_library(
name = "melon",
deps = [":papaya"],
)
foo_library(name = "papaya")
genquery(
name = "q",
expression = "deps(//fruits:melon)",
scope = [":melon"],
opts = ["--consistent_labels"],
)
""");
assertQueryResult("//fruits:q", "@@//fruits:melon", "@@//fruits:papaya");
}

private void assertQueryResult(String queryTarget, String... expected) throws Exception {
assertThat(getQueryResult(queryTarget).split("\n"))
.asList()
Expand Down

0 comments on commit 986823e

Please sign in to comment.