From 770a30a8a3f3010b3a722aa4e0b1bb6b422d80d7 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 13 Nov 2026 17:26:04 +0100 Subject: [PATCH 1/4] Pass quarto.cmd arguments to Deno unchanged on Windows quarto.cmd enables delayed expansion and forwarded %* on a line that used !VAR!. %* is substituted before delayed expansion runs, so the arguments were rescanned: an unmatched "!" was dropped, text between two "!" (even across separate arguments) was expanded as a variable name, and "^" was stripped. Rendering, previewing or running a file whose path contains "!" failed with the dev build, and any caller that starts quarto.cmd by name (terminal, IDE extensions) was affected. quarto.exe passes its arguments to Deno directly and is not. - Launch Deno under SETLOCAL DISABLEDELAYEDEXPANSION with %VAR%. The nested SETLOCAL keeps every variable the script set, which ENDLOCAL & would have dropped. One launch line now serves the dev and bundled layouts; the bundled branch clears QUARTO_IMPORT_MAP_ARG so an inherited value cannot reach Deno. - Compare "%~1" in the --version, -v and --paths short-circuits. Deno quotes every argument it passes to a batch file, so "%1" became ""--version"" and never matched, and a first argument containing & crashed the script. - Add a Windows-only test that runs both layouts with arguments containing ! ^ & % () and spaces, a first argument containing &, and the three short-circuits. An install or checkout path containing "!" or "^" still breaks the launcher; that path goes through delayed expansion before any argument is used, and is left for a separate change. --- package/scripts/windows/quarto.cmd | 20 ++-- tests/unit/launcher-arguments.test.ts | 163 ++++++++++++++++++++++++++ 2 files changed, 173 insertions(+), 10 deletions(-) create mode 100644 tests/unit/launcher-arguments.test.ts diff --git a/package/scripts/windows/quarto.cmd b/package/scripts/windows/quarto.cmd index 6fe4a1942d0..7f6dffd41bd 100644 --- a/package/scripts/windows/quarto.cmd +++ b/package/scripts/windows/quarto.cmd @@ -14,7 +14,7 @@ SET "QUARTO_LOCK_OPTIONS=--no-lock" IF EXIST "!QUARTO_TS_PATH!" ( set QUARTO_DEV_MODE=true - IF "%1"=="--version" ( + IF "%~1"=="--version" ( IF DEFINED QUARTO_FORCE_VERSION ( ECHO !QUARTO_FORCE_VERSION! GOTO end @@ -23,7 +23,7 @@ IF EXIST "!QUARTO_TS_PATH!" ( GOTO end ) - IF "%1"=="-v" ( + IF "%~1"=="-v" ( IF DEFINED QUARTO_FORCE_VERSION ( ECHO !QUARTO_FORCE_VERSION! GOTO end @@ -84,7 +84,7 @@ IF EXIST "!QUARTO_TS_PATH!" ( for %%i in ("!SCRIPT_PATH!\..\share") do SET "QUARTO_SHARE_PATH=%%~fi" ) - IF "%1"=="-v" ( + IF "%~1"=="-v" ( IF DEFINED QUARTO_FORCE_VERSION ( ECHO !QUARTO_FORCE_VERSION! GOTO end @@ -93,7 +93,7 @@ IF EXIST "!QUARTO_TS_PATH!" ( GOTO end ) - IF "%1"=="--version" ( + IF "%~1"=="--version" ( IF DEFINED QUARTO_FORCE_VERSION ( ECHO !QUARTO_FORCE_VERSION! GOTO end @@ -103,13 +103,14 @@ IF EXIST "!QUARTO_TS_PATH!" ( ) SET QUARTO_DENO_OPTIONS=--no-check + SET QUARTO_IMPORT_MAP_ARG= SET QUARTO_ACTION=run SET "QUARTO_TARGET=%SCRIPT_PATH%\quarto.js" SET "QUARTO_BIN_PATH=%SCRIPT_PATH%" SET QUARTO_CACHE_OPTIONS=--cached-only ) -IF "%1"=="--paths" ( +IF "%~1"=="--paths" ( ECHO !QUARTO_BIN_PATH! ECHO !QUARTO_SHARE_PATH! GOTO end @@ -167,11 +168,10 @@ IF DEFINED QUARTO_TS_PROFILE ( SET "QUARTO_TS_PROFILE=true" ) -IF DEFINED QUARTO_DEV_MODE ( - "!QUARTO_DENO!" !QUARTO_ACTION! !QUARTO_CACHE_OPTIONS! !QUARTO_DENO_OPTIONS! !QUARTO_DENO_EXTRA_OPTIONS! !QUARTO_IMPORT_MAP_ARG! "!QUARTO_TARGET!" %* -) ELSE ( - "!QUARTO_DENO!" !QUARTO_ACTION! !QUARTO_CACHE_OPTIONS! !QUARTO_DENO_OPTIONS! !QUARTO_DENO_EXTRA_OPTIONS! "!QUARTO_TARGET!" %* -) +REM Launch with delayed expansion off: %* is substituted before delayed expansion +REM runs, so on a line using !VAR! a "!" or "^" in the arguments would be consumed. +SETLOCAL DISABLEDELAYEDEXPANSION +"%QUARTO_DENO%" %QUARTO_ACTION% %QUARTO_CACHE_OPTIONS% %QUARTO_DENO_OPTIONS% %QUARTO_DENO_EXTRA_OPTIONS% %QUARTO_IMPORT_MAP_ARG% "%QUARTO_TARGET%" %* :end \ No newline at end of file diff --git a/tests/unit/launcher-arguments.test.ts b/tests/unit/launcher-arguments.test.ts new file mode 100644 index 00000000000..52657e7cba0 --- /dev/null +++ b/tests/unit/launcher-arguments.test.ts @@ -0,0 +1,163 @@ +/* + * launcher-arguments.test.ts + * + * The Windows launcher quarto.cmd must pass its arguments to Deno unchanged, + * including "!" and "^", which delayed expansion would otherwise consume, and + * must short-circuit --version, -v and --paths when the caller quotes them. + * Covered for both the dev layout (package/dist/bin/quarto.cmd) and the + * bundled layout (bin/quarto.cmd next to quarto.js). + * + * Copyright (C) 2026 Posit Software, PBC + */ + +import { unitTest } from "../test.ts"; +import { assert, assertEquals } from "../asserts.ts"; +import { isWindows } from "../../src/deno_ral/platform.ts"; +import { dirname, fromFileUrl, join } from "../../src/deno_ral/path.ts"; +import { + copySync, + ensureDirSync, + removeSync, + writeTextFileSync, +} from "../../src/deno_ral/fs.ts"; +import { execProcess } from "../../src/core/process.ts"; +import { withTempDir } from "../utils.ts"; + +const repoRoot = join(dirname(fromFileUrl(import.meta.url)), "..", ".."); + +const probe = 'import process from "node:process";\n' + + 'console.log("ARGS=" + JSON.stringify(process.argv.slice(2)));\n'; + +const specialArgs = [ + "C:\\work\\notes!", + "C:\\x!OS!y", + "c^d", + "a & b", + "100%", + "%OS%", + "p (1)", + "sp ace", +]; + +interface Launcher { + cmd: string; + env: Record; +} + +async function launch(launcher: Launcher, args: string[], cwd: string) { + const result = await execProcess({ + cmd: launcher.cmd, + args, + cwd, + stdout: "piped", + stderr: "piped", + env: launcher.env, + }); + const stdout = result.stdout ?? ""; + const details = `(exit ${result.code})\nstdout:\n${stdout}\nstderr:\n${ + result.stderr ?? "" + }`; + return { result, stdout, details }; +} + +async function assertArgumentsReachDeno( + launcher: Launcher, + args: string[], + cwd: string, +) { + const { result, stdout, details } = await launch(launcher, args, cwd); + const argsLine = stdout.split(/\r?\n/).find((line) => + line.startsWith("ARGS=") + ); + assertEquals( + argsLine, + "ARGS=" + JSON.stringify(args), + `quarto.cmd changed its arguments ${details}`, + ); + assert(result.success, `quarto.cmd exited with an error ${details}`); +} + +async function assertShortCircuits(launcher: Launcher, cwd: string) { + for (const flag of ["--version", "-v"]) { + const { stdout, details } = await launch(launcher, [flag], cwd); + assertEquals( + stdout.trim(), + "9.8.7", + `quarto.cmd ${flag} did not print the forced version ${details}`, + ); + } + const { stdout, details } = await launch(launcher, ["--paths"], cwd); + assert( + !stdout.includes("ARGS=") && stdout.trim().split(/\r?\n/).length === 2, + `quarto.cmd --paths did not print the bin and share paths ${details}`, + ); +} + +unitTest( + "launcher-arguments - bundled quarto.cmd passes arguments unchanged", + async () => { + await withTempDir(async (dir) => { + // Minimal bundled install layout, as in + // bundled-launcher-path-with-space.test.ts. + const bin = join(dir, "bin"); + ensureDirSync(bin); + copySync( + join(repoRoot, "package", "scripts", "windows", "quarto.cmd"), + join(bin, "quarto.cmd"), + ); + writeTextFileSync(join(bin, "quarto.js"), probe); + const tools = join(bin, "tools"); + const mklink = await execProcess({ + cmd: "cmd", + args: [ + "/c", + "mklink", + "/J", + tools, + join(repoRoot, "package", "dist", "bin", "tools"), + ], + stdout: "piped", + stderr: "piped", + }); + assert(mklink.success, `creating the junction failed: ${mklink.stderr}`); + try { + const launcher = { + cmd: join(bin, "quarto.cmd"), + env: { QUARTO_FORCE_VERSION: "9.8.7" }, + }; + await assertArgumentsReachDeno(launcher, ["run", ...specialArgs], dir); + await assertArgumentsReachDeno(launcher, ["a & b", "x"], dir); + await assertShortCircuits(launcher, dir); + } finally { + // Remove the junction itself (not its target) before the temp dir goes. + removeSync(tools); + } + }); + }, + { ignore: !isWindows }, +); + +unitTest( + "launcher-arguments - dev quarto.cmd passes arguments unchanged", + async () => { + await withTempDir(async (dir) => { + const target = join(dir, "probe.ts"); + writeTextFileSync(target, probe); + const launcher = { + cmd: join(repoRoot, "package", "dist", "bin", "quarto.cmd"), + env: { + QUARTO_TARGET: target, + QUARTO_NO_TYPECHECK: "true", + QUARTO_FORCE_VERSION: "9.8.7", + // See dev-launcher-path-with-space.test.ts: keeps a failed launch + // from running configure.cmd in the checkout under test. + NoDefaultCurrentDirectoryInExePath: "1", + }, + }; + await assertArgumentsReachDeno(launcher, ["run", ...specialArgs], dir); + await assertArgumentsReachDeno(launcher, ["a & b", "x"], dir); + await assertShortCircuits(launcher, dir); + }); + }, + { ignore: !isWindows, requiresDevQuarto: true }, +); From 3031ad7f8bc5eec0cacdd4d524e329b4d63588ed Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 13 Nov 2026 17:43:02 +0100 Subject: [PATCH 2/4] Keep cmd metacharacters in quarto.cmd option variables literal Expanding the option variables with %VAR% on the launch line let cmd parse their values: QUARTO_DENO_EXTRA_OPTIONS=--location=http://x/?a=1&b=2 split the Deno command at the "&", and an unquoted "^" was consumed. !VAR! had inserted these values after cmd parsed the line. Build the Deno command with !VAR! instead, carry it through a FOR /F variable, which cmd also substitutes after parsing, and run it with delayed expansion off so %* still reaches Deno unchanged. The test now also passes a QUARTO_DENO_EXTRA_OPTIONS value containing & ^ and !. --- package/scripts/windows/quarto.cmd | 13 +++++++++---- tests/unit/launcher-arguments.test.ts | 28 ++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/package/scripts/windows/quarto.cmd b/package/scripts/windows/quarto.cmd index 7f6dffd41bd..46c14cff0ce 100644 --- a/package/scripts/windows/quarto.cmd +++ b/package/scripts/windows/quarto.cmd @@ -168,10 +168,15 @@ IF DEFINED QUARTO_TS_PROFILE ( SET "QUARTO_TS_PROFILE=true" ) -REM Launch with delayed expansion off: %* is substituted before delayed expansion -REM runs, so on a line using !VAR! a "!" or "^" in the arguments would be consumed. -SETLOCAL DISABLEDELAYEDEXPANSION -"%QUARTO_DENO%" %QUARTO_ACTION% %QUARTO_CACHE_OPTIONS% %QUARTO_DENO_OPTIONS% %QUARTO_DENO_EXTRA_OPTIONS% %QUARTO_IMPORT_MAP_ARG% "%QUARTO_TARGET%" %* +REM The command is built with !VAR!, so cmd does not interpret & or ^ in option values, +REM and runs with delayed expansion off: %* is substituted before delayed expansion, +REM which would consume any "!" or "^" in the arguments. %%C is substituted after cmd +REM parses the line, which keeps the built command literal. +SET "QUARTO_DENO_CMD="!QUARTO_DENO!" !QUARTO_ACTION! !QUARTO_CACHE_OPTIONS! !QUARTO_DENO_OPTIONS! !QUARTO_DENO_EXTRA_OPTIONS! !QUARTO_IMPORT_MAP_ARG! "!QUARTO_TARGET!"" +FOR /F "delims=" %%C IN ("!QUARTO_DENO_CMD!") DO ( + SETLOCAL DISABLEDELAYEDEXPANSION + %%C %* +) :end \ No newline at end of file diff --git a/tests/unit/launcher-arguments.test.ts b/tests/unit/launcher-arguments.test.ts index 52657e7cba0..add2f99fe19 100644 --- a/tests/unit/launcher-arguments.test.ts +++ b/tests/unit/launcher-arguments.test.ts @@ -25,8 +25,15 @@ import { withTempDir } from "../utils.ts"; const repoRoot = join(dirname(fromFileUrl(import.meta.url)), "..", ".."); +// Prints the arguments and, when Deno got a --location option, its URL. const probe = 'import process from "node:process";\n' + - 'console.log("ARGS=" + JSON.stringify(process.argv.slice(2)));\n'; + 'console.log("ARGS=" + JSON.stringify(process.argv.slice(2)));\n' + + 'let loc = "";\n' + + "try { loc = globalThis.location.href; } catch { /* no --location */ }\n" + + 'console.log("LOC=" + loc);\n'; + +// A Deno option whose value holds characters cmd would otherwise interpret. +const location = "http://example.com/?a=1&b=^c!d"; const specialArgs = [ "C:\\work\\notes!", @@ -77,6 +84,23 @@ async function assertArgumentsReachDeno( assert(result.success, `quarto.cmd exited with an error ${details}`); } +async function assertOptionsReachDeno(launcher: Launcher, cwd: string) { + const { result, stdout, details } = await launch({ + cmd: launcher.cmd, + env: { + ...launcher.env, + QUARTO_DENO_EXTRA_OPTIONS: `--location=${location}`, + }, + }, ["run"], cwd); + const locLine = stdout.split(/\r?\n/).find((line) => line.startsWith("LOC=")); + assertEquals( + locLine, + `LOC=${location}`, + `quarto.cmd changed QUARTO_DENO_EXTRA_OPTIONS ${details}`, + ); + assert(result.success, `quarto.cmd exited with an error ${details}`); +} + async function assertShortCircuits(launcher: Launcher, cwd: string) { for (const flag of ["--version", "-v"]) { const { stdout, details } = await launch(launcher, [flag], cwd); @@ -127,6 +151,7 @@ unitTest( }; await assertArgumentsReachDeno(launcher, ["run", ...specialArgs], dir); await assertArgumentsReachDeno(launcher, ["a & b", "x"], dir); + await assertOptionsReachDeno(launcher, dir); await assertShortCircuits(launcher, dir); } finally { // Remove the junction itself (not its target) before the temp dir goes. @@ -156,6 +181,7 @@ unitTest( }; await assertArgumentsReachDeno(launcher, ["run", ...specialArgs], dir); await assertArgumentsReachDeno(launcher, ["a & b", "x"], dir); + await assertOptionsReachDeno(launcher, dir); await assertShortCircuits(launcher, dir); }); }, From 248925da07c94908009ab7aeb51803658adda3c7 Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 13 Nov 2026 17:54:33 +0100 Subject: [PATCH 3/4] Run quarto.cmd's Deno command outside a parenthesized block The FOR /F body that launches Deno was a parenthesized block, so a ")" in an argument typed unquoted at a terminal (quarto.cmd render report(1).qmd) closed it and cmd failed with ".qmd was unexpected". The launch line on main sat inside an IF/ELSE block and failed the same way. The DO clause is now a single line: SETLOCAL DISABLEDELAYEDEXPANSION & %%C %*. cmd applies delayed expansion to each command when it runs, so %* still reaches Deno with delayed expansion off. The test now also runs quarto.cmd through cmd with an unquoted command line. --- package/scripts/windows/quarto.cmd | 8 +++----- tests/unit/launcher-arguments.test.ts | 29 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/package/scripts/windows/quarto.cmd b/package/scripts/windows/quarto.cmd index 46c14cff0ce..33d5fa08812 100644 --- a/package/scripts/windows/quarto.cmd +++ b/package/scripts/windows/quarto.cmd @@ -171,12 +171,10 @@ IF DEFINED QUARTO_TS_PROFILE ( REM The command is built with !VAR!, so cmd does not interpret & or ^ in option values, REM and runs with delayed expansion off: %* is substituted before delayed expansion, REM which would consume any "!" or "^" in the arguments. %%C is substituted after cmd -REM parses the line, which keeps the built command literal. +REM parses the line, which keeps the built command literal. No parentheses around the +REM DO command: a ")" in an unquoted argument would close them. SET "QUARTO_DENO_CMD="!QUARTO_DENO!" !QUARTO_ACTION! !QUARTO_CACHE_OPTIONS! !QUARTO_DENO_OPTIONS! !QUARTO_DENO_EXTRA_OPTIONS! !QUARTO_IMPORT_MAP_ARG! "!QUARTO_TARGET!"" -FOR /F "delims=" %%C IN ("!QUARTO_DENO_CMD!") DO ( - SETLOCAL DISABLEDELAYEDEXPANSION - %%C %* -) +FOR /F "delims=" %%C IN ("!QUARTO_DENO_CMD!") DO SETLOCAL DISABLEDELAYEDEXPANSION & %%C %* :end \ No newline at end of file diff --git a/tests/unit/launcher-arguments.test.ts b/tests/unit/launcher-arguments.test.ts index add2f99fe19..9c9aa07fcb8 100644 --- a/tests/unit/launcher-arguments.test.ts +++ b/tests/unit/launcher-arguments.test.ts @@ -84,6 +84,33 @@ async function assertArgumentsReachDeno( assert(result.success, `quarto.cmd exited with an error ${details}`); } +// Runs quarto.cmd through cmd with an unquoted command line, as typed in a +// terminal, where cmd itself sees the ")" in "report(1).qmd". +async function assertTypedArgumentsReachDeno(launcher: Launcher, cwd: string) { + const result = await execProcess({ + cmd: "cmd", + args: ["/d", "/s", "/c", `""${launcher.cmd}" run report(1).qmd C:\\notes!"`], + windowsRawArguments: true, + cwd, + stdout: "piped", + stderr: "piped", + env: launcher.env, + }); + const stdout = result.stdout ?? ""; + const details = `(exit ${result.code})\nstdout:\n${stdout}\nstderr:\n${ + result.stderr ?? "" + }`; + const argsLine = stdout.split(/\r?\n/).find((line) => + line.startsWith("ARGS=") + ); + assertEquals( + argsLine, + 'ARGS=["run","report(1).qmd","C:\\\\notes!"]', + `quarto.cmd changed a typed command line ${details}`, + ); + assert(result.success, `quarto.cmd exited with an error ${details}`); +} + async function assertOptionsReachDeno(launcher: Launcher, cwd: string) { const { result, stdout, details } = await launch({ cmd: launcher.cmd, @@ -152,6 +179,7 @@ unitTest( await assertArgumentsReachDeno(launcher, ["run", ...specialArgs], dir); await assertArgumentsReachDeno(launcher, ["a & b", "x"], dir); await assertOptionsReachDeno(launcher, dir); + await assertTypedArgumentsReachDeno(launcher, dir); await assertShortCircuits(launcher, dir); } finally { // Remove the junction itself (not its target) before the temp dir goes. @@ -182,6 +210,7 @@ unitTest( await assertArgumentsReachDeno(launcher, ["run", ...specialArgs], dir); await assertArgumentsReachDeno(launcher, ["a & b", "x"], dir); await assertOptionsReachDeno(launcher, dir); + await assertTypedArgumentsReachDeno(launcher, dir); await assertShortCircuits(launcher, dir); }); }, From c4467f8b11bd1954f3a773807e37bfe66692e9db Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Fri, 13 Nov 2026 18:09:33 +0100 Subject: [PATCH 4/4] Record the quarto.cmd launcher fix as a PR 33 prerequisite Plan 33 Task 9 Step 4 replaces quarto.cmd with a full listing written before #15276 and this fix. Pasted as is, it would bring back the argument corruption, the "%1" shortcuts and the unquoted win_configuration.bat call. Per the migration AGENTS.md, the finding goes in the open-decisions table for PR 33's step 0, not into the plan. --- dev-docs/runtime-migration/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/dev-docs/runtime-migration/README.md b/dev-docs/runtime-migration/README.md index 3ecccbe0f92..77486c0a840 100644 --- a/dev-docs/runtime-migration/README.md +++ b/dev-docs/runtime-migration/README.md @@ -155,6 +155,7 @@ The rows below are the ones that still need a named person or an external answer | 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 | | 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 | | PRs 23 and 31 (from #15341) | `tests/unit/exec-process-utf8-chunks.test.ts` starts its child as `execPath() eval '