Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/shared/src/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ describe("readPathFromLaunchctl", () => {
});

describe("readEnvironmentFromLoginShell", () => {
it("uses Nushell-compatible syntax for a nu login shell", () => {
const execFile = vi.fn<
(
file: string,
args: ReadonlyArray<string>,
options: { encoding: "utf8"; timeout: number },
) => string
>(() => "__T3CODE_ENV_PATH_START__\n/a:/b\n__T3CODE_ENV_PATH_END__\n");

expect(readEnvironmentFromLoginShell("/opt/homebrew/bin/nu", ["PATH"], execFile)).toEqual({
PATH: "/a:/b",
});

const command = execFile.mock.calls[0]?.[1][1];
expect(command).toBe(
"print '__T3CODE_ENV_PATH_START__'; try { printenv PATH } catch { print '' }; print '__T3CODE_ENV_PATH_END__'",
);
expect(command).not.toContain("|| true");
});

it("extracts multiple environment variables from a login shell command", () => {
const execFile = vi.fn<
(
Expand Down
19 changes: 17 additions & 2 deletions packages/shared/src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,28 @@ function envCaptureEnd(name: string): string {
return `__T3CODE_ENV_${name}_END__`;
}

function buildEnvironmentCaptureCommand(names: ReadonlyArray<string>): string {
function isNushellExecutable(shell: string): boolean {
const executableName = NodePath.basename(shell).toLowerCase();
return executableName === "nu" || executableName === "nu.exe";
}

function buildEnvironmentCaptureCommand(shell: string, names: ReadonlyArray<string>): string {
const isNushell = isNushellExecutable(shell);

return names
.map((name) => {
if (!SHELL_ENV_NAME_PATTERN.test(name)) {
throw new Error(`Unsupported environment variable name: ${name}`);
}

if (isNushell) {
return [
`print '${envCaptureStart(name)}'`,
`try { printenv ${name} } catch { print '' }`,
`print '${envCaptureEnd(name)}'`,
].join("; ");
}

return [
`printf '%s\\n' '${envCaptureStart(name)}'`,
`printenv ${name} || true`,
Expand Down Expand Up @@ -312,7 +327,7 @@ export const readEnvironmentFromLoginShell: ShellEnvironmentReader = (
return {};
}

const output = execFile(shell, ["-ilc", buildEnvironmentCaptureCommand(names)], {
const output = execFile(shell, ["-ilc", buildEnvironmentCaptureCommand(shell, names)], {
encoding: "utf8",
timeout: 5000,
});
Expand Down
Loading