Fix compounding code injection on repeated runs of the same block#451
Open
nelsonlove wants to merge 1 commit into
Open
Fix compounding code injection on repeated runs of the same block#451nelsonlove wants to merge 1 commit into
nelsonlove wants to merge 1 commit into
Conversation
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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
handleExecutionassigns the injected source back into the block'sCodeBlockContext(block.srcCode = await ...injectCode(srcCode)). That context is shared between runs of the same rendered block (it's captured in the run button's click listener), so every re-run injects pre-/post-/import code on top of the previous run's already-injected source.Reproduction: with a labeled block (
{label="x"}) and a second block importing it ({import="x"}), click Run on the importing block N times without re-rendering the note → the imported code is included N times (visible as N repeated definition echoes in interactive mode, and N-fold side effects for non-idempotent imports).A side effect of the stale mutation is that
CodeInjector.parseFilecan no longer match the block against the note's source on re-runs (it comparessrcCodeto the file's block content to find the block's own args), so the block's ownimport/ignoreargs are silently dropped from the second run onward.Fix
Run against a shallow copy of the context:
block = { ...block, srcCode: <injected> }. The shared context is never mutated;runCode's optionaltransformalso operates on the copy.Testing
npm run buildpasses🤖 Generated with Claude Code