diff --git a/dev/dev-test.nu b/dev/dev-test.nu index b6d9bfc..31b7477 100755 --- a/dev/dev-test.nu +++ b/dev/dev-test.nu @@ -52,15 +52,12 @@ const FILTER_CACHE_REL = "target/skyg-test-filters.cache" # Nushell would otherwise accept `--nocapture` on every subcommand and reject # the rest before `main` can explain them. export def --wrapped main [...args: string] { - # Only script mode reaches this entry point: `use ... *` binds the file stem to - # a wrapper that calls the subcommands directly. A non-zero status is still - # wanted from `nu dev/dev-test.nu `, and `exit` is acceptable here. - try { - dispatch-script-args $args - } catch {|e| - print -e $"($RUNNER): ($e.msg)" - exit 1 - } + # Both script mode (`nu dev/dev-test.nu `) and module mode land here: + # `use ... *` binds the bare name `dev-test` to this entry point, so a typo + # such as `dev-test bogus` calls it too. Raising keeps a module-mode caller + # alive; an uncaught error already gives script mode a non-zero status, so + # never `exit` here. + dispatch-script-args $args } def dispatch-script-args [args: list] { @@ -377,7 +374,7 @@ def lint-steps [] { } # Run each step in order, stop at the first failure, always print the summary -# table and exit non-zero if anything failed. +# table and raise if anything failed (the process then exits non-zero). def run-steps [steps: list] { mut results = [] for s in $steps { @@ -759,16 +756,16 @@ def check-filter-cache [] { } # `exit` in a module ends the caller's Nushell session and cannot be caught, so -# a failing check must raise; only the script-mode entry point may exit. +# no code path may call it, not even the entry point: `use ... *` binds the bare +# name to `main` and `dev-test bogus` reaches it. A failing check raises instead. def check-module-mode [] { mut problems = [] let lines = (open --raw $SCRIPT_PATH | lines) - let entry = ($lines | enumerate | where {|r| $r.item =~ '^export def --wrapped main'} | get 0?.index) - let next = ($lines | enumerate | where {|r| $entry != null and $r.index > $entry and ($r.item =~ '^export def ')} | get 0?.index) - let exits = ($lines | enumerate | where {|r| $r.item =~ '^\s*exit\b'} | get index) - let allowed = ($exits | where {|i| $entry != null and $i > $entry and ($next == null or $i < $next)}) - for i in ($exits | where {|i| $i not-in $allowed}) { - $problems = ($problems | append $"line ($i + 1) calls exit outside the script-mode entry point: ($lines | get $i | str trim)") + # The scan has to see an inline `catch { …; exit 1 }` too, not just a line that + # starts with `exit`; comment lines are dropped first so prose cannot trip it. + let exits = ($lines | enumerate | where {|r| ($r.item | str replace --regex '^\s*#.*$' "") =~ '(^|[;{])\s*exit\b'} | get index) + for i in $exits { + $problems = ($problems | append $"line ($i + 1) calls exit, which ends a module-mode session: ($lines | get $i | str trim)") } # `(exit ...)` inside a double-quoted string is a subexpression that ends the # process instead of building the message. @@ -782,6 +779,12 @@ def check-module-mode [] { if $probe.exit_code != 0 or not ($probe.stdout | str contains "caught") or not ($probe.stdout | str contains "after") { $problems = ($problems | append $"module mode: a failing step was not catchable, exit code ($probe.exit_code); wanted 'caught' and 'after' in: ($probe.stdout | str trim) ($probe.stderr | str trim)") } + # Dispatch errors reach `main` too (`use ... *` binds the bare name), so a typo + # must be catchable as well. + let typo = (run-external "nu" "--no-config-file" "-c" $"use ($SCRIPT_PATH) *; try { ($RUNNER) bogus } catch { print 'caught' }; print 'after'" | complete) + if $typo.exit_code != 0 or not ($typo.stdout | str contains "caught") or not ($typo.stdout | str contains "after") { + $problems = ($problems | append $"module mode: an unknown subcommand was not catchable, exit code ($typo.exit_code); wanted 'caught' and 'after' in: ($typo.stdout | str trim) ($typo.stderr | str trim)") + } # Script mode still has to fail. let script = (with-env { SKYG_TEST_ASSUME_NO_KVM: "1" } { run-external "nu" "--no-config-file" $SCRIPT_PATH "vm" | complete @@ -789,6 +792,12 @@ def check-module-mode [] { if $script.exit_code == 0 or not ($script.stderr | str contains "/dev/kvm") { $problems = ($problems | append $"script mode: a failing step exited ($script.exit_code), expected non-zero with the /dev/kvm reason: ($script.stderr | str trim)") } + # The dispatch error is no longer caught in `main`, so script mode relies on + # an uncaught error for its failure status. + let script_typo = (run-external "nu" "--no-config-file" $SCRIPT_PATH "bogus" | complete) + if $script_typo.exit_code == 0 or not ($script_typo.stderr | str contains "unknown subcommand") { + $problems = ($problems | append $"script mode: an unknown subcommand exited ($script_typo.exit_code), expected non-zero with the unknown-subcommand reason: ($script_typo.stderr | str trim)") + } $problems } diff --git a/plan/phase-5-consolidator/ws-t-tests.md b/plan/phase-5-consolidator/ws-t-tests.md index 0971c08..9f99faf 100644 --- a/plan/phase-5-consolidator/ws-t-tests.md +++ b/plan/phase-5-consolidator/ws-t-tests.md @@ -93,3 +93,9 @@ The test builds under `nix build .#tests.x86_64-linux.peers` (or equivalent) and 4. **`TODO.md` T7.** Not edited here (outside WS-T's owned files, and the phase gate rejects changes outside them). Still says `dev/test.nu` and is unticked; the merger updates the name and ticks it. 5. **`AGENTS.md` wording.** "the same command works in an interactive shell" now reads "inside an interactive `devenv shell`". `docs/consolidator.md` says the cache sits at the repo root and that a failing check raises rather than closing the session. - Handoff 2026-10-19 (WS-T review round 2 -> all): unchanged commands and subcommand names. `nu dev/dev-test.nu self-check` now also fails if any command exits (outside `main`), if a message interpolates a call to `exit`, if a failing step is not catchable in module mode, or if the filter cache is not anchored on the repo root. Merger: items 3 and 4 above. +- 2026-10-19 (WS-T review round 3): Addressed `loop/T/review-n2.md` (one blocking item). No contract change, no file outside the owned list, no new dependency. + 1. **Blocking, `main` still exited on a dispatch error.** `export def --wrapped main` wrapped `dispatch-script-args` in `try`/`catch` and called `exit 1`. With `use dev/dev-test.nu *` the bare name `dev-test` is bound to `main`, so every unmatched call reaches it: `dev-test bogus` or a typo like `dev-test unti` ended the caller's Nushell session, and the comment claiming "only script mode reaches this entry point" was wrong. The `try`/`exit 1` is gone; the error propagates, which keeps module mode catchable (`try { dev-test bogus } catch { print caught }; print after` prints both lines) and still makes script mode exit 1 (`nu dev/dev-test.nu bogus` exits 1 with the unknown-subcommand message; Nushell gives an uncaught `error make` in a script status 1). The comment now says both modes land there and why `exit` must not be used. This supersedes the round-2 sentence "`main` is the only place that exits". + - `check-module-mode` no longer allows a window for `main`: any `exit` is a problem, including an inline one such as `catch { …; exit 1 }` (the scan drops comment lines, then matches `exit` at the start of a line or after `;`/`{`). The entry-point slicing (`entry`/`next`) is gone. + - New probes: module mode `try { dev-test bogus } catch { print 'caught' }; print 'after'` must print both lines with exit 0, next to the existing `vm` probe; script mode `nu dev/dev-test.nu bogus` must exit non-zero with the unknown-subcommand reason, so the "script mode still fails" guarantee is tested rather than assumed. `run-steps`' doc comment now says it raises (the process exits non-zero). + - Mutation-checked: restoring `try { … } catch { exit 1 }` in `main` fails the self-check on the `exit` line, and the module-mode `bogus` probe fails on its own. +- Handoff 2026-10-19 (WS-T review round 3 -> all): unchanged commands and subcommand names. `nu dev/dev-test.nu self-check` now fails if any line calls `exit` at all (including in `main`), if a message interpolates a call to `exit`, if a failing step or an unknown subcommand is not catchable in module mode, if `nu dev/dev-test.nu bogus` does not exit non-zero, or if the filter cache is not anchored on the repo root. Merger items from round 2 (stale `dev/test.nu` in other streams' plan files, `TODO.md` T7) stand.