diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryProcessor.java b/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryProcessor.java index 40fcd67d9c5b40..2d3dff5ff9cb66 100644 --- a/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryProcessor.java +++ b/src/main/java/com/google/devtools/build/lib/buildtool/PostAnalysisQueryProcessor.java @@ -18,6 +18,7 @@ import com.google.devtools.build.lib.buildtool.BuildTool.ExitException; import com.google.devtools.build.lib.cmdline.TargetPattern; import com.google.devtools.build.lib.events.Event; +import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; import com.google.devtools.build.lib.query2.NamedThreadSafeOutputFormatterCallback; import com.google.devtools.build.lib.query2.PostAnalysisQueryEnvironment; import com.google.devtools.build.lib.query2.PostAnalysisQueryEnvironment.TopLevelConfigurations; @@ -160,7 +161,10 @@ private void doPostAnalysisQuery( queryRuntimeHelper.getOutputStreamForQueryOutput(), env.getSkyframeExecutor(), runtime.getRuleClassProvider(), - env.getPackageManager()); + env.getPackageManager(), + env.getSkyframeExecutor() + .getEffectiveStarlarkSemantics( + env.getOptions().getOptions(BuildLanguageOptions.class))); String outputFormat = postAnalysisQueryEnvironment.getOutputFormat(); NamedThreadSafeOutputFormatterCallback callback = NamedThreadSafeOutputFormatterCallback.selectCallback(outputFormat, callbacks); diff --git a/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java index 600fbcf6971cc7..1926d885990104 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/PostAnalysisQueryEnvironment.java @@ -89,6 +89,7 @@ import java.util.function.Supplier; import java.util.stream.Collectors; import javax.annotation.Nullable; +import net.starlark.java.eval.StarlarkSemantics; /** * {@link QueryEnvironment} that runs queries based on results from the analysis phase. @@ -136,7 +137,8 @@ public PostAnalysisQueryEnvironment( OutputStream outputStream, SkyframeExecutor skyframeExecutor, RuleClassProvider ruleClassProvider, - PackageManager packageManager) + PackageManager packageManager, + StarlarkSemantics starlarkSemantics) throws QueryException, InterruptedException; public abstract String getOutputFormat(); diff --git a/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryEnvironment.java index 237499a8e271d5..33dda8c7187466 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/aquery/ActionGraphQueryEnvironment.java @@ -55,6 +55,7 @@ import java.util.Set; import java.util.function.Supplier; import javax.annotation.Nullable; +import net.starlark.java.eval.StarlarkSemantics; /** * {@link QueryEnvironment} that is specialized for running action graph queries over the configured @@ -138,7 +139,8 @@ public ConfiguredTargetValueAccessor getAccessor() { OutputStream out, SkyframeExecutor skyframeExecutor, RuleClassProvider ruleClassProvider, - PackageManager packageManager) { + PackageManager packageManager, + StarlarkSemantics starlarkSemantics) { return ImmutableList.of( new ActionGraphProtoOutputFormatterCallback( eventHandler, diff --git a/src/main/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryEnvironment.java b/src/main/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryEnvironment.java index 7d920381b81859..fbbb8227ce1be7 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/query2/cquery/ConfiguredTargetQueryEnvironment.java @@ -64,6 +64,7 @@ import java.util.function.Function; import java.util.function.Supplier; import javax.annotation.Nullable; +import net.starlark.java.eval.StarlarkSemantics; /** * {@link QueryEnvironment} that runs queries over the configured target (analysis) graph. @@ -193,7 +194,8 @@ private static ImmutableMap getTransitiveConfig OutputStream out, SkyframeExecutor skyframeExecutor, RuleClassProvider ruleClassProvider, - PackageManager packageManager) + PackageManager packageManager, + StarlarkSemantics starlarkSemantics) throws QueryException, InterruptedException { AspectResolver aspectResolver = cqueryOptions.aspectDeps.createResolver(packageManager, eventHandler); @@ -269,7 +271,7 @@ private static ImmutableMap getTransitiveConfig kct -> getFwdDeps(ImmutableList.of(kct)), getMainRepoMapping()), new StarlarkOutputFormatterCallback( - eventHandler, cqueryOptions, out, skyframeExecutor, accessor), + eventHandler, cqueryOptions, out, skyframeExecutor, accessor, starlarkSemantics), new FilesOutputFormatterCallback( eventHandler, cqueryOptions, out, skyframeExecutor, accessor, topLevelArtifactContext)); } diff --git a/src/main/java/com/google/devtools/build/lib/query2/cquery/StarlarkOutputFormatterCallback.java b/src/main/java/com/google/devtools/build/lib/query2/cquery/StarlarkOutputFormatterCallback.java index 752b27fca6a242..a4ecf8707b5263 100644 --- a/src/main/java/com/google/devtools/build/lib/query2/cquery/StarlarkOutputFormatterCallback.java +++ b/src/main/java/com/google/devtools/build/lib/query2/cquery/StarlarkOutputFormatterCallback.java @@ -131,15 +131,18 @@ public Object providers(ConfiguredTarget target) { // Starlark function with single required parameter "target", a ConfiguredTarget query result. private final StarlarkFunction formatFn; + private final StarlarkSemantics starlarkSemantics; StarlarkOutputFormatterCallback( ExtendedEventHandler eventHandler, CqueryOptions options, OutputStream out, SkyframeExecutor skyframeExecutor, - TargetAccessor accessor) + TargetAccessor accessor, + StarlarkSemantics starlarkSemantics) throws QueryException, InterruptedException { - super(eventHandler, options, out, skyframeExecutor, accessor, /*uniquifyResults=*/ false); + super(eventHandler, options, out, skyframeExecutor, accessor, /* uniquifyResults= */ false); + this.starlarkSemantics = starlarkSemantics; ParserInput input = null; String exceptionMessagePrefix; @@ -182,11 +185,11 @@ public Object providers(ConfiguredTarget target) { } try (Mutability mu = Mutability.create("formatter")) { ImmutableMap.Builder env = ImmutableMap.builder(); - Starlark.addMethods(env, new CqueryDialectGlobals(), StarlarkSemantics.DEFAULT); + Starlark.addMethods(env, new CqueryDialectGlobals(), starlarkSemantics); env.putAll(StarlarkGlobalsImpl.INSTANCE.getUtilToplevelsForCquery()); - Module module = Module.withPredeclared(StarlarkSemantics.DEFAULT, env.buildOrThrow()); + Module module = Module.withPredeclared(starlarkSemantics, env.buildOrThrow()); - StarlarkThread thread = new StarlarkThread(mu, StarlarkSemantics.DEFAULT); + StarlarkThread thread = new StarlarkThread(mu, starlarkSemantics); Starlark.execFile(input, FileOptions.DEFAULT, module, thread); Object formatFn = module.getGlobal("format"); if (formatFn == null) { @@ -228,7 +231,7 @@ public void processOutput(Iterable partialResult) throws Inter for (ConfiguredTarget target : partialResult) { try { StarlarkThread thread = - new StarlarkThread(Mutability.create("cquery evaluation"), StarlarkSemantics.DEFAULT); + new StarlarkThread(Mutability.create("cquery evaluation"), starlarkSemantics); thread.setMaxExecutionSteps(500_000L); // Invoke formatFn with `target` argument. diff --git a/src/test/shell/integration/configured_query_test.sh b/src/test/shell/integration/configured_query_test.sh index b853bd67912a17..c4607a7ad728de 100755 --- a/src/test/shell/integration/configured_query_test.sh +++ b/src/test/shell/integration/configured_query_test.sh @@ -893,8 +893,17 @@ EOF --starlark:expr="str(target.label) + '%foo'" > output \ 2>"$TEST_log" || fail "Expected success" - assert_contains "//$pkg:pylib%foo" output - assert_contains "//$pkg:pylibtwo%foo" output + assert_contains "^@//$pkg:pylib%foo$" output + assert_contains "^@//$pkg:pylibtwo%foo$" output + + bazel cquery "//$pkg:all" --output=starlark \ + --noincompatible_unambiguous_label_stringification \ + --starlark:expr="str(target.label) + '%foo'" > output \ + 2>"$TEST_log" || fail "Expected success" + + # Verify use of the effective rather than default Starlark semantics. + assert_contains "^//$pkg:pylib%foo$" output + assert_contains "^//$pkg:pylibtwo%foo$" output # Test that the default for --starlark:expr str(target.label) bazel cquery "//$pkg:all" --output=starlark >output \