Handle nested imports and prevent cycles in import flow - #98
Merged
Conversation
Track in-progress imports with an AsyncLocal ImmutableHashSet to avoid infinite recursion when files import each other. Rework source-file import flow to: 1) rebase nested #!import paths relative to the importing file (RebaseImportLine), 2) run magic directives individually in priority order and then run the file's own code (RunAsync), and 3) stop suppressing execution for empty/whitespace arguments. Add System.Collections.Immutable and related docs/comments. Update tests to reflect changed SuppressExecution behaviour, ensure directives are submitted separately, and add tests for nested import rebasing and circular import termination. Signed-off-by: Torrey Betts <torrey.betts@gmail.com>
SummarySummary
CoverageVerso - 79.4%
Verso.Abstractions - 83.4%
Verso.Ado - 81.4%
Verso.Blazor - 26%
Verso.Blazor.Shared - 61.1%
Verso.Cli - 41.3%
Verso.FSharp - 82.4%
Verso.Host - 55%
Verso.Http - 74.7%
Verso.JavaScript - 52.7%
Verso.PowerShell - 64.7%
Verso.Python - 76.4%
Verso.Sample.Diagram - 80.1%
Verso.Sample.Dice - 82.2%
Verso.Sample.Slides - 58.8%
Verso.Sample.Sparkline - 67.3%
Verso.Testing - 80.2%
|
Track the directory of the file currently being imported via a new AsyncLocal (ImportingDirectory) and use a new ResolveImportPath helper to prefer files sitting beside the importing file, falling back to the notebook path. Stop rewriting nested #!import directive text (which broke paths containing spaces); instead resolve paths to full paths when appropriate. ExecuteAsync now sets/restores ImportingDirectory around nested imports. Updated ResolvePath docs and replaced the old RebaseImportLine behavior. Tests updated to exercise ResolveImportPath and a new NestedImportIntegrationTests integration suite was added to cover nested resolution, spaces-in-paths, fallback behavior, precedence, recursion termination, multi-level nesting and imports inside imported notebooks. Signed-off-by: Torrey Betts <torrey.betts@gmail.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.
This pull request improves the behavior and reliability of the
#!importmagic command in the Verso project, focusing on correct handling of nested imports, directive execution ordering, and user expectations regarding code execution. The changes also update related tests to match the new logic and clarify documentation.Key improvements and changes:
Import logic and execution order
ImportMagicCommand.cs) [1] [2] [3] [4]ImportMagicCommand.cs) [1] [2] [3]Nested import path handling
RebaseImportLineto resolve nested import paths relative to the importing file, making helper files and folders more portable and intuitive to use. (ImportMagicCommand.cs) [1] [2]Documentation and comments
ImportMagicCommand.cs) [1] [2]Test updates
SuppressExecutionis not set, and that directives and code are executed in the correct order and isolation. (ImportMagicCommandTests.cs) [1] [2] [3] [4] [5] [6] [7] [8] [9]Code hygiene
System.Collections.Immutableimport for thread-safe tracking of in-progress imports. (ImportMagicCommand.cs)These changes make the import process more robust, predictable, and user-friendly, especially when working with complex, nested, or reusable code files.