cargo_build_script incompatible with rust_toolchain using target_json

オープン
#4,268 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 3 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
65/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust

調査の方向性

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 の本文から書いたものです。

説明

needs-triage

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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

bazelbuild/rules_rust のほかの issue

bazelbuild/rules_rust の issue をすべて見る

似ている issue

Build System の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。