uudoc: move tldr.zip warning to build.rs (#10039)

Move the tldr.zip missing warning from runtime (uudoc.rs) to compile
time (build.rs). This ensures the warning is printed only once during
the build, instead of 100+ times when make calls uudoc separately for
each utility.

Fixes #9940
This commit is contained in:
Ruiyang Wang
2026-01-18 09:54:50 -08:00
committed by GitHub
parent 3c55820f5e
commit eb482fb2ee
3 changed files with 25 additions and 26 deletions
+13 -1
View File
@@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (vars) krate mangen
// spell-checker:ignore (vars) krate mangen tldr
use std::env;
use std::fs::File;
@@ -19,6 +19,18 @@ pub fn main() {
// See <https://doc.rust-lang.org/cargo/reference/build-scripts.html#change-detection>
println!("cargo:rerun-if-changed=build.rs");
// Check for tldr.zip when building uudoc to warn users once at build time
// instead of repeatedly at runtime for each utility
if env::var("CARGO_FEATURE_UUDOC").is_ok() && !Path::new("docs/tldr.zip").exists() {
println!(
"cargo:warning=No tldr archive found, so the documentation will not include examples."
);
println!("cargo:warning=To include examples, download the tldr archive:");
println!(
"cargo:warning= curl -L https://github.com/tldr-pages/tldr/releases/latest/download/tldr.zip -o docs/tldr.zip"
);
}
if let Ok(profile) = env::var("PROFILE") {
println!("cargo:rustc-cfg=build={profile:?}");
}
-19
View File
@@ -133,19 +133,6 @@ fn gen_completions<T: Args>(args: impl Iterator<Item = OsString>, util_map: &Uti
process::exit(0);
}
/// print tldr error
fn print_tldr_error() {
eprintln!("Warning: No tldr archive found, so the documentation will not include examples.");
eprintln!(
"To include examples in the documentation, download the tldr archive and put it in the docs/ folder."
);
eprintln!();
eprintln!(
" curl -L https://github.com/tldr-pages/tldr/releases/latest/download/tldr.zip -o docs/tldr.zip"
);
eprintln!();
}
/// # Errors
/// Returns an error if the writer fails.
#[allow(clippy::too_many_lines)]
@@ -162,9 +149,6 @@ fn main() -> io::Result<()> {
match command {
"manpage" => {
let args_iter = args.into_iter().skip(2);
if tldr_zip.is_none() {
print_tldr_error();
}
gen_manpage(
&mut tldr_zip,
args_iter,
@@ -186,9 +170,6 @@ fn main() -> io::Result<()> {
}
}
}
if tldr_zip.is_none() {
print_tldr_error();
}
let utils = util_map::<Box<dyn Iterator<Item = OsString>>>();
match std::fs::create_dir("docs/src/utils/") {
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(()),
+12 -6
View File
@@ -28,9 +28,11 @@ fn test_manpage_generation() {
"Command failed with status: {}",
output.status
);
// Note: tldr warning is now printed at build time (in build.rs), not at runtime
assert!(
String::from_utf8_lossy(&output.stderr).contains("Warning: No tldr archive found"),
"stderr should contains tldr alert",
output.stderr.is_empty(),
"stderr should be empty but got: {}",
String::from_utf8_lossy(&output.stderr)
);
let output_str = String::from_utf8_lossy(&output.stdout);
@@ -52,9 +54,11 @@ fn test_manpage_coreutils() {
"Command failed with status: {}",
output.status
);
// Note: tldr warning is now printed at build time (in build.rs), not at runtime
assert!(
String::from_utf8_lossy(&output.stderr).contains("Warning: No tldr archive found"),
"stderr should contains tldr alert",
output.stderr.is_empty(),
"stderr should be empty but got: {}",
String::from_utf8_lossy(&output.stderr)
);
let output_str = String::from_utf8_lossy(&output.stdout);
@@ -123,9 +127,11 @@ fn test_manpage_base64() {
"Command failed with status: {}",
output.status
);
// Note: tldr warning is now printed at build time (in build.rs), not at runtime
assert!(
String::from_utf8_lossy(&output.stderr).contains("Warning: No tldr archive found"),
"stderr should contains tldr alert",
output.stderr.is_empty(),
"stderr should be empty but got: {}",
String::from_utf8_lossy(&output.stderr)
);
let output_str = String::from_utf8_lossy(&output.stdout);