Skip to content

Commit

Permalink
Use effective StarlarkSemantics with cquery --output=starlark
Browse files Browse the repository at this point in the history
Previously, `BuildLanguageOption`s had no effect as the default
semantics were used.
  • Loading branch information
fmeum committed Sep 13, 2023
1 parent a75644e commit 4886b3e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<T> callback =
NamedThreadSafeOutputFormatterCallback.selectCallback(outputFormat, callbacks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -193,7 +194,8 @@ private static ImmutableMap<String, BuildConfigurationValue> getTransitiveConfig
OutputStream out,
SkyframeExecutor skyframeExecutor,
RuleClassProvider ruleClassProvider,
PackageManager packageManager)
PackageManager packageManager,
StarlarkSemantics starlarkSemantics)
throws QueryException, InterruptedException {
AspectResolver aspectResolver =
cqueryOptions.aspectDeps.createResolver(packageManager, eventHandler);
Expand Down Expand Up @@ -269,7 +271,7 @@ private static ImmutableMap<String, BuildConfigurationValue> 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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConfiguredTarget> accessor)
TargetAccessor<ConfiguredTarget> 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;
Expand Down Expand Up @@ -182,11 +185,11 @@ public Object providers(ConfiguredTarget target) {
}
try (Mutability mu = Mutability.create("formatter")) {
ImmutableMap.Builder<String, Object> 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) {
Expand Down Expand Up @@ -228,7 +231,7 @@ public void processOutput(Iterable<ConfiguredTarget> 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.
Expand Down

0 comments on commit 4886b3e

Please sign in to comment.