Skip to content

Commit 27b4a5b

Browse files
authored
Treat fmtutil-sys failure as non-fatal during package install
quarto render --to pdf aborted with `ERROR: Problem running 'fmtutil-sys --all' to rebuild format tree.` whenever automatic LaTeX package installation triggered a post-update format-tree rebuild that exited non-zero. Format-tree rebuild is best-effort housekeeping unrelated to install success — match upstream tinytex R behavior, which discards the fmtutil exit code. - New `fmtutilFailureMessage` helper + unit tests; both installPackage recovery branches now log a warning and continue instead of rejecting - `fmtutilCommand` routed through `safeWindowsExec` on Windows (mirrors `tlmgrCommand`) for defense against runscript.tlu short-path failures (rstudio/tinytex#427) - Program path + args pre-quoted via `requireQuoting` per the documented `safeWindowsExec` spaces test (#13997) - New rule doc `.claude/rules/typescript/windows-safe-exec.md` capturing the requireQuoting-before-safeWindowsExec convention Fixes #14461
1 parent e768e5c commit 27b4a5b

4 files changed

Lines changed: 112 additions & 16 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
paths:
3+
- "src/**/*.ts"
4+
---
5+
6+
# Windows: `safeWindowsExec` quoting
7+
8+
`safeWindowsExec` (defined at `src/core/windows.ts`) is the right tool whenever a Windows process needs to be spawned through `cmd /c` instead of being launched directly by Deno. Examples include `.bat` wrappers (`tlmgr.bat`), TeX Live binaries that use `runscript.tlu` for path resolution (`fmtutil-sys.exe`, see `rstudio/tinytex#427`), registry queries via `reg.exe`, and other shell-sensitive callouts elsewhere under `src/core/` (`zip.ts`, `shell.ts`). Grep `safeWindowsExec` for the current set of call sites.
9+
10+
## Quote both program and args before calling
11+
12+
`safeWindowsExec` writes a temp `.bat` whose contents are `[program, ...args].join(" ")`. Any space in the program path or any arg tokenizes the line incorrectly, and the spawn either fails or runs the wrong binary. Pass the program path and the args through `requireQuoting` together, then split the returned array — the unit test `safeWindowsExec - handles program path with spaces (issue #13997)` in `tests/unit/windows-exec.test.ts` is the authoritative usage example, and `fmtutilCommand` in `src/command/render/latexmk/texlive.ts` is the in-tree consumer.
13+
14+
`tlmgrCommand` quotes only the user-supplied subcommand args, not the program path, because `tlmgr.bat` historically lives at a stable TinyTeX path. New call sites should default to quoting both — paths under `C:\Users\<name with spaces>\…` are common enough that this isn't theoretical.

‎news/changelog-1.10.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,5 @@ All changes included in 1.10:
7070
- ([#14342](https://github.com/quarto-dev/quarto-cli/issues/14342)): Work around TOCTOU race in Deno's `expandGlobSync` that can cause unexpected exceptions to be raised while traversing directories during project initialization.
7171
- ([#14445](https://github.com/quarto-dev/quarto-cli/issues/14445)): Fix intermittent `Uncaught (in promise) TypeError: Writable stream is closed or errored.` aborting renders on Linux. `execProcess` now awaits and swallows the rejection from `process.stdin.close()` when the child closes its stdin first. The captured stderr is now also surfaced when `typst-gather analyze` falls back to staging all packages, so failures are diagnosable without bypassing `quarto`.
7272
- ([#14359](https://github.com/quarto-dev/quarto-cli/issues/14359)): Fix intermediate `.quarto_ipynb` file not being deleted after rendering a `.qmd` with Jupyter engine, causing numbered variants (`_1`, `_2`, ...) to accumulate on disk across renders.
73+
- ([#14461](https://github.com/quarto-dev/quarto-cli/issues/14461)): Fix `quarto render --to pdf` aborting with `ERROR: Problem running 'fmtutil-sys --all' to rebuild format tree.` when an automatically-installed LaTeX package's post-update format rebuild fails. Format-tree rebuild is now treated as best-effort housekeeping (matching upstream `tinytex` R behavior) — the failure is logged as a warning and the package install completes.
7374
- ([#14472](https://github.com/quarto-dev/quarto-cli/issues/14472)): Add support for Kotlin in code annotations and YAML cell options. (author: @barendgehrels)

‎src/command/render/latexmk/texlive.ts‎

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import * as ld from "../../../core/lodash.ts";
77

88
import { execProcess } from "../../../core/process.ts";
9+
import { ProcessResult } from "../../../core/process-types.ts";
910
import { lines } from "../../../core/text.ts";
1011
import { requireQuoting, safeWindowsExec } from "../../../core/windows.ts";
1112
import { hasTinyTex, tinyTexBinDir } from "../../../tools/impl/tinytex-info.ts";
1213
import { join } from "../../../deno_ral/path.ts";
1314
import { logProgress } from "../../../core/log.ts";
1415
import { isWindows } from "../../../deno_ral/platform.ts";
16+
import { warning } from "../../../deno_ral/log.ts";
1517

1618
export interface TexLiveContext {
1719
preferTinyTex: boolean;
@@ -242,12 +244,12 @@ async function installPackage(
242244
return Promise.reject("Problem running `tlmgr update`.");
243245
}
244246

245-
// Rebuild format tree
247+
// Rebuild format tree (best-effort; failure is non-fatal — see
248+
// fmtutilFailureMessage doc).
246249
const fmtutilResult = await fmtutilCommand(context);
247-
if (fmtutilResult.code !== 0) {
248-
return Promise.reject(
249-
"Problem running `fmtutil-sys --all` to rebuild format tree.",
250-
);
250+
const fmtutilWarn = fmtutilFailureMessage(fmtutilResult);
251+
if (fmtutilWarn) {
252+
warning(fmtutilWarn);
251253
}
252254
}
253255

@@ -281,12 +283,12 @@ async function installPackage(
281283
return Promise.reject("Problem running `tlmgr update`.");
282284
}
283285

284-
// Rebuild format tree
286+
// Rebuild format tree (best-effort; failure is non-fatal — see
287+
// fmtutilFailureMessage doc).
285288
const fmtutilResult = await fmtutilCommand(context);
286-
if (fmtutilResult.code !== 0) {
287-
return Promise.reject(
288-
"Problem running `fmtutil-sys --all` to rebuild format tree.",
289-
);
289+
const fmtutilWarn = fmtutilFailureMessage(fmtutilResult);
290+
if (fmtutilWarn) {
291+
warning(fmtutilWarn);
290292
}
291293

292294
// Rerun the install command
@@ -453,16 +455,50 @@ function tlmgrCommand(
453455
}
454456
}
455457

458+
// Returns a warning message when `fmtutil-sys --all` failed, or `undefined`
459+
// when it succeeded. fmtutil failure is treated as non-fatal: package install
460+
// already succeeded by the time we reach the recovery branches in
461+
// `installPackage`, and the format-tree rebuild is best-effort housekeeping
462+
// to mitigate l3kernel version-mismatch issues (#7252).
463+
//
464+
// Upstream tinytex R package follows the same pattern (R/tlmgr.R discards
465+
// the `system2('fmtutil', ...)` exit code).
466+
export function fmtutilFailureMessage(
467+
result: ProcessResult,
468+
): string | undefined {
469+
if (result.code === 0) {
470+
return undefined;
471+
}
472+
const stderr = result.stderr?.trim() ?? "";
473+
const detail = stderr.length > 0 ? `\n${stderr}` : "";
474+
return `Failed to rebuild format tree (\`fmtutil-sys --all\` exited ${result.code}). This is non-fatal — package installation will continue.${detail}`;
475+
}
476+
456477
// Execute fmtutil
457478
// https://tug.org/texlive/doc/fmtutil.html
479+
//
480+
// On Windows, route through `safeWindowsExec` (mirrors `tlmgrCommand`).
481+
// This wraps the call in a temp `.bat` invoked via `cmd /c`, which
482+
// avoids 8.3 short-path resolution failures inside TeX Live's
483+
// `runscript.tlu` (rstudio/tinytex#427).
458484
function fmtutilCommand(context: TexLiveContext) {
459485
const fmtutil = texLiveCmd("fmtutil-sys", context);
460-
return execProcess(
461-
{
462-
cmd: fmtutil.fullPath,
463-
args: ["--all"],
486+
const execFmtutil = (cmd: string[]) => {
487+
return execProcess({
488+
cmd: cmd[0],
489+
args: cmd.slice(1),
464490
stdout: "piped",
465491
stderr: "piped",
466-
},
467-
);
492+
});
493+
};
494+
if (isWindows) {
495+
// Quote both program and args before handing them to `safeWindowsExec`
496+
// — `safeWindowsExec` joins them into a `.bat` line with a literal space
497+
// separator, so paths containing spaces (e.g. `C:\Users\Jane Doe\...`)
498+
// would otherwise be tokenized incorrectly. See issue #13997 and the
499+
// `safeWindowsExec - handles program path with spaces` unit test.
500+
const quoted = requireQuoting([fmtutil.fullPath, "--all"]);
501+
return safeWindowsExec(quoted.args[0], quoted.args.slice(1), execFmtutil);
502+
}
503+
return execFmtutil([fmtutil.fullPath, "--all"]);
468504
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* fmtutil-handler.test.ts
3+
*
4+
* Copyright (C) 2026 Posit Software, PBC
5+
*
6+
*/
7+
8+
import { fmtutilFailureMessage } from "../../../src/command/render/latexmk/texlive.ts";
9+
import { unitTest } from "../../test.ts";
10+
import { assert, assertEquals } from "testing/asserts";
11+
12+
// deno-lint-ignore require-await
13+
unitTest("fmtutilFailureMessage - undefined when fmtutil-sys succeeded", async () => {
14+
assertEquals(
15+
fmtutilFailureMessage({ code: 0, success: true, stdout: "", stderr: "" }),
16+
undefined,
17+
);
18+
});
19+
20+
// deno-lint-ignore require-await
21+
unitTest("fmtutilFailureMessage - warning text when exit code is non-zero", async () => {
22+
const msg = fmtutilFailureMessage({
23+
code: 1,
24+
success: false,
25+
stdout: "",
26+
stderr: "fmtutil: error rebuilding format luatex",
27+
});
28+
assert(msg, "expected a warning message");
29+
assert(msg.includes("fmtutil-sys --all"), "should mention the command");
30+
assert(msg.includes("non-fatal"), "should signal non-fatal");
31+
assert(msg.includes("fmtutil: error rebuilding format luatex"), "should include stderr");
32+
});
33+
34+
// deno-lint-ignore require-await
35+
unitTest("fmtutilFailureMessage - omits stderr suffix when stderr is empty/whitespace", async () => {
36+
const msg = fmtutilFailureMessage({
37+
code: 1,
38+
success: false,
39+
stdout: "",
40+
stderr: " \n",
41+
});
42+
const baseMsg =
43+
`Failed to rebuild format tree (\`fmtutil-sys --all\` exited 1). This is non-fatal — package installation will continue.`;
44+
assertEquals(msg, baseMsg);
45+
});

0 commit comments

Comments
 (0)