Skip to content

Commit 2e498bd

Browse files
committed
pre,postrender - use QUARTO_USE_* environment when requested
1 parent 412ea63 commit 2e498bd

File tree

10 files changed

+130
-23
lines changed

10 files changed

+130
-23
lines changed

src/command/render/project.ts

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -326,18 +326,34 @@ export async function renderProject(
326326

327327
// run pre-render step if we are rendering all files
328328
if (preRenderScripts.length) {
329+
// https://github.com/quarto-dev/quarto-cli/issues/10828
330+
// some environments limit the length of environment variables.
331+
// It's hard to know in advance what the limit is, so we will
332+
// instead ask users to configure their environment with
333+
// the names of the files we will write the list of files to.
334+
335+
const filesToRender = projectRenderConfig.filesToRender
336+
.map((fileToRender) => fileToRender.path)
337+
.map((file) => relative(projDir, file));
338+
const env: Record<string, string> = {
339+
...prePostEnv,
340+
};
341+
342+
if (Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES")) {
343+
Deno.writeTextFileSync(
344+
Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES")!,
345+
filesToRender.join("\n"),
346+
);
347+
} else {
348+
env.QUARTO_PROJECT_INPUT_FILES = filesToRender.join("\n");
349+
}
350+
329351
await runPreRender(
330352
projDir,
331353
preRenderScripts,
332354
progress,
333355
!!projectRenderConfig.options.flags?.quiet,
334-
{
335-
...prePostEnv,
336-
QUARTO_PROJECT_INPUT_FILES: projectRenderConfig.filesToRender
337-
.map((fileToRender) => fileToRender.path)
338-
.map((file) => relative(projDir, file))
339-
.join("\n"),
340-
},
356+
env,
341357
);
342358

343359
// re-initialize project context
@@ -814,17 +830,34 @@ export async function renderProject(
814830

815831
// run post-render if this isn't incremental
816832
if (postRenderScripts.length) {
833+
// https://github.com/quarto-dev/quarto-cli/issues/10828
834+
// some environments limit the length of environment variables.
835+
// It's hard to know in advance what the limit is, so we will
836+
// instead ask users to configure their environment with
837+
// the names of the files we will write the list of files to.
838+
839+
const env: Record<string, string> = {
840+
...prePostEnv,
841+
};
842+
843+
if (Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES")) {
844+
Deno.writeTextFileSync(
845+
Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES")!,
846+
outputFiles.map((outputFile) => relative(projDir, outputFile.file))
847+
.join("\n"),
848+
);
849+
} else {
850+
env.QUARTO_PROJECT_OUTPUT_FILES = outputFiles
851+
.map((outputFile) => relative(projDir, outputFile.file))
852+
.join("\n");
853+
}
854+
817855
await runPostRender(
818856
projDir,
819857
postRenderScripts,
820858
progress,
821859
!!projectRenderConfig.options.flags?.quiet,
822-
{
823-
...prePostEnv,
824-
QUARTO_PROJECT_OUTPUT_FILES: outputFiles
825-
.map((outputFile) => relative(projDir, outputFile.file))
826-
.join("\n"),
827-
},
860+
env,
828861
);
829862
}
830863
}
@@ -908,6 +941,23 @@ async function runScripts(
908941

909942
const handler = handlerForScript(script);
910943
if (handler) {
944+
if (env) {
945+
env = {
946+
...env,
947+
};
948+
} else {
949+
env = {};
950+
}
951+
if (!env) throw new Error("should never get here");
952+
const input = Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES");
953+
const output = Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES");
954+
if (input) {
955+
env["QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES"] = input;
956+
}
957+
if (output) {
958+
env["QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES"] = output;
959+
}
960+
911961
const result = await handler.run(script, args.splice(1), undefined, {
912962
cwd: projDir,
913963
stdout: quiet ? "piped" : "inherit",

src/quarto.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export async function quarto(
9393

9494
// passthrough to run handlers
9595
if (args[0] === "run" && args[1] !== "help" && args[1] !== "--help") {
96-
const result = await runScript(args.slice(1));
96+
const result = await runScript(args.slice(1), env);
9797
Deno.exit(result.code);
9898
}
9999

@@ -151,16 +151,16 @@ export async function quarto(
151151

152152
const promise = quartoCommand.command("help", new HelpCommand().global())
153153
.command("completions", new CompletionsCommand()).hidden().parse(args);
154-
for (const [key, value] of Object.entries(oldEnv)) {
155-
if (value === undefined) {
156-
Deno.env.delete(key);
157-
} else {
158-
Deno.env.set(key, value);
159-
}
160-
}
161154

162155
try {
163156
await promise;
157+
for (const [key, value] of Object.entries(oldEnv)) {
158+
if (value === undefined) {
159+
Deno.env.delete(key);
160+
} else {
161+
Deno.env.set(key, value);
162+
}
163+
}
164164
} catch (e) {
165165
if (e instanceof CommandError) {
166166
logError(e, false);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.quarto/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
project:
2+
type: website
3+
pre-render: check-input.ts
4+
post-render: check-output.ts
5+
6+
website:
7+
title: "issue-10828"
8+
navbar:
9+
left:
10+
- href: index.qmd
11+
text: Home
12+
- about.qmd
13+
14+
format:
15+
html:
16+
theme: cosmo
17+
css: styles.css
18+
toc: true
19+
20+
21+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: "About"
3+
---
4+
5+
About this site
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deno.readTextFileSync(Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES"));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deno.readTextFileSync(Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES"));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: "issue-10828"
3+
---
4+
5+
This is a Quarto website.
6+
7+
To learn more about Quarto websites visit <https://quarto.org/docs/websites>.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* css styles */

tests/smoke/project/project-prepost.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { join } from "../../../src/deno_ral/path.ts";
1010
import { existsSync } from "../../../src/deno_ral/fs.ts";
1111
import { testQuartoCmd } from "../../test.ts";
1212
import { fileExists, noErrors, printsMessage, verifyNoPath, verifyPath } from "../../verify.ts";
13-
import { safeRemoveIfExists } from "../../../src/core/path.ts";
13+
import { normalizePath, safeRemoveIfExists } from "../../../src/core/path.ts";
1414

1515
const renderDir = docs("project/prepost/mutate-render-list");
1616
const dir = join(Deno.cwd(), renderDir);
@@ -76,4 +76,24 @@ testQuartoCmd(
7676
verifyPath(path);
7777
safeRemoveIfExists(path);
7878
}
79-
});
79+
});
80+
81+
testQuartoCmd(
82+
"render",
83+
[docs("project/prepost/issue-10828")],
84+
[],
85+
{
86+
env: {
87+
"QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES": normalizePath(docs("project/prepost/issue-10828/input-files.txt")),
88+
"QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES": normalizePath(docs("project/prepost/issue-10828/output-files.txt"))
89+
},
90+
teardown: async () => {
91+
const inputPath = normalizePath(docs("project/prepost/issue-10828/input-files.txt"));
92+
const outputPath = normalizePath(docs("project/prepost/issue-10828/output-files.txt"));
93+
verifyPath(inputPath);
94+
safeRemoveIfExists(inputPath);
95+
verifyPath(outputPath);
96+
safeRemoveIfExists(outputPath);
97+
}
98+
}
99+
)

0 commit comments

Comments
 (0)