Skip to content

crate_universe: resolve a non-root module's own labels to its repo - #4436

Open
TrapsterDK wants to merge 2 commits into
bazelbuild:mainfrom
TrapsterDK:crate-universe-module-labels
Open

TrapsterDK wants to merge 2 commits into
bazelbuild:mainfrom
TrapsterDK:crate-universe-module-labels

Conversation

@TrapsterDK

Copy link
Copy Markdown
Contributor

Fixes #4435.

A module pinned as the root records labels to its own targets from crate.annotation (override_target_lib, patches, ...) as @@//... in cargo-bazel-lock.json. The digest already ignores the repo name so that lockfile stays valid when the module is a non-root dependency. But the files rendered from it still said @@//..., which then means the root module's repo, so the build failed with no such target '//:foo'.

How it works:

  • crate_universe/extensions.bzl: for a non-root module, _generate_hub_and_spokes replaces @@// with @@<module repo>// in the hub files, the spoke BUILD files and the lockfile JSON it reads back (that last one covers patches passed to http_archive/git_repository and the Path crates). The canonical repo name comes from cfg.lockfile.repo_name; non-root modules must already set lockfile, and it lives in the module's own repo.
  • The rewrite is a plain textual replace of @@// across the whole lockfile content and the rendered files. That matches what @@// meant when the lockfile was pinned: the pinning module was the root, so @@// was its own repo. //... and @//... strings in string-list annotation attributes (deps, build_script_data, ...) are not rewritten; how those resolve is a separate, existing behaviour.
  • Root modules are unchanged. The lockfile format and digest don't change, so existing lockfiles don't need a repin, and it doesn't depend on + vs ~ in canonical names.

Test:

  • New integration fixture crate_universe/tests/integration/non_root_module_labels. It uses override_target as a non-root module and has a build_test on @cu_test_override_target//:override_targets_example, which goes through override_target_lib = "//:foo". override_targets_example is now public for that.
  • The override_target CI tasks also run bazel test //... @cu_test_override_target//:all @cu_test_override_target//overrides:all in the new fixture through post_shell_commands, since presubmit is at the 128 task limit. Running override_target's own tests from there also covers the proc-macro (paste) and custom-build (anyhow) overrides with the module as non-root.

bazel test //... @cu_test_override_target//:all @cu_test_override_target//overrides:all in non_root_module_labels fails without the change (no such target '//:foo' for foo, no such package 'overrides' for paste and the anyhow genquery) and passes with it. bazel test //... in override_target (root case) still passes. buildifier -mode=check -lint=warn is clean on the changed files.

This is separate from #4386, which also changes how _generate_hub_and_spokes handles non-root modules. The two touch nearby lines but not the same ones, and both add post_shell_commands, to different presubmit tasks.

Assisted by Claude.

A module pinned as the root records labels to its own targets from
`crate.annotation` (`override_target_lib`, `patches`, ...) as `@@//...`
in its lockfile. The digest ignores the repo name so the lockfile stays
valid when the module is a non-root dependency, but the files rendered
from it kept `@@//...`, which then names the root module's repo.

For non-root modules, `_generate_hub_and_spokes` now points `@@//` at
the module's canonical repo (taken from its `lockfile` label) in the hub
files, spoke BUILD files and lockfile JSON it reads back. Root modules,
the lockfile format and the digest are unchanged.

Test: new integration fixture `non_root_module_labels` uses
`override_target` as a non-root module. `bazel test //...
@cu_test_override_target//:all @cu_test_override_target//overrides:all`
there covers the `lib`, `proc-macro` and `custom-build` overrides. It
fails without the change (`no such target '//:foo'`, `no such package
'overrides'`) and passes with it. It runs from the `override_target` CI
tasks.

Assisted-by: Claude
… docstring

buildifier-lint (v8.5.1, --warnings=all) flagged the `@@//` examples in
the docstring. Suppress it the same way as the replace() call below it.

Ran: buildifier-lint --lint=warn --warnings=all and buildifier
--mode=check on the PR's .bzl/.bazel files; both clean.

Assisted-by: Claude
@TrapsterDK

Copy link
Copy Markdown
Contributor Author

The code-format-checks failure was real: buildifier-lint flagged canonical-repository at crate_universe/extensions.bzl:534, from the @@// examples in the _own_repo_labels docstring. Fixed in c1eca3f with # buildifier: disable=canonical-repository, the same way the replace() line below it is already handled.

Ran buildifier-lint v8.5.1 --lint=warn --warnings=all and buildifier --mode=check on the PR's .bzl/.bazel files. Both are clean now; lint failed with the same error before the change.

Assisted by Claude.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crate_universe: labels to a module's own targets point at the root module when it is used as a dependency

1 participant