From eed3586ed6a4ece4394366e67108cbfd79d41355 Mon Sep 17 00:00:00 2001 From: Nelson Love Date: Sat, 11 Jul 2026 14:13:37 -0400 Subject: [PATCH] Don't write injected code back into the shared block context The CodeBlockContext is shared between runs of the same rendered block; assigning the injected source back into it made every re-run inject pre/post/import code on top of the previous run's injection. Co-Authored-By: Claude Fable 5 --- src/RunButton.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/RunButton.ts b/src/RunButton.ts index a3dae51d..2991f256 100644 --- a/src/RunButton.ts +++ b/src/RunButton.ts @@ -37,7 +37,10 @@ async function handleExecution(block: CodeBlockContext) { const s: ExecutorSettings = block.outputter.settings; button.className = disabledClass; - block.srcCode = await new CodeInjector(app, s, language).injectCode(srcCode); + // Run against a copy of the block context: the context is shared between + // runs of the same rendered block, so writing the injected code back into + // it would compound pre/post/import injections on every re-run. + block = { ...block, srcCode: await new CodeInjector(app, s, language).injectCode(srcCode) }; switch (language) { case "js": return runCode(s.nodePath, s.nodeArgs, s.jsFileExtension, block, { transform: (code) => macro.expandJS(code) });