From 4886b3ed4c4754c17975c9f17fe3e2d232b6ef5c Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 25 Aug 2023 12:54:24 +0200 Subject: [PATCH] Use effective `StarlarkSemantics` with `cquery --output=starlark` Previously, `BuildLanguageOption`s had no effect as the default semantics were used. --- .../lib/buildtool/PostAnalysisQueryProcessor.java | 6 +++++- .../lib/query2/PostAnalysisQueryEnvironment.java | 4 +++- .../aquery/ActionGraphQueryEnvironment.java | 4 +++- .../cquery/ConfiguredTargetQueryEnvironment.java | 6 ++++-- .../cquery/StarlarkOutputFormatterCallback.java | 15 +++++++++------ 5 files changed, 24 insertions(+), 11 deletions(-) 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.