Skip to content

Commit 4f7dc60

Browse files
committed
fix(server): anchor Nushell imports to the worktree
Nushell parses --commands before evaluating its leading cd. If login.nu changes directories, relative source and use statements can select files from the login directory even when the action runs in the worktree. Use Nu's mktemp, save, to nuon, source, and rm commands to load the action from a temporary .nu file in the worktree. Relative imports resolve against that file's directory, and the child retains the user's login definitions. Remove the file before the action runs so it does not appear in git status; clean it up in the parent when parsing fails. Preparation errors still report a numeric completion status. Guard null catch arguments on older Nu. Extend the existing mocked-terminal table with source/use input and assert the complete native wrapper. The expanded assertion fails before the fix. vp run check:changed passes, including 12 tests in two affected files. Runtime: 60 PTY cases and eight preparation-failure cases passed on macOS with Nushell 0.97.1, 0.98.0, 0.99.1, and 0.116.1. Login startup changes cwd; both directories contain conflicting setup.nu and module.nu files: before: source ./setup.nu -> SOURCE-WRONG, status 0 before: use ./module.nu *; location -> MODULE-WRONG, status 0 after: source ./setup.nu -> SOURCE-WORKTREE, status 0 after: use ./module.nu *; location -> MODULE-WORKTREE, status 0 file present only in login cwd -> diagnostic, status 1, no leftover exit 0 / exit 7 / error make -> statuses 0 / 7 / 1 Nested imports, login-defined commands, multiline quoting, paths with spaces and quotes, TTY input/output, and cleanup passed. Creation and save errors show diagnostics and report status 1 without running the action. Model and harness: GPT-6.1 Sol through Codex in T3 Code.
1 parent 7f9ac59 commit 4f7dc60

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

‎apps/server/src/project/ProjectSetupScriptRunner.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ describe("ProjectSetupScriptRunner", () => {
126126

127127
it.effect.each([
128128
{ command: "vp i", escaped: "vp i" },
129+
{
130+
command: "source ./setup.nu\nuse ./module.nu *",
131+
escaped: "source ./setup.nu\\u{a}use ./module.nu *",
132+
},
129133
{
130134
command: 'print "quoted"; print \'C:\\tools\'; print "$env.PATH"\nexit 7 # comment\r\n\t',
131135
escaped:
@@ -143,7 +147,14 @@ describe("ProjectSetupScriptRunner", () => {
143147
const sentinel = written.match(/__T3_SETUP_DONE___[0-9a-f]{32}:/)?.[0];
144148
expect(sentinel).toBeDefined();
145149
expect(written).toBe(
146-
` $env.LAST_EXIT_CODE = 0; try { ^$nu.current-exe --login --commands "cd $env.T3CODE_WORKTREE_PATH\\u{a}${escaped}" } catch { if $env.LAST_EXIT_CODE == 0 { $env.LAST_EXIT_CODE = 1 } }; print $"\\n${sentinel}($env.LAST_EXIT_CODE)"\r`,
150+
` ${[
151+
"$env.LAST_EXIT_CODE = 0",
152+
"let __t3f = try { mktemp --tmpdir-path $env.T3CODE_WORKTREE_PATH --suffix .nu .t3-setup-XXXXXXXX } catch {|e| print -e $e.msg; null }",
153+
`if $__t3f == null { $env.LAST_EXIT_CODE = 1 } else { try { [$"rm --permanent ($__t3f | to nuon)" "${escaped}"] | str join (char newline) | save --force --raw $__t3f; ^$nu.current-exe --login --commands $"cd $env.T3CODE_WORKTREE_PATH; source ($__t3f | to nuon)" } catch {|e| if $e != null and $e.exit_code? == null { print -e $e.msg }; if $env.LAST_EXIT_CODE == 0 { $env.LAST_EXIT_CODE = 1 } } }`,
154+
"let __t3c = $env.LAST_EXIT_CODE",
155+
"try { if $__t3f != null and ($__t3f | path exists) { rm --permanent $__t3f } } catch {|e| print -e $e.msg }",
156+
`print $"\\n${sentinel}($__t3c)"`,
157+
].join("; ")}\r`,
147158
);
148159
}),
149160
);

‎apps/server/src/project/ProjectSetupScriptRunner.ts‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,18 @@ function wrapCommandForCompletion(
190190
case "fish":
191191
return `begin\r${body}\rend; printf '\\n${sentinel}%s\\n' $status`;
192192
case "nushell": {
193-
const escaped = `cd $env.T3CODE_WORKTREE_PATH\n${command}`.replace(
194-
/["\\\x00-\x1f]/g,
195-
(char) =>
196-
char === '"' || char === "\\" ? `\\${char}` : `\\u{${char.charCodeAt(0).toString(16)}}`,
193+
const escaped = command.replace(/["\\\x00-\x1f]/g, (char) =>
194+
char === '"' || char === "\\" ? `\\${char}` : `\\u{${char.charCodeAt(0).toString(16)}}`,
197195
);
198-
// Nu's catch records vary across versions, and exit bypasses catch entirely.
199-
return `$env.LAST_EXIT_CODE = 0; try { ^$nu.current-exe --login --commands "${escaped}" } catch { if $env.LAST_EXIT_CODE == 0 { $env.LAST_EXIT_CODE = 1 } }; print $"\\n${sentinel}($env.LAST_EXIT_CODE)"`;
196+
// Nu resolves imports before running cd; source a file rooted in the worktree.
197+
return [
198+
"$env.LAST_EXIT_CODE = 0",
199+
"let __t3f = try { mktemp --tmpdir-path $env.T3CODE_WORKTREE_PATH --suffix .nu .t3-setup-XXXXXXXX } catch {|e| print -e $e.msg; null }",
200+
`if $__t3f == null { $env.LAST_EXIT_CODE = 1 } else { try { [$"rm --permanent ($__t3f | to nuon)" "${escaped}"] | str join (char newline) | save --force --raw $__t3f; ^$nu.current-exe --login --commands $"cd $env.T3CODE_WORKTREE_PATH; source ($__t3f | to nuon)" } catch {|e| if $e != null and $e.exit_code? == null { print -e $e.msg }; if $env.LAST_EXIT_CODE == 0 { $env.LAST_EXIT_CODE = 1 } } }`,
201+
"let __t3c = $env.LAST_EXIT_CODE",
202+
"try { if $__t3f != null and ($__t3f | path exists) { rm --permanent $__t3f } } catch {|e| print -e $e.msg }",
203+
`print $"\\n${sentinel}($__t3c)"`,
204+
].join("; ");
200205
}
201206
case "posix":
202207
return `( ${body}\r); printf '\\n${sentinel}%s\\n' "$?"`;

0 commit comments

Comments
 (0)