cargo_build_script incompatible with rust_toolchain using target_json
メンテナーはふだん 3 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 65/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- rust
- 領域
- build-system, tooling
調査の方向性
The issue is in cargo_build_script.bzl and cargo_build_script_runner/bin.rs. Start by examining the cargo_build_script rule implementation around line 468 and the runner's get_target_env_vars function. The fix involves checking if target_json is set and passing the JSON file path instead of the target triple, and adding -Zunstable-options when the target ends with .json. Run the provided reproduction steps to verify the fix works with a custom JSON target.
索引モデルが issue の本文から書いたものです。
説明
Description
I am trying to use a tier-3 language, and defining the platform as a platform JSON.
It seems cargo_build_script cannot be used with a rust_toolchain that has target_json due to a type mismatch between literal and filename.
- Current: analysis fails with
Error in run: got dict<string, File> for 'env', want dict<string, string>. - Expected: build works with JSON targets.
Reproduction steps
MODULE.bazel:
module(name = "repro")
bazel_dep(name = "rules_rust", version = "0.74.0")
bazel_dep(name = "platforms", version = "1.1.0")
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(edition = "2021", versions = ["nightly/2026-09-21"])
use_repo(rust, "rust_toolchains", "rust_linux_x86_64__x86_64-unknown-linux-gnu__nightly_tools")
register_toolchains("//:json_toolchain", "@rust_toolchains//:all")
BUILD.bazel:
load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
load("@rules_rust//rust:defs.bzl", "rust_binary")
load("@rules_rust//rust:toolchain.bzl", "rust_toolchain")
load(":target.bzl", "TARGET_JSON")
constraint_setting(name = "spec")
constraint_value(name = "json_spec", constraint_setting = ":spec")
platform(name = "json_platform", constraint_values = ["@platforms//cpu:x86_64", "@platforms//os:linux", ":json_spec"])
TOOLS = "@rust_linux_x86_64__x86_64-unknown-linux-gnu__nightly_tools"
rust_toolchain(
name = "json_rust_toolchain",
binary_ext = "", dylib_ext = ".so", staticlib_ext = ".a",
exec_triple = "x86_64-unknown-linux-gnu",
extra_rustc_flags = ["-Zunstable-options"],
rust_doc = TOOLS + "//:rustdoc",
rust_std = TOOLS + "//:rust_std-x86_64-unknown-linux-gnu",
rustc = TOOLS + "//:rustc",
rustc_lib = TOOLS + "//:rustc_lib",
stdlib_linkflags = ["-ldl", "-lpthread"],
target_json = TARGET_JSON,
)
toolchain(
name = "json_toolchain",
exec_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:linux"],
target_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:linux", ":json_spec"],
toolchain = ":json_rust_toolchain",
toolchain_type = "@rules_rust//rust:toolchain_type",
)
cargo_build_script(name = "bs", srcs = ["build.rs"], edition = "2021")
rust_binary(name = "bin", srcs = ["main.rs"], edition = "2021", deps = [":bs"])
build.rs:
fn main() { println!("cargo:rustc-cfg=from_build_script"); }
main.rs:
fn main() { }
Trigger:
(echo 'TARGET_JSON = """'; rustc -Zunstable-options --print target-spec-json --target x86_64-unknown-linux-gnu; echo '"""') > target.bzl
bazel build //:bs --platforms=//:json_platform --@rules_rust//rust/toolchain/channel=nightly
Additional context
ERROR: /home/bluecmd/work/rust-repro/BUILD.bazel:30:19: in cargo_build_script rule //:bs:
Traceback (most recent call last):
File "/home/bluecmd/.cache/bazel/_bazel_bluecmd/ea14b543b17791a382a3d5f7ed953007/external/rules_rust+/cargo/private/cargo_build_script.bzl", line 703, column 20, in _cargo_build_script_impl
ctx.actions.run(
Error in run: got dict<string, File> for 'env', want dict<string, string>
ERROR: /home/bluecmd/work/rust-repro/BUILD.bazel:30:19: Analysis of target '//:bs' (config: 63aac55) failed
ERROR: Analysis of target '//:bin' failed; build aborted: Analysis failed
INFO: Elapsed time: 33.434s, Critical Path: 0.06s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
Something like this might work?
Adding -Zunstable-options since the error without seems to want it; error loading target specification: custom targets are unstable and require -Zunstable-options".
--- a/cargo/private/cargo_build_script.bzl
+++ b/cargo/private/cargo_build_script.bzl
@@ -468,7 +468,7 @@
"RUSTDOC": toolchain.rust_doc.path,
- "TARGET": toolchain.target_flag_value,
+ "TARGET": toolchain.target_json.path if toolchain.target_json else toolchain.target_flag_value,
--- a/cargo/private/cargo_build_script_runner/bin.rs
+++ b/cargo/private/cargo_build_script_runner/bin.rs
@@ -505,12 +505,14 @@
fn get_target_env_vars<P: AsRef<Path>>(rustc: &P) -> Result<BTreeMap<String, String>, String> {
- let output = Command::new(rustc.as_ref())
- .arg("--print=cfg")
- .arg(format!(
- "--target={}",
- env::var("TARGET").expect("missing TARGET")
- ))
+ let target = env::var("TARGET").expect("missing TARGET");
+ let mut command = Command::new(rustc.as_ref());
+ command.arg("--print=cfg").arg(format!("--target={target}"));
+ if target.ends_with(".json") {
+ command.arg("-Zunstable-options");
+ }
+ let output = command
.output()
Impact
Blocks every crate with a build.rs on custom JSON targets.
Bazel and rules_rust version
Bazel 9.2.0, rules_rust 0.74.0
- 主要言語
- Starlark
- スター
- 857
- フォーク
- 701
- 平均マージ
- 4日 7時間
- マージ済み PR(30日)
- 46
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
bazelbuild/rules_rust のほかの issue
-
`rust_test(crate = ...)` does not stage the crate's transitive dynamic libraries into runfiles対応中かも @Wito-1 が 65 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
bazelbuild/rules_rust#4309 ·
メンテナーはふだん 3 日以内に返信
-
Windows (no-symlink path): drain_runfiles_dir_windows deletes retained runfiles — inverted filename_suffixes_to_retain condition対応中かも @Mattbusel が 17 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
bazelbuild/rules_rust#4266 ·
メンテナーはふだん 3 日以内に返信
-
rust.toolchain(strip_level = ...) is silently ignored対応中かも @Mattbusel が 17 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
bazelbuild/rules_rust#4223 ·
メンテナーはふだん 3 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 90/100
bazelbuild/rules_rust#4152 · リアクション 1 件 ·
メンテナーはふだん 3 日以内に返信
-
awaiting-response
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
bazelbuild/rules_rust#3599 · コメント 1 件 ·
メンテナーはふだん 3 日以内に返信
bazelbuild/rules_rust の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
bazelbuild/apple_support#707 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
-
out-of-date
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
CachyOS/CachyOS-PKGBUILDS#2232 ·
メンテナーはふだん 1 日以内に返信
-
blast/large refactor risk/low spend-dem
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
chriskelly/MusicWall#87 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
neilotoole/sq#1503 ·
メンテナーはふだん 1 日以内に返信