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

[6.3.0] Fix WORKSPACE toolchain resolution with --enable_bzlmod #18649

Merged
merged 2 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
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
  • Loading branch information
fmeum authored and iancha1992 committed Jun 12, 2023
commit dd02d438d55b66243b12fc7515e07792ae5ea6f4
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ public abstract class ModuleKey {
"bazel_tools",
RepositoryName.BAZEL_TOOLS,
"local_config_platform",
RepositoryName.createUnvalidated("local_config_platform"));
RepositoryName.createUnvalidated("local_config_platform"),
// Ensures that references to "@platforms" in WORKSPACE files resolve to the repository of
// the "platforms" module. Without this, constraints on toolchains registered in WORKSPACE
// would reference the "platforms" repository defined in the WORKSPACE suffix, whereas
// the host constraints generated by local_config_platform would reference the "platforms"
// module repository, resulting in a toolchain resolution mismatch.
"platforms",
RepositoryName.createUnvalidated("platforms"));

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

Expand Down
78 changes: 78 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,84 @@ def testNativeModuleNameAndVersion(self):
self.assertIn('@@bar~override reporting in: bar@2.0', stderr)
self.assertIn('@@quux reporting in: None@None', stderr)

def testWorkspaceToolchainRegistrationWithPlatformsConstraint(self):
"""Regression test for https://github.com/bazelbuild/bazel/issues/17289."""
self.ScratchFile('MODULE.bazel')
self.ScratchFile(
'WORKSPACE', ['register_toolchains("//:my_toolchain_toolchain")']
)
os.remove(self.Path('WORKSPACE.bzlmod'))

self.ScratchFile(
'BUILD.bazel',
[
'load(":defs.bzl", "get_host_os", "my_consumer", "my_toolchain")',
'toolchain_type(name = "my_toolchain_type")',
'my_toolchain(',
' name = "my_toolchain",',
' my_value = "Hello, Bzlmod!",',
')',
'toolchain(',
' name = "my_toolchain_toolchain",',
' toolchain = ":my_toolchain",',
' toolchain_type = ":my_toolchain_type",',
' target_compatible_with = [',
' "@platforms//os:" + get_host_os(),',
' ],',
')',
'my_consumer(',
' name = "my_consumer",',
')',
],
)

self.ScratchFile(
'defs.bzl',
[
(
'load("@local_config_platform//:constraints.bzl",'
' "HOST_CONSTRAINTS")'
),
'def _my_toolchain_impl(ctx):',
' return [',
' platform_common.ToolchainInfo(',
' my_value = ctx.attr.my_value,',
' ),',
' ]',
'my_toolchain = rule(',
' implementation = _my_toolchain_impl,',
' attrs = {',
' "my_value": attr.string(),',
' },',
')',
'def _my_consumer(ctx):',
' my_toolchain_info = ctx.toolchains["//:my_toolchain_type"]',
' out = ctx.actions.declare_file(ctx.attr.name)',
(
' ctx.actions.write(out, "my_value ='
' {}".format(my_toolchain_info.my_value))'
),
' return [DefaultInfo(files = depset([out]))]',
'my_consumer = rule(',
' implementation = _my_consumer,',
' attrs = {},',
' toolchains = ["//:my_toolchain_type"],',
')',
'def get_host_os():',
' for constraint in HOST_CONSTRAINTS:',
' if constraint.startswith("@platforms//os:"):',
' return constraint.removeprefix("@platforms//os:")',
],
)

self.RunBazel([
'build',
'//:my_consumer',
'--toolchain_resolution_debug=//:my_toolchain_type',
])
with open(self.Path('bazel-bin/my_consumer'), 'r') as f:
self.assertEqual(f.read().strip(), 'my_value = Hello, Bzlmod!')


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