fix: correctly handle relative user-specified output paths in compile command (#1941)

This change ensures that user-specified output paths in the compile
command are properly handled, including relative paths.

Previously, the export task would reject relative output paths with an
error. This change converts relative paths to absolute paths by joining
them with the current working directory, allowing users to specify
relative paths like 'output/test.pdf' when compiling documents.

The fix adds path normalization using PathClean and handles the
conversion gracefully while maintaining the existing behavior for
absolute paths.

Fixes the issue where commands like:
  tinymist compile resume-en.typ ./output/test.pdf
would fail with "output path is relative" error.
This commit is contained in:
moeleak 2025-07-30 21:35:36 +08:00 committed by GitHub
parent 641b28fc45
commit 3821861134
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View file

@ -207,7 +207,7 @@ impl TaskCompileArgs {
let export = ExportTask {
when,
output: None,
output: self.output.as_deref().map(PathPattern::new),
transform: transforms,
};

View file

@ -10,6 +10,7 @@ use reflexo_typst::{Bytes, CompilationTask, ExportComputation};
use tinymist_project::LspWorld;
use tinymist_std::error::prelude::*;
use tinymist_std::fs::paths::write_atomic;
use tinymist_std::path::PathClean;
use tinymist_std::typst::TypstDocument;
use tinymist_task::{get_page_selection, ExportMarkdownTask, ExportTarget, PdfExport, TextExport};
use tokio::sync::mpsc;
@ -178,6 +179,12 @@ impl ExportTask {
let Some(write_to) = output.substitute(&entry) else {
return Ok(None);
};
let write_to = if write_to.is_relative() {
let cwd = std::env::current_dir().context("failed to get current directory")?;
cwd.join(write_to).clean()
} else {
write_to.to_path_buf()
};
if write_to.is_relative() {
bail!("ExportTask({task:?}): output path is relative: {write_to:?}");
}