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

Bug: crates_vendor is generating a broken defs.bzl #3255

Open
redsun82 opened this issue Feb 11, 2025 · 1 comment
Open

Bug: crates_vendor is generating a broken defs.bzl #3255

redsun82 opened this issue Feb 11, 2025 · 1 comment

Comments

@redsun82
Copy link

redsun82 commented Feb 11, 2025

While upgrading to 0.57.1 (or 0.57.0) we encountered a problem where crates_vendor is generating a defs.bzl where the labels are broken. For example instead of generating

"anyhow": Label("@vendor__anyhow-1.0.44//:anyhow"),

we are getting instead

"anyhow": Label("@vendor//:anyhow-1.0.44")

This then results in an error like

ERROR: no such package '@@[unknown repo 'vendor' requested from @@]//'

Initially I thought something was off with the default label template that can be provided via render_config, but I quickly found out that changing the crate_label_template there has no effect.

A reproducer can be found here (malformed content here and here). We're running bazel 8.0.0.

For the time being I'm about to work around this via this python script

import sys
import re
import pathlib

label_re = re.compile(r'"@vendor//:(.+)-([\d.]+)"')

file = pathlib.Path(sys.argv[1])
temp = file.with_suffix(f'{file.suffix}.tmp')

with open(file) as input, open(temp, "w") as output:
    for line in input:
        line = label_re.sub(lambda m: f'"@vendor__{m[1]}-{m[2]}//:{m[1].replace("-", "_")}"', line)
        output.write(line)

temp.rename(file)
@UebelAndre
Copy link
Collaborator

Ah, I think this is because the remote_name repository is only defined when loading the content from crates.bzl

###############################################################################
# @generated
# This file is auto-generated by the cargo-bazel tool.
#
# DO NOT MODIFY: Local changes may be replaced in future executions.
###############################################################################
"""Rules for defining repositories for remote `crates_vendor` repositories"""
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
# buildifier: disable=bzl-visibility
load("@rules_rust//crate_universe/private:crates_vendor.bzl", "crates_vendor_remote_repository")
# buildifier: disable=bzl-visibility
load("{{ crates_module_label(file="defs.bzl") }}", _crate_repositories = "crate_repositories")
def crate_repositories():
"""Generates repositories for vendored crates.
Returns:
A list of repos visible to the module through the module extension.
"""
maybe(
crates_vendor_remote_repository,
name = "{{ repository_name }}",
build_file = Label("{{ crates_module_label(file="BUILD.bazel") }}"),
defs_module = Label("{{ crates_module_label(file="defs.bzl") }}"),
)
direct_deps = [struct(repo = "{{ repository_name }}", is_dev_dep = False)]
direct_deps.extend(_crate_repositories())
return direct_deps

This should probably just be deleted at this point. The idea was to separate the all_crate_deps api from just accessing the URLs but the way it's currently setup is not helpful as crates.bzl loads from defs.bzl and thus you load all the wild starlark code.

Maybe in an ideal world crates.bzl would contain all the repo definitions and defs.bzl would be just the all_crate_deps interfaces but for simplicity (as there have been zero complaints about starlark analysis time) just consolidating would be good. Could I get someone to open a PR for this? It should be pretty straight forward to simply move the content of crates.bzl into defs.bzl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants