Skip to content

Commit aeb073b

Browse files
authored
Merge pull request #12337 from quarto-dev/bugfix/12336
Clean up transients from `quarto inspect`
2 parents 8bd9c6e + 20078c1 commit aeb073b

File tree

7 files changed

+231
-151
lines changed

7 files changed

+231
-151
lines changed

news/changelog-1.7.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ All changes included in 1.7:
4141

4242
- ([#12042](https://github.com/quarto-dev/quarto-cli/issues/12042)): Preserve Markdown content that follows YAML metadata in a `raw` .ipynb cell.
4343

44+
## `quarto inspect`
45+
46+
- ([#12336](https://github.com/quarto-dev/quarto-cli/issues/12336)): Clean up transient files created by `quarto inspect`.
47+
4448
## `html` format
4549

4650
- ([#12277](https://github.com/quarto-dev/quarto-cli/pull/12277)): Provide light and dark plot and table renderings with `renderings: [light,dark]`

src/project/project-context.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import { ExecutionEngine, kMarkdownEngine } from "../execute/types.ts";
6969
import { projectResourceFiles } from "./project-resources.ts";
7070

7171
import {
72+
cleanupFileInformationCache,
7273
ignoreFieldsForProjectType,
7374
normalizeFormatYaml,
7475
projectConfigFile,
@@ -268,6 +269,7 @@ export async function projectContext(
268269
dir: join(dir, ".quarto"),
269270
prefix: "quarto-session-temp",
270271
});
272+
const fileInformationCache = new Map();
271273
const result: ProjectContext = {
272274
resolveBrand: async (fileName?: string) =>
273275
projectResolveBrand(result, fileName),
@@ -287,7 +289,7 @@ export async function projectContext(
287289
},
288290
dir,
289291
engines: [],
290-
fileInformationCache: new Map(),
292+
fileInformationCache,
291293
files: {
292294
input: [],
293295
},
@@ -313,6 +315,7 @@ export async function projectContext(
313315
diskCache: await createProjectCache(join(dir, ".quarto")),
314316
temp,
315317
cleanup: () => {
318+
cleanupFileInformationCache(result);
316319
result.diskCache.close();
317320
temp.cleanup();
318321
},
@@ -361,6 +364,7 @@ export async function projectContext(
361364
const temp = createTempContext({
362365
dir: join(dir, ".quarto", "temp"),
363366
});
367+
const fileInformationCache = new Map();
364368
const result: ProjectContext = {
365369
resolveBrand: async (fileName?: string) =>
366370
projectResolveBrand(result, fileName),
@@ -381,7 +385,7 @@ export async function projectContext(
381385
dir,
382386
config: projectConfig,
383387
engines: [],
384-
fileInformationCache: new Map(),
388+
fileInformationCache,
385389
files: {
386390
input: [],
387391
},
@@ -404,6 +408,7 @@ export async function projectContext(
404408
diskCache: await createProjectCache(join(dir, ".quarto")),
405409
temp,
406410
cleanup: () => {
411+
cleanupFileInformationCache(result);
407412
result.diskCache.close();
408413
},
409414
};
@@ -429,6 +434,7 @@ export async function projectContext(
429434
configResolvers.shift();
430435
} else if (force) {
431436
const temp = globalTempContext();
437+
const fileInformationCache = new Map();
432438
const context: ProjectContext = {
433439
resolveBrand: async (fileName?: string) =>
434440
projectResolveBrand(context, fileName),
@@ -453,7 +459,7 @@ export async function projectContext(
453459
[kProjectOutputDir]: flags?.outputDir,
454460
},
455461
},
456-
fileInformationCache: new Map(),
462+
fileInformationCache,
457463
files: {
458464
input: [],
459465
},
@@ -476,6 +482,7 @@ export async function projectContext(
476482
diskCache: await createProjectCache(join(temp.baseDir, ".quarto")),
477483
temp,
478484
cleanup: () => {
485+
cleanupFileInformationCache(context);
479486
context.diskCache.close();
480487
},
481488
};

src/project/project-shared.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Copyright (C) 2020-2022 Posit Software, PBC
55
*/
66

7-
import { existsSync } from "../deno_ral/fs.ts";
7+
import { existsSync, safeRemoveSync } from "../deno_ral/fs.ts";
88
import {
99
dirname,
1010
isAbsolute,
@@ -582,3 +582,27 @@ export async function projectResolveBrand(
582582
}
583583
}
584584
}
585+
586+
export function cleanupFileInformationCache(project: ProjectContext) {
587+
project.fileInformationCache.forEach((entry) => {
588+
if (entry?.target?.data) {
589+
const data = entry.target.data as {
590+
transient?: boolean;
591+
};
592+
if (data.transient && entry.target?.input) {
593+
safeRemoveSync(entry.target?.input);
594+
}
595+
}
596+
});
597+
}
598+
599+
export async function withProjectCleanup<T>(
600+
project: ProjectContext,
601+
fn: (project: ProjectContext) => Promise<T>,
602+
): Promise<T> {
603+
try {
604+
return await fn(project);
605+
} finally {
606+
project.cleanup();
607+
}
608+
}

0 commit comments

Comments
 (0)