Skip to content

Commit b8fd729

Browse files
committed
Refactor cleanupNotebook for clarity and fix keep-ipynb edge case
Restructure cleanupNotebook with early returns that separate three concerns: non-transient notebooks, keep-ipynb preservation, and transient deletion. Previously, when keep-ipynb: true was set but the fileInformationCache missed on target.source (e.g., path normalization mismatches or preview re-render invalidation), the else-if branch would still delete the file. The refactored logic checks keep-ipynb first and short-circuits before the cache lookup, so the user's explicit keep-ipynb: true is always honored regardless of cache state. Cache update remains an internal optimization for later project-level cleanup — not the authoritative signal for whether to delete.
1 parent 5c0e1fb commit b8fd729

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

src/execute/jupyter/jupyter.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -863,16 +863,26 @@ function cleanupNotebook(
863863
format: Format,
864864
project: EngineProjectContext,
865865
) {
866-
// Make notebook non-transient when keep-ipynb is set
867866
const data = target.data as JupyterTargetData;
868-
const cached = project.fileInformationCache.get(target.source);
869-
if (cached && data.transient && format.execute[kKeepIpynb]) {
870-
if (cached.target && cached.target.data) {
867+
868+
// Nothing to do for non-transient notebooks (user-authored .ipynb files)
869+
if (!data.transient) {
870+
return;
871+
}
872+
873+
// User wants to keep the notebook — do not delete.
874+
// Also mark the cache entry (if any) as non-transient so later cache
875+
// cleanup doesn't remove it.
876+
if (format.execute[kKeepIpynb]) {
877+
const cached = project.fileInformationCache.get(target.source);
878+
if (cached && cached.target && cached.target.data) {
871879
(cached.target.data as JupyterTargetData).transient = false;
872880
}
873-
} else if (data.transient) {
874-
safeRemoveSync(target.input);
881+
return;
875882
}
883+
884+
// Otherwise the transient notebook is no longer needed; delete it.
885+
safeRemoveSync(target.input);
876886
}
877887

878888
interface JupyterTargetData {

0 commit comments

Comments
 (0)