From a245f8ce61740646230c123b5005cfc636bfa344 Mon Sep 17 00:00:00 2001 From: llama Date: Wed, 17 Dec 2025 00:38:18 +0800 Subject: [PATCH] fix(build): treat proto/i18n's implicit outputs as inputs (#4439) * fix(build): make proto/i18n's implicit outputs explicit * use option_env! instead of rerun-if-env-changed > As of 1.46, using env! and option_env! in source code will automatically detect changes and trigger rebuilds. https://doc.rust-lang.org/cargo/reference/build-scripts.html#rerun-if-env-changed * ditto for STRINGS_PY and STRINGS_TS * fix comment * remove space --- rslib/i18n/build.rs | 4 +--- rslib/i18n/python.rs | 3 +-- rslib/i18n/typescript.rs | 3 +-- rslib/io/src/lib.rs | 9 +++++++++ 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/rslib/i18n/build.rs b/rslib/i18n/build.rs index f604c9167..75bc38787 100644 --- a/rslib/i18n/build.rs +++ b/rslib/i18n/build.rs @@ -8,7 +8,6 @@ mod python; mod typescript; mod write_strings; -use std::env; use std::path::PathBuf; use anki_io::create_dir_all; @@ -32,8 +31,7 @@ fn main() -> Result<()> { python::write_py_interface(&modules)?; // write strings.json file to requested path - println!("cargo:rerun-if-env-changed=STRINGS_JSON"); - if let Ok(path) = env::var("STRINGS_JSON") { + if let Some(path) = option_env!("STRINGS_JSON") { if !path.is_empty() { let path = PathBuf::from(path); let meta_json = serde_json::to_string_pretty(&modules).unwrap(); diff --git a/rslib/i18n/python.rs b/rslib/i18n/python.rs index ca780c041..a564de48d 100644 --- a/rslib/i18n/python.rs +++ b/rslib/i18n/python.rs @@ -1,7 +1,6 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -use std::env; use std::fmt::Write; use std::path::PathBuf; @@ -21,7 +20,7 @@ pub fn write_py_interface(modules: &[Module]) -> Result<()> { render_methods(modules, &mut out); render_legacy_enum(modules, &mut out); - if let Ok(path) = env::var("STRINGS_PY") { + if let Some(path) = option_env!("STRINGS_PY") { let path = PathBuf::from(path); create_dir_all(path.parent().unwrap())?; write_file_if_changed(path, out)?; diff --git a/rslib/i18n/typescript.rs b/rslib/i18n/typescript.rs index ce30048e2..0f483cb58 100644 --- a/rslib/i18n/typescript.rs +++ b/rslib/i18n/typescript.rs @@ -1,7 +1,6 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -use std::env; use std::fmt::Write; use std::path::PathBuf; @@ -22,7 +21,7 @@ pub fn write_ts_interface(modules: &[Module]) -> Result<()> { render_module_map(modules, &mut ts_out); render_methods(modules, &mut ts_out); - if let Ok(path) = env::var("STRINGS_TS") { + if let Some(path) = option_env!("STRINGS_TS") { let path = PathBuf::from(path); create_dir_all(path.parent().unwrap())?; write_file_if_changed(path, ts_out)?; diff --git a/rslib/io/src/lib.rs b/rslib/io/src/lib.rs index cb44467e6..0fd85e490 100644 --- a/rslib/io/src/lib.rs +++ b/rslib/io/src/lib.rs @@ -335,6 +335,15 @@ pub fn write_file_if_changed(path: impl AsRef, contents: impl AsRef<[u8]>) .map(|existing| existing != contents) .unwrap_or(true) }; + + match std::env::var("CARGO_PKG_NAME") { + Ok(pkg) if pkg == "anki_proto" || pkg == "anki_i18n" => { + // at comptime for the proto/i18n crates, register implicit output as input + println!("cargo:rerun-if-changed={}", path.to_str().unwrap()); + } + _ => {} + } + if changed { write_file(path, contents)?; Ok(true)