Skip to content

Commit 1782b5b

Browse files
authored
Fix quarto.cmd altering arguments that contain ! or ^ (#15365)
quarto.cmd forwarded its arguments with %* on a line that used delayed expansion, so cmd rescanned them: a "!" was dropped or expanded as a variable name, and "^" was stripped. Rendering, previewing or running a file whose path contains "!" failed with the dev build and with any caller that starts quarto.cmd by name, such as a terminal or IDE extension. The Deno command is now built with delayed expansion on and run through FOR /F with delayed expansion off, so %* reaches Deno unchanged and option variables stay literal even when they contain "&" or "^". - Launch Deno under a nested SETLOCAL, which keeps the variables the script set - Compare "%~1" in the --version, -v and --paths shortcuts; Deno quotes the first argument, so "%1" never matched - Add a Windows-only test that runs both layouts with metacharacters in the arguments and option values An install or checkout path containing "!" or "^" still breaks the launcher; that case is left for a separate change.
1 parent 5f06c8d commit 1782b5b

3 files changed

Lines changed: 232 additions & 10 deletions

File tree

‎dev-docs/runtime-migration/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ The rows below are the ones that still need a named person or an external answer
155155
| PR 08 Task 1 and Task 2 step 2 (from #15341, the `execProcess` UTF-8 fix on `main`) | The plan's premise that `src/deno_ral/process.ts` is 10 lines (context section, `08-ral-process.md:25`) is stale: `execPath(): string` already exists there with the planned signature. Task 2 step 2 imports `pooledMap` (`:933`) and its `processOutput` snippet (`:1047-1066`) restores the per-chunk body that decodes each chunk twice, which would revert the fix; applied as written over the current body, which already prints through its own helper, it would also print inherited output twice. Re-derive the snippet from the current `processOutput` (decode once with `{ stream: true }`, flush at the end, skip empty text) and make `writeStdoutSync`/`writeStderrSync` the only change; drop `pooledMap` from the import as `filteredAsyncIterator` no longer uses it. Add `unit/exec-process-utf8-chunks.test.ts` to Task 2's verification | PR 08 executor | PR 08 step 0 |
156156
| PR 28 Task 4 step 3 (from #15341) | the merged-mode `addStream` snippet (`28-ral-process-on-node-apis.md:894-900`) wraps only filtered streams (`filter ? filteredAsyncIterator(iterator, filter) : iterator`), which drops the per-stream decoding that keeps a character split across chunks whole when both streams are merged. Keep `filteredAsyncIterator(iterator, filter)` on both streams, and add `unit/exec-process-utf8-chunks.test.ts` to the PR's gate | PR 28 executor | PR 28 step 0 |
157157
| PRs 23 and 31 (from #15341) | `tests/unit/exec-process-utf8-chunks.test.ts` starts its child as `execPath() eval '<script>'`, a Deno-only invocation (the `eval` subcommand; the script body itself uses only `node:fs`). Add it to PR 23's runtime re-invocation inventory (`23-codemod-tests.md:42-43`) so PR 31's `runtimeScriptCommand` converts it | PR 23 executor | PR 23 step 0 |
158+
| PR 33 Task 9 Step 4 (from #15365, the `quarto.cmd` argument fix on `main`) | The full `quarto.cmd` listing (`33-build-tooling-node.md:2111-2207`) predates three launcher fixes on `main` and would revert them. Its launch line and `QUARTO_LAUNCHER_DEBUG` line put `%*` on a line that uses `!VAR!` under `ENABLEDELAYEDEXPANSION`, so arguments containing `!` or `^` are altered; `main` builds the command with `!VAR!`, carries it through `FOR /F`, and runs it on an unparenthesized `DO SETLOCAL DISABLEDELAYEDEXPANSION & %%C %*`. Its `IF "%1"==` shortcuts never match when the caller quotes the argument, as Deno does for every batch-file argument; `main` compares `"%~1"`. Its `call !QUARTO_ROOT!\win_configuration.bat` drops the quotes #15276 added. Re-derive the Windows launcher from current `main` instead of pasting the listing, and keep `tests/unit/launcher-arguments.test.ts`, `dev-launcher-path-with-space.test.ts` and `bundled-launcher-path-with-space.test.ts` green. | PR 33 executor | PR 33 step 0 |
158159

159160
## Shared rules and tables
160161

‎package/scripts/windows/quarto.cmd‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ SET "QUARTO_LOCK_OPTIONS=--no-lock"
1414
IF EXIST "!QUARTO_TS_PATH!" (
1515
set QUARTO_DEV_MODE=true
1616

17-
IF "%1"=="--version" (
17+
IF "%~1"=="--version" (
1818
IF DEFINED QUARTO_FORCE_VERSION (
1919
ECHO !QUARTO_FORCE_VERSION!
2020
GOTO end
@@ -23,7 +23,7 @@ IF EXIST "!QUARTO_TS_PATH!" (
2323
GOTO end
2424
)
2525

26-
IF "%1"=="-v" (
26+
IF "%~1"=="-v" (
2727
IF DEFINED QUARTO_FORCE_VERSION (
2828
ECHO !QUARTO_FORCE_VERSION!
2929
GOTO end
@@ -84,7 +84,7 @@ IF EXIST "!QUARTO_TS_PATH!" (
8484
for %%i in ("!SCRIPT_PATH!\..\share") do SET "QUARTO_SHARE_PATH=%%~fi"
8585
)
8686

87-
IF "%1"=="-v" (
87+
IF "%~1"=="-v" (
8888
IF DEFINED QUARTO_FORCE_VERSION (
8989
ECHO !QUARTO_FORCE_VERSION!
9090
GOTO end
@@ -93,7 +93,7 @@ IF EXIST "!QUARTO_TS_PATH!" (
9393
GOTO end
9494
)
9595

96-
IF "%1"=="--version" (
96+
IF "%~1"=="--version" (
9797
IF DEFINED QUARTO_FORCE_VERSION (
9898
ECHO !QUARTO_FORCE_VERSION!
9999
GOTO end
@@ -103,13 +103,14 @@ IF EXIST "!QUARTO_TS_PATH!" (
103103
)
104104

105105
SET QUARTO_DENO_OPTIONS=--no-check
106+
SET QUARTO_IMPORT_MAP_ARG=
106107
SET QUARTO_ACTION=run
107108
SET "QUARTO_TARGET=%SCRIPT_PATH%\quarto.js"
108109
SET "QUARTO_BIN_PATH=%SCRIPT_PATH%"
109110
SET QUARTO_CACHE_OPTIONS=--cached-only
110111
)
111112

112-
IF "%1"=="--paths" (
113+
IF "%~1"=="--paths" (
113114
ECHO !QUARTO_BIN_PATH!
114115
ECHO !QUARTO_SHARE_PATH!
115116
GOTO end
@@ -167,11 +168,13 @@ IF DEFINED QUARTO_TS_PROFILE (
167168
SET "QUARTO_TS_PROFILE=true"
168169
)
169170

170-
IF DEFINED QUARTO_DEV_MODE (
171-
"!QUARTO_DENO!" !QUARTO_ACTION! !QUARTO_CACHE_OPTIONS! !QUARTO_DENO_OPTIONS! !QUARTO_DENO_EXTRA_OPTIONS! !QUARTO_IMPORT_MAP_ARG! "!QUARTO_TARGET!" %*
172-
) ELSE (
173-
"!QUARTO_DENO!" !QUARTO_ACTION! !QUARTO_CACHE_OPTIONS! !QUARTO_DENO_OPTIONS! !QUARTO_DENO_EXTRA_OPTIONS! "!QUARTO_TARGET!" %*
174-
)
171+
REM The command is built with !VAR!, so cmd does not interpret & or ^ in option values,
172+
REM and runs with delayed expansion off: %* is substituted before delayed expansion,
173+
REM which would consume any "!" or "^" in the arguments. %%C is substituted after cmd
174+
REM parses the line, which keeps the built command literal. No parentheses around the
175+
REM DO command: a ")" in an unquoted argument would close them.
176+
SET "QUARTO_DENO_CMD="!QUARTO_DENO!" !QUARTO_ACTION! !QUARTO_CACHE_OPTIONS! !QUARTO_DENO_OPTIONS! !QUARTO_DENO_EXTRA_OPTIONS! !QUARTO_IMPORT_MAP_ARG! "!QUARTO_TARGET!""
177+
FOR /F "delims=" %%C IN ("!QUARTO_DENO_CMD!") DO SETLOCAL DISABLEDELAYEDEXPANSION & %%C %*
175178

176179

177180
:end
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
* launcher-arguments.test.ts
3+
*
4+
* The Windows launcher quarto.cmd must pass its arguments to Deno unchanged,
5+
* including "!" and "^", which delayed expansion would otherwise consume, and
6+
* must short-circuit --version, -v and --paths when the caller quotes them.
7+
* Covered for both the dev layout (package/dist/bin/quarto.cmd) and the
8+
* bundled layout (bin/quarto.cmd next to quarto.js).
9+
*
10+
* Copyright (C) 2026 Posit Software, PBC
11+
*/
12+
13+
import { unitTest } from "../test.ts";
14+
import { assert, assertEquals } from "../asserts.ts";
15+
import { isWindows } from "../../src/deno_ral/platform.ts";
16+
import { dirname, fromFileUrl, join } from "../../src/deno_ral/path.ts";
17+
import {
18+
copySync,
19+
ensureDirSync,
20+
removeSync,
21+
writeTextFileSync,
22+
} from "../../src/deno_ral/fs.ts";
23+
import { execProcess } from "../../src/core/process.ts";
24+
import { withTempDir } from "../utils.ts";
25+
26+
const repoRoot = join(dirname(fromFileUrl(import.meta.url)), "..", "..");
27+
28+
// Prints the arguments and, when Deno got a --location option, its URL.
29+
const probe = 'import process from "node:process";\n' +
30+
'console.log("ARGS=" + JSON.stringify(process.argv.slice(2)));\n' +
31+
'let loc = "";\n' +
32+
"try { loc = globalThis.location.href; } catch { /* no --location */ }\n" +
33+
'console.log("LOC=" + loc);\n';
34+
35+
// A Deno option whose value holds characters cmd would otherwise interpret.
36+
const location = "http://example.com/?a=1&b=^c!d";
37+
38+
const specialArgs = [
39+
"C:\\work\\notes!",
40+
"C:\\x!OS!y",
41+
"c^d",
42+
"a & b",
43+
"100%",
44+
"%OS%",
45+
"p (1)",
46+
"sp ace",
47+
];
48+
49+
interface Launcher {
50+
cmd: string;
51+
env: Record<string, string>;
52+
}
53+
54+
async function launch(launcher: Launcher, args: string[], cwd: string) {
55+
const result = await execProcess({
56+
cmd: launcher.cmd,
57+
args,
58+
cwd,
59+
stdout: "piped",
60+
stderr: "piped",
61+
env: launcher.env,
62+
});
63+
const stdout = result.stdout ?? "";
64+
const details = `(exit ${result.code})\nstdout:\n${stdout}\nstderr:\n${
65+
result.stderr ?? ""
66+
}`;
67+
return { result, stdout, details };
68+
}
69+
70+
async function assertArgumentsReachDeno(
71+
launcher: Launcher,
72+
args: string[],
73+
cwd: string,
74+
) {
75+
const { result, stdout, details } = await launch(launcher, args, cwd);
76+
const argsLine = stdout.split(/\r?\n/).find((line) =>
77+
line.startsWith("ARGS=")
78+
);
79+
assertEquals(
80+
argsLine,
81+
"ARGS=" + JSON.stringify(args),
82+
`quarto.cmd changed its arguments ${details}`,
83+
);
84+
assert(result.success, `quarto.cmd exited with an error ${details}`);
85+
}
86+
87+
// Runs quarto.cmd through cmd with an unquoted command line, as typed in a
88+
// terminal, where cmd itself sees the ")" in "report(1).qmd".
89+
async function assertTypedArgumentsReachDeno(launcher: Launcher, cwd: string) {
90+
const result = await execProcess({
91+
cmd: "cmd",
92+
args: ["/d", "/s", "/c", `""${launcher.cmd}" run report(1).qmd C:\\notes!"`],
93+
windowsRawArguments: true,
94+
cwd,
95+
stdout: "piped",
96+
stderr: "piped",
97+
env: launcher.env,
98+
});
99+
const stdout = result.stdout ?? "";
100+
const details = `(exit ${result.code})\nstdout:\n${stdout}\nstderr:\n${
101+
result.stderr ?? ""
102+
}`;
103+
const argsLine = stdout.split(/\r?\n/).find((line) =>
104+
line.startsWith("ARGS=")
105+
);
106+
assertEquals(
107+
argsLine,
108+
'ARGS=["run","report(1).qmd","C:\\\\notes!"]',
109+
`quarto.cmd changed a typed command line ${details}`,
110+
);
111+
assert(result.success, `quarto.cmd exited with an error ${details}`);
112+
}
113+
114+
async function assertOptionsReachDeno(launcher: Launcher, cwd: string) {
115+
const { result, stdout, details } = await launch({
116+
cmd: launcher.cmd,
117+
env: {
118+
...launcher.env,
119+
QUARTO_DENO_EXTRA_OPTIONS: `--location=${location}`,
120+
},
121+
}, ["run"], cwd);
122+
const locLine = stdout.split(/\r?\n/).find((line) => line.startsWith("LOC="));
123+
assertEquals(
124+
locLine,
125+
`LOC=${location}`,
126+
`quarto.cmd changed QUARTO_DENO_EXTRA_OPTIONS ${details}`,
127+
);
128+
assert(result.success, `quarto.cmd exited with an error ${details}`);
129+
}
130+
131+
async function assertShortCircuits(launcher: Launcher, cwd: string) {
132+
for (const flag of ["--version", "-v"]) {
133+
const { stdout, details } = await launch(launcher, [flag], cwd);
134+
assertEquals(
135+
stdout.trim(),
136+
"9.8.7",
137+
`quarto.cmd ${flag} did not print the forced version ${details}`,
138+
);
139+
}
140+
const { stdout, details } = await launch(launcher, ["--paths"], cwd);
141+
assert(
142+
!stdout.includes("ARGS=") && stdout.trim().split(/\r?\n/).length === 2,
143+
`quarto.cmd --paths did not print the bin and share paths ${details}`,
144+
);
145+
}
146+
147+
unitTest(
148+
"launcher-arguments - bundled quarto.cmd passes arguments unchanged",
149+
async () => {
150+
await withTempDir(async (dir) => {
151+
// Minimal bundled install layout, as in
152+
// bundled-launcher-path-with-space.test.ts.
153+
const bin = join(dir, "bin");
154+
ensureDirSync(bin);
155+
copySync(
156+
join(repoRoot, "package", "scripts", "windows", "quarto.cmd"),
157+
join(bin, "quarto.cmd"),
158+
);
159+
writeTextFileSync(join(bin, "quarto.js"), probe);
160+
const tools = join(bin, "tools");
161+
const mklink = await execProcess({
162+
cmd: "cmd",
163+
args: [
164+
"/c",
165+
"mklink",
166+
"/J",
167+
tools,
168+
join(repoRoot, "package", "dist", "bin", "tools"),
169+
],
170+
stdout: "piped",
171+
stderr: "piped",
172+
});
173+
assert(mklink.success, `creating the junction failed: ${mklink.stderr}`);
174+
try {
175+
const launcher = {
176+
cmd: join(bin, "quarto.cmd"),
177+
env: { QUARTO_FORCE_VERSION: "9.8.7" },
178+
};
179+
await assertArgumentsReachDeno(launcher, ["run", ...specialArgs], dir);
180+
await assertArgumentsReachDeno(launcher, ["a & b", "x"], dir);
181+
await assertOptionsReachDeno(launcher, dir);
182+
await assertTypedArgumentsReachDeno(launcher, dir);
183+
await assertShortCircuits(launcher, dir);
184+
} finally {
185+
// Remove the junction itself (not its target) before the temp dir goes.
186+
removeSync(tools);
187+
}
188+
});
189+
},
190+
{ ignore: !isWindows },
191+
);
192+
193+
unitTest(
194+
"launcher-arguments - dev quarto.cmd passes arguments unchanged",
195+
async () => {
196+
await withTempDir(async (dir) => {
197+
const target = join(dir, "probe.ts");
198+
writeTextFileSync(target, probe);
199+
const launcher = {
200+
cmd: join(repoRoot, "package", "dist", "bin", "quarto.cmd"),
201+
env: {
202+
QUARTO_TARGET: target,
203+
QUARTO_NO_TYPECHECK: "true",
204+
QUARTO_FORCE_VERSION: "9.8.7",
205+
// See dev-launcher-path-with-space.test.ts: keeps a failed launch
206+
// from running configure.cmd in the checkout under test.
207+
NoDefaultCurrentDirectoryInExePath: "1",
208+
},
209+
};
210+
await assertArgumentsReachDeno(launcher, ["run", ...specialArgs], dir);
211+
await assertArgumentsReachDeno(launcher, ["a & b", "x"], dir);
212+
await assertOptionsReachDeno(launcher, dir);
213+
await assertTypedArgumentsReachDeno(launcher, dir);
214+
await assertShortCircuits(launcher, dir);
215+
});
216+
},
217+
{ ignore: !isWindows, requiresDevQuarto: true },
218+
);

0 commit comments

Comments
 (0)