diff --git a/src/main/java/com/google/devtools/build/lib/query2/common/CommonQueryOptions.java b/src/main/java/com/google/devtools/build/lib/query2/common/CommonQueryOptions.java index b653e2b284a824..307191ae09e1d9 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/common/CommonQueryOptions.java +++ b/src/main/java/com/google/devtools/build/lib/query2/common/CommonQueryOptions.java @@ -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 // /////////////////////////////////////////////////////////// diff --git a/src/main/java/com/google/devtools/build/lib/rules/genquery/BUILD b/src/main/java/com/google/devtools/build/lib/rules/genquery/BUILD index 1a881a3b833f2c..f38e962e0e90f9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/genquery/BUILD +++ b/src/main/java/com/google/devtools/build/lib/rules/genquery/BUILD @@ -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", diff --git a/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java b/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java index cc15ac1195b9ca..80c248efcbe87f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java +++ b/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java @@ -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; @@ -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 = diff --git a/src/test/java/com/google/devtools/build/lib/buildtool/GenQueryIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/buildtool/GenQueryIntegrationTest.java index 6169c47e416aac..fd3878e472fc10 100644 --- a/src/test/java/com/google/devtools/build/lib/buildtool/GenQueryIntegrationTest.java +++ b/src/test/java/com/google/devtools/build/lib/buildtool/GenQueryIntegrationTest.java @@ -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()