Skip to content

Commit 9a65c8a

Browse files
committed
Fix relative paths for renderer commands.
1 parent a64a7b7 commit 9a65c8a

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed

src/renderer/mod.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod markdown_renderer;
2020
use shlex::Shlex;
2121
use std::fs;
2222
use std::io::{self, ErrorKind, Read};
23-
use std::path::PathBuf;
23+
use std::path::{Path, PathBuf};
2424
use std::process::{Command, Stdio};
2525

2626
use crate::book::Book;
@@ -133,14 +133,44 @@ impl CmdRenderer {
133133
CmdRenderer { name, cmd }
134134
}
135135

136-
fn compose_command(&self) -> Result<Command> {
136+
fn compose_command(&self, root: &Path, destination: &Path) -> Result<Command> {
137137
let mut words = Shlex::new(&self.cmd);
138-
let executable = match words.next() {
139-
Some(e) => e,
138+
let exe = match words.next() {
139+
Some(e) => PathBuf::from(e),
140140
None => bail!("Command string was empty"),
141141
};
142142

143-
let mut cmd = Command::new(executable);
143+
let exe = if exe.components().count() == 1 {
144+
// Search PATH for the executable.
145+
exe
146+
} else {
147+
// Relative paths are preferred to be relative to the book root.
148+
let abs_exe = root.join(&exe);
149+
if abs_exe.exists() {
150+
abs_exe
151+
} else {
152+
// Historically paths were relative to the destination, but
153+
// this is not the preferred way.
154+
let legacy_path = destination.join(&exe);
155+
if legacy_path.exists() {
156+
warn!(
157+
"Renderer command `{}` uses a path relative to the \
158+
renderer output directory `{}`. This was previously \
159+
accepted, but has been deprecated. Relative executable \
160+
paths should be relative to the book root.",
161+
exe.display(),
162+
destination.display()
163+
);
164+
legacy_path
165+
} else {
166+
// Let this bubble through to later be handled by
167+
// handle_render_command_error.
168+
abs_exe.to_path_buf()
169+
}
170+
}
171+
};
172+
173+
let mut cmd = Command::new(exe);
144174

145175
for arg in words {
146176
cmd.arg(arg);
@@ -195,7 +225,7 @@ impl Renderer for CmdRenderer {
195225
let _ = fs::create_dir_all(&ctx.destination);
196226

197227
let mut child = match self
198-
.compose_command()?
228+
.compose_command(&ctx.root, &ctx.destination)?
199229
.stdin(Stdio::piped())
200230
.stdout(Stdio::inherit())
201231
.stderr(Stdio::inherit())

tests/alternative_backends.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use mdbook::config::Config;
44
use mdbook::MDBook;
5-
#[cfg(not(windows))]
5+
use std::fs;
66
use std::path::Path;
77
use tempfile::{Builder as TempFileBuilder, TempDir};
88

@@ -71,6 +71,45 @@ fn backends_receive_render_context_via_stdin() {
7171
assert!(got.is_ok());
7272
}
7373

74+
#[test]
75+
fn relative_command_path() {
76+
// Checks behavior of relative paths for the `command` setting.
77+
let temp = TempFileBuilder::new().prefix("mdbook").tempdir().unwrap();
78+
let renderers = temp.path().join("renderers");
79+
fs::create_dir(&renderers).unwrap();
80+
rust_exe(
81+
&renderers,
82+
"myrenderer",
83+
r#"fn main() {
84+
std::fs::write("output", "test").unwrap();
85+
}"#,
86+
);
87+
let do_test = |cmd_path| {
88+
let mut config = Config::default();
89+
config
90+
.set("output.html", toml::value::Table::new())
91+
.unwrap();
92+
config.set("output.myrenderer.command", cmd_path).unwrap();
93+
let md = MDBook::init(&temp.path())
94+
.with_config(config)
95+
.build()
96+
.unwrap();
97+
let output = temp.path().join("book/myrenderer/output");
98+
assert!(!output.exists());
99+
md.build().unwrap();
100+
assert!(output.exists());
101+
fs::remove_file(output).unwrap();
102+
};
103+
// Legacy paths work, relative to the output directory.
104+
if cfg!(windows) {
105+
do_test("../../renderers/myrenderer.exe");
106+
} else {
107+
do_test("../../renderers/myrenderer");
108+
}
109+
// Modern path, relative to the book directory.
110+
do_test("renderers/myrenderer");
111+
}
112+
74113
fn dummy_book_with_backend(
75114
name: &str,
76115
command: &str,
@@ -112,3 +151,14 @@ fn success_cmd() -> &'static str {
112151
"true"
113152
}
114153
}
154+
155+
fn rust_exe(temp: &Path, name: &str, src: &str) {
156+
let rs = temp.join(name).with_extension("rs");
157+
fs::write(&rs, src).unwrap();
158+
let status = std::process::Command::new("rustc")
159+
.arg(rs)
160+
.current_dir(temp)
161+
.status()
162+
.expect("rustc should run");
163+
assert!(status.success());
164+
}

0 commit comments

Comments
 (0)