http_archive : depend on arch broken in 7.2.0
Description of the bug:
in 7.1.2 possible to use http_archive with url depend on current os/arch in 7.2.0rc1 it fails with error
ERROR: Failed to load Starlark extension '@@host_platform//:constraints.bzl'.
Cycle in the workspace file detected. This indicates that a repository is used prior to being defined.
The following chain of repository dependencies lead to the missing definition.
- @@host_platform
This could either mean you have to add the '@@host_platform' repository with a statement like `http_archive` in your WORKSPACE file (note that transitive dependencies are not added automatically), or move an existing definition earlier in your WORKSPACE file.
ERROR: Error computing the main repository mapping: cycles detected during computation of main repo mapping
How to fix this?
Which category does this issue belong to?
Core
What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
==> BUILD.bazel <==
==> lib.bzl <==
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
def get_host_cpu():
cpu_constraint = "@platforms//cpu:"
for constraint in HOST_CONSTRAINTS:
if constraint.startswith(cpu_constraint):
return constraint[len(cpu_constraint):]
return
def get_host_os():
cpu_constraint = "@platforms//os:"
for constraint in HOST_CONSTRAINTS:
if constraint.startswith(cpu_constraint):
return constraint[len(cpu_constraint):]
return
def get_host_target():
return get_host_os() + "-" + get_host_cpu()
def packages():
http_archive(
name = "test",
url = "https://some.host/test/" + get_host_target(),
)
==> MODULE.bazel <==
==> WORKSPACE.bazel <==
load("lib.bzl", "packages")
packages()
Which operating system are you running Bazel on?
linux
What is the output of bazel info release?
7.2.0rc1
If bazel info release returns development version or (@non-git), tell us how you built Bazel.
No response
What's the output of git remote get-url origin; git rev-parse HEAD ?
No response
Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.
maybe https://github.com/bazelbuild/platforms/pull/86
Have you found anything relevant by searching the web?
No response
Any other information, logs, or outputs that you want to share?
No response
@bazel-io fork 7.2.0
This is actually unearthing a broader problem -- when we load a .bzl file during WORKSPACE evaluation, we always use the repo mapping of the appropriate WORKSPACE chunk (composed with the root module's mappings). This is the case even when the .bzl file being loaded comes from a module with its own correct mappings etc.
I'm a bit surprised that we never ran into this until now, although I suppose nobody was loading .bzls from Bzlmod-ified modules in their WORKSPACE files.
The fix will probably involve some form of change in BzlLoadFunction#getRepositoryMapping, although I haven't thought through backwards-compatibility and all that yet...
@proller in the mean time, if you need to work around this, you could wrap your packages() macro in a module extension and use that instead (see https://github.com/bazelbuild/bazel-skylib/blob/main/docs/modules_doc.md#modulesas_extension)
At this point, we probably won't dedicate any more development time towards fixing WORKSPACE issues. You should be able to work around this issue in particular by using a module extension instead (see above comment). Closing.