Skip to content

Commit 807f2a1

Browse files
philsccopybara-github
authored andcommitted
Fix Incompatible Target Skipping for test args
Before the fix, the test in this patch would error out with the following message: ERROR: ...: in args attribute of sh_test rule //target_skipping:foo_test: label '//target_skipping:some_foo3_target' in $(location) expression expands to no files The problem was that the `RunfilesSupport` class for the `RunfilesProvider` instance was computing the arguments as the incompatible target was being constructed. That meant that the arguments get evaluated and it would try to resolve things like `$(location :incompatible_dep)`. That can't really succeed so bazel would throw an error. This patch makes it so arguments are ignored when constructing an incompatible target. Closes #13219. PiperOrigin-RevId: 363233765
1 parent 586de95 commit 807f2a1

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/main/java/com/google/devtools/build/lib/analysis/RunfilesSupport.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,22 @@ public static RunfilesSupport withExecutable(
453453
computeActionEnvironment(ruleContext));
454454
}
455455

456+
/**
457+
* Creates and returns a {@link RunfilesSupport} object for the given rule and executable. This
458+
* version discards all arguments. Only use this for <a
459+
* href="https://docs.bazel.build/versions/master/platforms.html#skipping-incompatible-targets">Incompatible
460+
* Target Skipping</a>.
461+
*/
462+
public static RunfilesSupport withExecutableButNoArgs(
463+
RuleContext ruleContext, Runfiles runfiles, Artifact executable) {
464+
return RunfilesSupport.create(
465+
ruleContext,
466+
executable,
467+
runfiles,
468+
CommandLine.EMPTY,
469+
computeActionEnvironment(ruleContext));
470+
}
471+
456472
private static CommandLine computeArgs(RuleContext ruleContext, CommandLine additionalArgs) {
457473
if (!ruleContext.getRule().isAttrDefined("args", Type.STRING_LIST)) {
458474
// Some non-_binary rules create RunfilesSupport instances; it is fine to not have an args

src/main/java/com/google/devtools/build/lib/analysis/constraints/RuleContextConstraintSemantics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ private static ConfiguredTarget createIncompatibleConfiguredTarget(
10241024
if (!outputArtifacts.isEmpty()) {
10251025
Artifact executable = outputArtifacts.get(0);
10261026
RunfilesSupport runfilesSupport =
1027-
RunfilesSupport.withExecutable(ruleContext, runfiles, executable);
1027+
RunfilesSupport.withExecutableButNoArgs(ruleContext, runfiles, executable);
10281028
builder.setRunfilesSupport(runfilesSupport, executable);
10291029

10301030
ruleContext.registerAction(

src/test/shell/integration/target_compatible_with_test.sh

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,10 @@ function atest_build_event_protocol() {
378378
# incompatible targets are themselves deemed incompatible and should therefore
379379
# not be built.
380380
function test_non_top_level_skipping() {
381-
cat >> target_skipping/BUILD <<EOF
381+
touch target_skipping/foo_test.sh
382+
chmod +x target_skipping/foo_test.sh
383+
384+
cat >> target_skipping/BUILD <<'EOF'
382385
genrule(
383386
name = "genrule_foo1",
384387
target_compatible_with = [":foo1"],
@@ -391,6 +394,15 @@ sh_binary(
391394
srcs = ["foo1.sh"],
392395
target_compatible_with = [":foo2"],
393396
)
397+
398+
# Make sure that using an incompatible target in Make variable substitution
399+
# doesn't produce an unexpected error.
400+
sh_test(
401+
name = "foo_test",
402+
srcs = ["foo_test.sh"],
403+
data = [":some_foo3_target"],
404+
args = ["$(location :some_foo3_target)"],
405+
)
394406
EOF
395407
396408
cd target_skipping || fail "couldn't cd into workspace"
@@ -402,6 +414,14 @@ EOF
402414
//target_skipping:sh_foo2 &> "${TEST_log}" && fail "Bazel passed unexpectedly."
403415
expect_log 'ERROR: Target //target_skipping:sh_foo2 is incompatible and cannot be built, but was explicitly requested'
404416
expect_log 'FAILED: Build did NOT complete successfully'
417+
418+
bazel build \
419+
--show_result=10 \
420+
--host_platform=@//target_skipping:foo2_bar1_platform \
421+
--platforms=@//target_skipping:foo2_bar1_platform \
422+
//target_skipping:foo_test &> "${TEST_log}" && fail "Bazel passed unexpectedly."
423+
expect_log 'ERROR: Target //target_skipping:foo_test is incompatible and cannot be built, but was explicitly requested'
424+
expect_log 'FAILED: Build did NOT complete successfully'
405425
}
406426
407427
# Validate that targets are skipped when the implementation is in Starlark

0 commit comments

Comments
 (0)