Files
user77 624b8edddf Fix crash on circular import reached through a .. path (#18736)
## Description

On unix, importing a module that imports one of its parents back through
a `..` path never
terminates and nushell dies with a segfault instead of reporting a
circular import (#18731):

```nu
mkdir /tmp/nucrash/c/i
"export module i" | save /tmp/nucrash/c/mod.nu
"use ../mod.nu"   | save /tmp/nucrash/c/i/mod.nu

nu -c "use /tmp/nucrash/c"
# Segmentation fault (core dumped)
```

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 once
symlinks are in play. `FileStack`, which is what detects circular
imports, compared those paths
as they were, so `/tmp/nucrash/c/mod.nu` and
`/tmp/nucrash/c/i/../mod.nu` looked like two
different files. Each trip around the cycle appended another `/i/..` to
the path, so the import
never 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_found` naming a path with
several hundred `../i`
components in it, as @pickx saw on the issue. Windows was already fine,
because
`std::path::absolute` resolves `..` there, so the same repro reports
`nu::parser::circular_import`
today. This makes the other platforms behave that way too.

Files on the stack are now identified by their canonical path, which
resolves `..` along with
symlinks 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.nu`
still resolves and is not reported as circular
(`module_nested_imports_in_dirs` covers 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.nu` containing `use ../mod.nu`. Nushell reported
a circular import on
Windows but crashed on other platforms, and now reports the circular
import everywhere.

## Additional notes

fixes #18731
2026-11-20 08:26:22 -06:00
..