Skip to content

Commit 84fadcf

Browse files
philsccopybara-github
authored andcommitted
Fix builds for filegroup targets with incompatible dependencies
When building a `filegroup` with incompatible dependencies, the logic was such that the incompatible dependencies would actually be built by bazel. These dependencies only expose `FailAction`s so the build failed. This is caused by bazel skipping the incompatibility check for any rules that don't participate in toolchain resolution. For example, without the fix in `RuleContextConstraintSemantics.java`, the added unit test fails with: ERROR: /home/phil/.cache/bazel/_bazel_phil/b4868b516be4e3bea4d221937a55ced4/sandbox/linux-sandbox/46/execroot/io_bazel/_tmp/c749fd38ac9a4ad6bd41e9653bbceab5/workspace/target_skipping/BUILD:103:10: Reporting failed target //target_skipping:binary located at /home/phil/.cache/bazel/_bazel_phil/b4868b516be4e3bea4d221937a55ced4/sandbox/linux-sandbox/46/execroot/io_bazel/_tmp/c749fd38ac9a4ad6bd41e9653bbceab5/workspace/target_skipping/BUILD:103:10 failed: Can't build this. This target is incompatible. Please file a bug upstream. caused by //target_skipping:binary Target //target_skipping:filegroup failed to build This patch makes it so targets that don't use toolchain resolution skip the check for the `target_compatible_with` attribute, but still check for incompatible dependencies. Closes #12601. PiperOrigin-RevId: 345263391
1 parent 65e9732 commit 84fadcf

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -854,12 +854,9 @@ public static ConfiguredTarget incompatibleConfiguredTarget(
854854
RuleContext ruleContext,
855855
OrderedSetMultimap<DependencyKind, ConfiguredTargetAndData> prerequisiteMap)
856856
throws ActionConflictException, InterruptedException {
857-
if (!ruleContext.getRule().getRuleClassObject().useToolchainResolution()) {
858-
return null;
859-
}
860-
861-
// This is incompatible if explicitly specified to be.
862-
if (ruleContext.attributes().has("target_compatible_with")) {
857+
// The target (ruleContext) is incompatible if explicitly specified to be.
858+
if (ruleContext.getRule().getRuleClassObject().useToolchainResolution()
859+
&& ruleContext.attributes().has("target_compatible_with")) {
863860
ImmutableList<ConstraintValueInfo> invalidConstraintValues =
864861
stream(
865862
PlatformProviderUtils.constraintValues(

src/test/shell/integration/target_compatible_with_test.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,51 @@ function test_console_log_for_tests() {
252252
expect_log '^//target_skipping:pass_on_foo1_bar2 * PASSED in'
253253
}
254254
255+
# Validates that filegroups (i.e. a rule that doesn't use toolchain resolution)
256+
# is correctly skipped when it depends on an incompatible target. This is a
257+
# regression test for https://github.com/bazelbuild/bazel/issues/12582.
258+
function test_filegroup() {
259+
cat > target_skipping/binary.cc <<EOF
260+
#include <cstdio>
261+
int main() {
262+
return 0;
263+
}
264+
EOF
265+
266+
cat >> target_skipping/BUILD <<EOF
267+
cc_binary(
268+
name = "binary",
269+
srcs = ["binary.cc"],
270+
target_compatible_with = [
271+
":foo3",
272+
],
273+
)
274+
275+
filegroup(
276+
name = "filegroup",
277+
srcs = [
278+
":binary",
279+
],
280+
)
281+
EOF
282+
283+
cd target_skipping || fail "couldn't cd into workspace"
284+
285+
bazel build \
286+
--show_result=10 \
287+
--host_platform=@//target_skipping:foo1_bar1_platform \
288+
--platforms=@//target_skipping:foo1_bar1_platform \
289+
:all &> "${TEST_log}" || fail "Bazel failed unexpectedly."
290+
expect_log 'Target //target_skipping:filegroup was skipped'
291+
292+
bazel build \
293+
--show_result=10 \
294+
--host_platform=@//target_skipping:foo1_bar1_platform \
295+
--platforms=@//target_skipping:foo1_bar1_platform \
296+
:filegroup &> "${TEST_log}" && fail "Bazel passed unexpectedly."
297+
expect_log 'Target //target_skipping:filegroup is incompatible and cannot be built'
298+
}
299+
255300
# Validates that incompatible target skipping errors behave nicely with
256301
# --keep_going. In other words, even if there's an error in the target skipping
257302
# (e.g. because the user explicitly requested an incompatible target) we still

0 commit comments

Comments
 (0)