Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use effective StarlarkSemantics with cquery --output=starlark #19334

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading