@@ -318,13 +318,13 @@ register_toolchains(
318
318
EOF
319
319
}
320
320
321
- function write_rule () {
321
+ function write_rules () {
322
322
local pkg=" ${1} "
323
323
mkdir -p " ${pkg} /rule"
324
324
cat > " ${pkg} /rule/rule.bzl" << EOF
325
325
load("//${pkg} /platform:platform.bzl", "ShowPlatformInfo", "describe_platform_info")
326
326
327
- def _sample_impl (ctx):
327
+ def _report (ctx):
328
328
toolchain = ctx.toolchains["//${pkg} /toolchain:toolchain_type"]
329
329
message = ctx.attr.message
330
330
exec_platform = describe_platform_info(ctx.attr._exec[ShowPlatformInfo])
@@ -336,6 +336,10 @@ def _sample_impl(ctx):
336
336
output = log,
337
337
content = str,
338
338
)
339
+ return log
340
+
341
+ def _sample_impl(ctx):
342
+ log = _report(ctx)
339
343
return [DefaultInfo(files = depset([log]))]
340
344
341
345
sample = rule(
@@ -353,6 +357,41 @@ sample = rule(
353
357
toolchains = ["//${pkg} /toolchain:toolchain_type"],
354
358
incompatible_use_toolchain_transition = True,
355
359
)
360
+
361
+ def _sample_test_impl(ctx):
362
+ log = _report(ctx)
363
+ # Write a fake test executable.
364
+ executable = ctx.actions.declare_file(ctx.label.name)
365
+ ctx.actions.write(
366
+ output = executable,
367
+ content = "\n".join([
368
+ "#!/usr/bin/env bash",
369
+ "exit 0",
370
+ ]),
371
+ is_executable = True,
372
+ )
373
+
374
+ return [DefaultInfo(
375
+ executable = executable,
376
+ files = depset([executable, log]),
377
+ )]
378
+
379
+ sample_test = rule(
380
+ implementation = _sample_test_impl,
381
+ attrs = {
382
+ "message": attr.string(),
383
+ "_exec": attr.label(
384
+ cfg = "exec",
385
+ providers = [ShowPlatformInfo],
386
+ default = Label("//${pkg} /platform")),
387
+ },
388
+ outputs = {
389
+ "log": "%{name}.log",
390
+ },
391
+ toolchains = ["//${pkg} /toolchain:toolchain_type"],
392
+ incompatible_use_toolchain_transition = True,
393
+ test = True,
394
+ )
356
395
EOF
357
396
cat > " ${pkg} /rule/BUILD" << EOF
358
397
package(default_visibility = ["//visibility:public"])
@@ -365,7 +404,7 @@ function test_toolchain_transition() {
365
404
write_constraints " ${pkg} "
366
405
write_platforms " ${pkg} "
367
406
write_toolchains " ${pkg} "
368
- write_rule " ${pkg} "
407
+ write_rules " ${pkg} "
369
408
370
409
mkdir -p " ${pkg} "
371
410
cat > " ${pkg} /BUILD" << EOF
398
437
expect_log ' extra_lib: message: extra_lib foo, target_dep: target, tool_dep: exec-alpha'
399
438
}
400
439
440
+ # Regression test for b/284495846.
441
+ # Test rules use the test trimming transition, which means that toolchain
442
+ # dependencies are in a different configuration that the actual parent target,
443
+ # which caused an issue where the execution platform was not correctly forwarded
444
+ # to the toolchain.
445
+ function test_toolchain_transition_test_rule() {
446
+ local -r pkg=" ${FUNCNAME[0]} "
447
+ write_constraints " ${pkg} "
448
+ write_platforms " ${pkg} "
449
+ write_toolchains " ${pkg} "
450
+ write_rules " ${pkg} "
451
+
452
+ mkdir -p " ${pkg} "
453
+ cat > " ${pkg} /BUILD" << EOF
454
+ package(default_visibility = ["//visibility:public"])
455
+
456
+ load("//${pkg} /rule:rule.bzl", "sample_test")
457
+
458
+ # Use a test rul to trigger b/284495846
459
+ sample_test(
460
+ name = "sample_test",
461
+ exec_compatible_with = [
462
+ "//${pkg} /constraint:beta",
463
+ ],
464
+ message = "Hello",
465
+ )
466
+ EOF
467
+
468
+ bazel build \
469
+ --platforms=" //${pkg} /platform:target" \
470
+ --host_platform=" //${pkg} /platform:host" \
471
+ --use_target_platform_for_tests \
472
+ " //${pkg} :sample_test" & > $TEST_log || fail " Build failed"
473
+
474
+ # Verify contents of sample_test.log.
475
+ cat " bazel-bin/${pkg} /sample_test.log" >> $TEST_log
476
+ # The execution platform should be beta.
477
+ expect_log ' rule message: "Hello", exec platform: "exec-beta"'
478
+ # The toolchain should have proper target and exec matching the top target.
479
+ expect_log ' sample_toolchain: message: beta toolchain, target_dep: target, tool_dep: exec-beta'
480
+ }
481
+
401
482
# Regression test for https://github.com/bazelbuild/bazel/issues/11993
402
483
# This was causing cquery to not correctly generate ConfiguredTargetKeys for
403
484
# toolchains, leading to the message "Targets were missing from graph"
@@ -406,7 +487,7 @@ function test_toolchain_transition_cquery() {
406
487
write_constraints " ${pkg} "
407
488
write_platforms " ${pkg} "
408
489
write_toolchains " ${pkg} "
409
- write_rule " ${pkg} "
490
+ write_rules " ${pkg} "
410
491
411
492
mkdir -p " ${pkg} "
412
493
cat > " ${pkg} /BUILD" << EOF
0 commit comments