Repository navigation
Fix crash on circular import reached through a .. path - #18736
Conversation
|
so this stores the canonical path alongside the original path (as |
8c0b042 to
e4a4e64
Compare
|
Good question, I built both and measured instead of guessing. Setup: a module chain N levels deep (
At the depths anyone actually hits, all three are the same to within run to run noise, so There is also a reason to keep the original path that is not about speed. If you would still rather have the smaller struct I am happy to switch, it is a small change. |
|
thanks for checking! I think this call isn't mine to make. deferring to a more experienced maintainer. |
|
Following up, since this has been waiting a couple of weeks for someone to pick it up after pickx deferred. To save the next reader the scrollback: the crash is real and the fix is small, and the only open question was pickx's, whether to canonicalize on the fly in |
There was a problem hiding this comment.
🟢 Approval recommended
The implementation addresses the recursion failure while preserving path-resolution behavior and includes focused regression coverage.
Pull request overview
Canonicalizes file identities in FileStack to reliably detect circular imports through .. paths and symlinks while preserving original paths for resolution and errors.
Changes:
- Separates display/resolution paths from canonical comparison identities.
- Adds a regression test for parent-directory circular imports.
File summaries
| File | Description |
|---|---|
crates/nu-protocol/src/engine/state_working_set.rs |
Canonicalizes file-stack identities. |
tests/modules/mod.rs |
Tests circular imports through ... |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
is this still an issue? if so can you rebase on main? |
The parser builds module and source paths with `std::path::absolute`, which keeps `..` components on unix. `FileStack` compared those paths as they were, so the same file reached as `c/mod.nu` and as `c/i/../mod.nu` counted as two files and the circular import check never fired. Each trip around the cycle appended another `/i/..`, so the parser recursed until the stack ran out. Files on the stack are now identified by their canonical path, which resolves `..` and symlinks, while the path a file was reached by is kept for error messages and for resolving the files it imports. Paths with no file behind them keep their textually expanded form. Reported by @cornmonger, who pointed at the comparison in `FileStack`.
e4a4e64 to
8a1c777
Compare
|
Yes it still happens. On main at b4993b5 (Linux), the repro from the issue ends with thread 'main' has overflowed its stack and fatal runtime error: stack overflow, aborting, exit code 134. I rebased onto that commit, and with the fix the same command reports nu::parser::circular_import and exits 1. The only conflict was in tests/modules/mod.rs, where a new test had landed in the same place, so I kept both. |
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
The fix stops the .. cycle for use, overlay use and source. On macOS main reported a missing file with a c/i/../i/../... path, and the branch reports the circular import. Your numbers settle pickx's question for me: storing the identity once per file is fine. Two things are left: virtual std paths pick up the launch directory, and with_file now pays a realpath for every runtime working set in a script.
Findings: 1
Open (3)
Notes from my clanker, checked on 8a1c777. Built the branch, ran cargo test --test tests -- modules:: parsing:: (273 passed), and ran each case on the branch binary and on 0.117.1. The timings compare the branch with a trial build of it where with_file skips canonicalizing, both debug. CI already ran clippy and the tests on this head, and I ran nothing on Linux or Windows.
Standard library paths such as `std/log/mod.nu` are virtual, but they were canonicalized against the process's current directory, so a real file at that relative path took their identity and a module wrapping `std/log` reported a circular import. The parser now tells `FileStack` which paths are virtual, and those are compared by their path as given, never against files on disk. `with_file` canonicalized the root of every working set, which cost a realpath for each caught error in a script. The root is now identified only when a file on disk is pushed after it. A file on disk is identified by the canonical path of the directory it was reached in, joined with its name. A file resolves its imports against that directory, so the same file reached through a symlink in another directory is not a circular import. Windows device paths are kept as given. Adds tests for `source` cycles through `..` and through a symlink, for the std case and for the symlinked file.
|
One more change you didn't ask for. While going through every path that reaches # d1/f.nu -> d2/f.nu (symlink)
# d2/f.nu: source sib.nu
# d1/sib.nu: source ../d2/f.nu
# d2/sib.nu: 'd2 sib'
source d1/f.nu
# main: d2 sib
# 8a1c77700: Error: nu::parser::circular_import
# branch: d2 sibA file on disk is now identified by the canonical path of its directory joined with its name. That still stops every It isn't exact, and I'd like your view on it. Where What else changed: Windows device paths are kept as given, as on main. With What I ran, all on Linux, on main, on 8a1c777 and on the branch: 37 cases covering On Windows, Not tested: macOS and |
There was a problem hiding this comment.
Review overview
🟢 Minor suggestions
All three threads are fixed on 18009bb and I've resolved them. One new low: the root's lazy identity has no test.
Findings: 0
Notes from my clanker, checked on 18009bb. Built the branch on macOS and ran each case on it and on an installed 0.117.1 (c447af7): the std/log wrapper from two launch dirs and after a config cd, .. cycles through use, overlay use and source, symlink cycles through source, your d1/d2 example, plain and .. cycles under --ide-check, nu a.nu, nu-check and run --full-reparse. Ran cargo test --test tests -- circular cyclical with StackedFile::is changed so the root is never identified (10 passed). CI is green on this head; I didn't run anything on Windows.
|
I'd keep the directory + name identity. On 18009bb on macOS, your d1/d2 example prints |
`nu a.nu` puts the script at the bottom of the file stack before it has an identity. The new test runs a child nu on the `..` fixture and checks that the cycle is reported where it closes, at `source ../a.nu`.
|
Thanks for running the cases on macOS, and for agreeing to keep the directory and name identity with the caveat in Not tested: your mutation and the main check on macOS and Windows. I ran those on Linux only. |
|
looks like we have a conflict now |
Port the six new tests to the new `Playground` from nushell#18915, which removed `Playground::setup`.
|
Fixed by merging main. #18915 removed |
|
Thanks |



Description
On unix, importing a module that imports one of its parents back through a
..path neverterminates and nushell dies with a segfault instead of reporting a circular import (#18731):
The parser builds these paths with
std::path::absolute, which drops.components but keeps..on unix, because a..component is not the same thing as the parent directory oncesymlinks are in play.
FileStack, which is what detects circular imports, compared those pathsas they were, so
/tmp/nucrash/c/mod.nuand/tmp/nucrash/c/i/../mod.nulooked like twodifferent files. Each trip around the cycle appended another
/i/..to the path, so the importnever repeated itself, the check never fired, and the parser recursed until the stack ran out.
That also explains why the symptom is not the same everywhere. Whether you get a crash depends
on which limit the growing path hits first: on macOS the path stops resolving before the stack
runs out, so it surfaces as a
module_not_foundnaming a path with several hundred../icomponents in it, as @pickx saw on the issue. Windows was already fine, because
std::path::absoluteresolves..there, so the same repro reportsnu::parser::circular_importtoday. This makes the other platforms behave that way too.
Files on the stack are now identified by their canonical path, which resolves
..along withsymlinks pointing at a file that is already being parsed. The path a file was actually reached
by is kept as it was, since that is what error messages show and what the files it imports are
resolved against, so nothing else changes. Paths with no file behind them, such as the virtual
paths of the standard library, fall back to expanding
.and..textually.Legitimate
..imports are unaffected: a module importing a sibling through../other/mod.nustill resolves and is not reported as circular (
module_nested_imports_in_dirscovers this).Tested on Linux, where the repro now prints a circular import error and exits 1 instead of
dying on SIGSEGV, and on Windows, where behavior is unchanged. The new regression test fails
on Linux without the fix and passes with it.
User-facing changes (Release notes)
Fixed a crash when importing a module that imports one of its parents back through a
..path,for example a
c/i/mod.nucontaininguse ../mod.nu. Nushell reported a circular import onWindows but crashed on other platforms, and now reports the circular import everywhere.
Additional notes
fixes #18731