fix(core/build): fix direct cargo build for rust analyzer
What changed, and why it matters
This change is a build-system convenience fix. It lets the Rust code compile when run directly by a developer's tools (like rust-analyzer or plain cargo) by adding a fallback path for a missing environment variable. It does not change how the actual Trezor firmware is built or introduce any security-relevant behavior.
No security action needed. This is a benign build tooling fix.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit modifies core/embed/models/build.rs so that when the TREZOR_BOARD_HEADER environment variable is not set (which happens outside the normal xtask build flow), the build script falls back to reading the default board header from the model’s TOML files. Previously, the build would fail because std::env::var returned an error. The new code adds a default_board_header() helper that parses model.toml and the corresponding board TOML to determine the correct header. This is purely a developer-experience change for non-production build paths.
Changed components
core/embed/models/build.rsInspect captured patch +56 / −2
diff --git a/core/embed/models/build.rs b/core/embed/models/build.rs
index f0122196..45f5209c 100644
--- a/core/embed/models/build.rs
+++ b/core/embed/models/build.rs
@@ -23,9 +23,14 @@ fn main() -> Result<()> {
// Board header is resolved by xtask from the selected board's TOML and
// passed here so the correct revision header is used regardless of which
- // board revision is active.
+ // board revision is active. When building outside xtask (rust-analyzer or a
+ // bare `cargo` invocation) the variable is unset, so fall back to the
+ // model's default board header to keep the build working.
println!("cargo:rerun-if-env-changed=TREZOR_BOARD_HEADER");
- let board_header_path = std::env::var("TREZOR_BOARD_HEADER")?;
+ let board_header_path = match std::env::var("TREZOR_BOARD_HEADER") {
+ Ok(path) => path,
+ Err(_) => default_board_header(model_id)?,
+ };
let board_header = format!("\"{}\"", board_header_path);
xbuild::build(|lib| {
@@ -177,6 +182,55 @@ fn main() -> Result<()> {
})
}
+/// Default board header for a model, used as a fallback when xtask does not
+/// provide `TREZOR_BOARD_HEADER` (e.g. rust-analyzer or a bare `cargo` build).
+///
+/// Reads the model's `default_board` from `<model>/model.toml` and that board's
+/// `header` (or `emulator_header` for an emulator build) from
+/// `<model>/boards/<default_board>.toml`, so the tomls stay the single source of
+/// truth. Returns "" for an unselected model; that case is rejected later by the
+/// model dispatch in `main`.
+fn default_board_header(model_id: &str) -> Result<String> {
+ if model_id.is_empty() {
+ return Ok(String::new());
+ }
+
+ let model_dir = std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR")?).join(model_id);
+
+ let read_toml = |path: &std::path::Path| -> Result<toml::Value> {
+ println!("cargo:rerun-if-changed={}", path.display());
+ let content = std::fs::read_to_string(path)?;
+ Ok(toml::from_str(&content)?)
+ };
+
+ let model = read_toml(&model_dir.join("model.toml"))?;
+ let default_board = model
+ .get("default_board")
+ .and_then(toml::Value::as_str)
+ .ok_or_else(|| color_eyre::eyre::eyre!("{model_id}/model.toml missing 'default_board'"))?;
+
+ let board = read_toml(
+ &model_dir
+ .join("boards")
+ .join(format!("{default_board}.toml")),
+ )?;
+ // Mirror xtask: an emulator build uses the board's emulator configuration
+ // header instead of the on-hardware one.
+ let header_key = if cfg!(feature = "emulator") {
+ "emulator_header"
+ } else {
+ "header"
+ };
+ let header = board
+ .get(header_key)
+ .and_then(toml::Value::as_str)
+ .ok_or_else(|| {
+ color_eyre::eyre::eyre!("{model_id}/boards/{default_board}.toml missing '{header_key}'")
+ })?;
+
+ Ok(header.to_string())
+}
+
fn model_to_num(model: &str) -> u32 {
let model_bytes = model.as_bytes();
(model_bytes[3] as u32) << 24
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.