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.

Work towards bazelbuild#17864

Closes bazelbuild#19334.

PiperOrigin-RevId: 566747820
Change-Id: I5071f97c68e02808ea8183b9c52c89a2e8e39620
  • Loading branch information
fmeum authored and copybara-github committed Sep 19, 2023
1 parent 20cdacc commit 1447f24
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 13 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 @@ -137,7 +138,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 @@ -56,6 +56,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 @@ -143,7 +144,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 @@ -65,6 +65,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 @@ -198,7 +199,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 @@ -266,7 +268,7 @@ eventHandler, cqueryOptions, out, skyframeExecutor, accessor, getLabelPrinter())
kct -> getFwdDeps(ImmutableList.of(kct)),
getLabelPrinter()),
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
13 changes: 11 additions & 2 deletions src/test/shell/integration/configured_query_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down

0 comments on commit 1447f24

Please sign in to comment.