Skip to content

Bug: crates.from_specs doesn't work with lockfile #3783

@thmsbinder

Description

@thmsbinder

Bug: crates.from_specs doesn't work with lockfile

Summary

When using crates.from_specs with the lockfile option, building a simple Rust snippet with a dependency on a third-party crate crashes with the following message:

ERROR: /home/tbinder/.cache/bazel/_bazel_tbinder/410c413e36a4f7e76b03d66dd30eac3e/external/rules_rust+/crate_universe/private/common_utils.bzl:55:36: Traceback (most recent call last):
	File "/home/tbinder/.cache/bazel/_bazel_tbinder/410c413e36a4f7e76b03d66dd30eac3e/external/rules_rust+/crate_universe/extensions.bzl", line 1034, column 37, in _crate_impl
		_generate_hub_and_spokes(
	File "/home/tbinder/.cache/bazel/_bazel_tbinder/410c413e36a4f7e76b03d66dd30eac3e/external/rules_rust+/crate_universe/extensions.bzl", line 674, column 22, in _generate_hub_and_spokes
		execute_generator(
	File "/home/tbinder/.cache/bazel/_bazel_tbinder/410c413e36a4f7e76b03d66dd30eac3e/external/rules_rust+/crate_universe/private/generate_utils.bzl", line 512, column 28, in execute_generator
		result = cargo_bazel_fn(
	File "/home/tbinder/.cache/bazel/_bazel_tbinder/410c413e36a4f7e76b03d66dd30eac3e/external/rules_rust+/crate_universe/private/common_utils.bzl", line 94, column 23, in _execute
		return execute(
	File "/home/tbinder/.cache/bazel/_bazel_tbinder/410c413e36a4f7e76b03d66dd30eac3e/external/rules_rust+/crate_universe/private/common_utils.bzl", line 55, column 36, in execute
		result = repository_ctx.execute(
Error in execute: Argument 3 of execute is neither a path, label, nor string.
ERROR: Analysis of target '//:hello' failed; build aborted: error evaluating module extension @@rules_rust+//crate_universe:extensions.bzl%crate
INFO: Elapsed time: 0.796s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
FAILED: 
    Fetching module extension @@rules_rust+//crate_universe:extensions.bzl%crate; Generating crate BUILD files for `crates`

Without the lockfile line in MODULE.bazel, it works fine. The crash persisted when trying random different versions of the ingredients. A direct workaround is not known - perhaps using cargo_lockfile instead of lockfile is one.

Steps to reproduce

Install the following four files in an empty directory:

  • .bazelversion - contains "8.4.1"
  • BUILD - as below
  • MODULE.bazel - as below
  • hello.rs - as below

Then run the following, where the build line crashes:

CARGO_BAZEL_REPIN=1 bazelisk build --nobuild @crates//:all
bazelisk build :hello

BUILD

load("@rules_rust//rust:defs.bzl", "rust_binary")

rust_binary(
    name = "hello",
    srcs = ["hello.rs"],
    deps = ["@crates//:log"],
)

hello.rs

use log::debug;

fn main() {
  let greeting: &str = "Hello world!";
  debug!("{}", greeting);
  println!("{}", greeting);
}

MODULE.bazel

module(name = "rules_rust_test")

# Register the C++ toolchain.
bazel_dep(name = "toolchains_llvm", version = "1.4.0")
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
llvm.toolchain(
    llvm_version = "17.0.6",
)
use_repo(llvm, "llvm_toolchain")
register_toolchains("@llvm_toolchain//:all")

# https://github.com/bazelbuild/rules_rust
bazel_dep(name = "rules_rust", version = "0.65.0")

# Register the Rust toolchain.
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
    edition = "2021",
    versions = ["1.83.0"],
)
use_repo(rust, "rust_toolchains")
register_toolchains("@rust_toolchains//:all")

crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
# https://docs.rs/log/0.4.29/log/index.html
crate.spec(
    package = "log",
    version = "0.4.28",
)
crate.from_specs(
    # To update: CARGO_BAZEL_REPIN=1 bazelisk build --nobuild @crates//:all
    lockfile = "//:cargo-bazel-lock.json",
)
use_repo(crate, "crates")

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageThe ticket needs maintainer attention.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions