Skip to content

Commit 343ab03

Browse files
fmeumcopybara-github
authored andcommitted
Fix WORKSPACE toolchain resolution with --enable_bzlmod
The canonical repo name of the `platforms` module is now forced to be `platforms`, which ensures that `@platforms` constraints used by toolchains defined in `WORKSPACE` match those referenced by the auto-configured host platform provided by `local_config_platform`. Fixes #17289 Closes #18624. PiperOrigin-RevId: 539710874 Change-Id: I171f308b06e7ec7559641b49b4c8c53dddac0d3c
1 parent e262538 commit 343ab03

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleKey.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ public abstract class ModuleKey {
3939
"bazel_tools",
4040
RepositoryName.BAZEL_TOOLS,
4141
"local_config_platform",
42-
RepositoryName.createUnvalidated("local_config_platform"));
42+
RepositoryName.createUnvalidated("local_config_platform"),
43+
// Ensures that references to "@platforms" in WORKSPACE files resolve to the repository of
44+
// the "platforms" module. Without this, constraints on toolchains registered in WORKSPACE
45+
// would reference the "platforms" repository defined in the WORKSPACE suffix, whereas
46+
// the host constraints generated by local_config_platform would reference the "platforms"
47+
// module repository, resulting in a toolchain resolution mismatch.
48+
"platforms",
49+
RepositoryName.createUnvalidated("platforms"));
4350

4451
public static final ModuleKey ROOT = create("", Version.EMPTY);
4552

src/test/py/bazel/bzlmod/bazel_module_test.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,84 @@ def testNativeModuleNameAndVersion(self):
588588
self.assertIn('@@bar~override reporting in: [email protected]', stderr)
589589
self.assertIn('@@quux reporting in: None@None', stderr)
590590

591+
def testWorkspaceToolchainRegistrationWithPlatformsConstraint(self):
592+
"""Regression test for https://github.com/bazelbuild/bazel/issues/17289."""
593+
self.ScratchFile('MODULE.bazel')
594+
self.ScratchFile(
595+
'WORKSPACE', ['register_toolchains("//:my_toolchain_toolchain")']
596+
)
597+
os.remove(self.Path('WORKSPACE.bzlmod'))
598+
599+
self.ScratchFile(
600+
'BUILD.bazel',
601+
[
602+
'load(":defs.bzl", "get_host_os", "my_consumer", "my_toolchain")',
603+
'toolchain_type(name = "my_toolchain_type")',
604+
'my_toolchain(',
605+
' name = "my_toolchain",',
606+
' my_value = "Hello, Bzlmod!",',
607+
')',
608+
'toolchain(',
609+
' name = "my_toolchain_toolchain",',
610+
' toolchain = ":my_toolchain",',
611+
' toolchain_type = ":my_toolchain_type",',
612+
' target_compatible_with = [',
613+
' "@platforms//os:" + get_host_os(),',
614+
' ],',
615+
')',
616+
'my_consumer(',
617+
' name = "my_consumer",',
618+
')',
619+
],
620+
)
621+
622+
self.ScratchFile(
623+
'defs.bzl',
624+
[
625+
(
626+
'load("@local_config_platform//:constraints.bzl",'
627+
' "HOST_CONSTRAINTS")'
628+
),
629+
'def _my_toolchain_impl(ctx):',
630+
' return [',
631+
' platform_common.ToolchainInfo(',
632+
' my_value = ctx.attr.my_value,',
633+
' ),',
634+
' ]',
635+
'my_toolchain = rule(',
636+
' implementation = _my_toolchain_impl,',
637+
' attrs = {',
638+
' "my_value": attr.string(),',
639+
' },',
640+
')',
641+
'def _my_consumer(ctx):',
642+
' my_toolchain_info = ctx.toolchains["//:my_toolchain_type"]',
643+
' out = ctx.actions.declare_file(ctx.attr.name)',
644+
(
645+
' ctx.actions.write(out, "my_value ='
646+
' {}".format(my_toolchain_info.my_value))'
647+
),
648+
' return [DefaultInfo(files = depset([out]))]',
649+
'my_consumer = rule(',
650+
' implementation = _my_consumer,',
651+
' attrs = {},',
652+
' toolchains = ["//:my_toolchain_type"],',
653+
')',
654+
'def get_host_os():',
655+
' for constraint in HOST_CONSTRAINTS:',
656+
' if constraint.startswith("@platforms//os:"):',
657+
' return constraint.removeprefix("@platforms//os:")',
658+
],
659+
)
660+
661+
self.RunBazel([
662+
'build',
663+
'//:my_consumer',
664+
'--toolchain_resolution_debug=//:my_toolchain_type',
665+
])
666+
with open(self.Path('bazel-bin/my_consumer'), 'r') as f:
667+
self.assertEqual(f.read().strip(), 'my_value = Hello, Bzlmod!')
668+
591669

592670
if __name__ == '__main__':
593671
unittest.main()

0 commit comments

Comments
 (0)