From 19325d5027cf8b318cb1d829f18ef2d3b2fea707 Mon Sep 17 00:00:00 2001 From: "Y.D.X." <73375426+YDX-2147483647@users.noreply.github.com> Date: Wed, 11 Jun 2025 22:21:05 +0800 Subject: [PATCH] Warning when watching stdin (#6381) Co-authored-by: Laurenz --- crates/typst-cli/src/watch.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/typst-cli/src/watch.rs b/crates/typst-cli/src/watch.rs index 0813d8ffd..630d340b2 100644 --- a/crates/typst-cli/src/watch.rs +++ b/crates/typst-cli/src/watch.rs @@ -10,11 +10,12 @@ use codespan_reporting::term::{self, termcolor}; use ecow::eco_format; use notify::{Event, RecommendedWatcher, RecursiveMode, Watcher as _}; use same_file::is_same_file; -use typst::diag::{bail, StrResult}; +use typst::diag::{bail, warning, StrResult}; +use typst::syntax::Span; use typst::utils::format_duration; use crate::args::{Input, Output, WatchCommand}; -use crate::compile::{compile_once, CompileConfig}; +use crate::compile::{compile_once, print_diagnostics, CompileConfig}; use crate::timings::Timer; use crate::world::{SystemWorld, WorldCreationError}; use crate::{print_error, terminal}; @@ -55,6 +56,11 @@ pub fn watch(timer: &mut Timer, command: &WatchCommand) -> StrResult<()> { // Perform initial compilation. timer.record(&mut world, |world| compile_once(world, &mut config))??; + // Print warning when trying to watch stdin. + if matches!(&config.input, Input::Stdin) { + warn_watching_std(&world, &config)?; + } + // Recompile whenever something relevant happens. loop { // Watch all dependencies of the most recent compilation. @@ -332,3 +338,15 @@ impl Status { } } } + +/// Emits a warning when trying to watch stdin. +fn warn_watching_std(world: &SystemWorld, config: &CompileConfig) -> StrResult<()> { + let warning = warning!( + Span::detached(), + "cannot watch changes for stdin"; + hint: "to recompile on changes, watch a regular file instead"; + hint: "to compile once and exit, please use `typst compile` instead" + ); + print_diagnostics(world, &[], &[warning], config.diagnostic_format) + .map_err(|err| eco_format!("failed to print diagnostics ({err})")) +}