Skip to content

Commit 39ca9de

Browse files
committed
fix(ssh): load remote profiles with the account login shell
1 parent a8dba9c commit 39ca9de

2 files changed

Lines changed: 99 additions & 9 deletions

File tree

‎packages/ssh/src/tunnel.test.ts‎

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { assert, describe, it } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import * as NetService from "@t3tools/shared/Net";
4+
import * as HostProcess from "@t3tools/shared/HostProcess";
45
import * as Deferred from "effect/Deferred";
56
import * as Duration from "effect/Duration";
67
import * as Effect from "effect/Effect";
78
import * as Fiber from "effect/Fiber";
9+
import * as FileSystem from "effect/FileSystem";
810
import * as Layer from "effect/Layer";
911
import * as PlatformError from "effect/PlatformError";
1012
import * as Result from "effect/Result";
@@ -111,23 +113,103 @@ describe("ssh tunnel scripts", () => {
111113
username: "julius",
112114
port: 2222,
113115
} as const;
114-
const spawnedCommands: Array<ReadonlyArray<string>> = [];
115-
const spawner = ChildProcessSpawner.make((command) =>
116-
Effect.sync(() => {
117-
spawnedCommands.push(commandArgs(command));
118-
return makeSuccessfulProcess('loaded nvm default\n{"remotePort":3774}\n');
119-
}),
116+
const spawner = ChildProcessSpawner.make(() =>
117+
Effect.succeed(makeSuccessfulProcess('loaded nvm default\n{"remotePort":3774}\n')),
120118
);
121119
const layerSpawner = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner);
122120
const layerProcess = Layer.merge(NodeServices.layer, layerSpawner);
123121

124122
return Effect.gen(function* () {
125123
const result = yield* SshTunnel.launchOrReuseRemoteServer(target, undefined, ARCHIVE);
126124
assert.equal(result.remotePort, 3774);
127-
assert.deepEqual(spawnedCommands[0]?.slice(-5, -1), ["sh", "-l", "-s", "--"]);
128125
}).pipe(Effect.provide(layerProcess));
129126
});
130127

128+
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
129+
"loads login settings without changing the POSIX bootstrap or its state key",
130+
() =>
131+
Effect.gen(function* () {
132+
const fs = yield* FileSystem.FileSystem;
133+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-ssh-login-" });
134+
const startupFile = `${directory}/bash-env`;
135+
// BASH_ENV checks login mode without replacing or relying on a user's profile.
136+
yield* fs.writeFileString(
137+
startupFile,
138+
"if shopt -q login_shell; then export T3_SSH_LOGIN_ENV=loaded; fi\n",
139+
);
140+
const target = { alias: "login-test", hostname: "login-test", username: null, port: null };
141+
let remoteCommand = "";
142+
const capture = ChildProcessSpawner.make((command) =>
143+
Effect.sync(() => {
144+
const args = commandArgs(command);
145+
remoteCommand = args.slice(args.indexOf(target.alias) + 1).join(" ");
146+
return makeSuccessfulProcess('{"remotePort":3774}\n');
147+
}),
148+
);
149+
yield* SshTunnel.launchOrReuseRemoteServer(target, undefined, ARCHIVE).pipe(
150+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, capture),
151+
);
152+
yield* fs.writeFileString(
153+
`${directory}/.zprofile`,
154+
"if [[ -o login ]]; then export T3_SSH_LOGIN_ENV=loaded; fi\n",
155+
);
156+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
157+
const cases = [
158+
{ shell: "/bin/bash", outerShell: "/bin/sh", expected: "loaded" },
159+
{ shell: "/bin/sh", outerShell: "/bin/sh", expected: "missing" },
160+
{ shell: "", outerShell: "/bin/sh", expected: "missing" },
161+
{ shell: "/unavailable/csh", outerShell: "/bin/sh", expected: "missing" },
162+
{ shell: "/unavailable/tcsh", outerShell: "/bin/sh", expected: "missing" },
163+
];
164+
if (yield* fs.exists("/bin/zsh")) {
165+
cases.push({ shell: "/bin/zsh", outerShell: "/bin/zsh", expected: "loaded" });
166+
}
167+
if (yield* fs.exists("/bin/tcsh")) {
168+
cases.push({ shell: "/bin/tcsh", outerShell: "/bin/tcsh", expected: "missing" });
169+
}
170+
for (const { shell, outerShell, expected } of cases) {
171+
const child = yield* spawner.spawn(
172+
ChildProcess.make(outerShell, ["-c", remoteCommand], {
173+
env: {
174+
SHELL: shell,
175+
BASH_ENV: startupFile,
176+
ZDOTDIR: directory,
177+
T3_SSH_LOGIN_ENV: "",
178+
},
179+
stdin: {
180+
stream: Stream.make(
181+
new TextEncoder().encode('printf "%s:%s" "${T3_SSH_LOGIN_ENV:-missing}" "$1"'),
182+
),
183+
endOnDone: true,
184+
},
185+
}),
186+
);
187+
const [stdout, stderr, exitCode] = yield* Effect.all(
188+
[
189+
child.stdout.pipe(
190+
Stream.decodeText(),
191+
Stream.runFold(
192+
() => "",
193+
(a, b) => a + b,
194+
),
195+
),
196+
child.stderr.pipe(
197+
Stream.decodeText(),
198+
Stream.runFold(
199+
() => "",
200+
(a, b) => a + b,
201+
),
202+
),
203+
child.exitCode,
204+
],
205+
{ concurrency: "unbounded" },
206+
);
207+
assert.equal(Number(exitCode), 0, stderr);
208+
assert.match(stdout, new RegExp(`^${expected}:[a-f0-9]{16}$`), shell || "unset SHELL");
209+
}
210+
}).pipe(Effect.provide(NodeServices.layer)),
211+
);
212+
131213
it.effect("accepts pretty-printed pairing JSON after remote shell startup noise", () => {
132214
const target = {
133215
alias: "devbox",
@@ -297,7 +379,7 @@ describe("ssh tunnel scripts", () => {
297379
}
298380
return tunnel;
299381
}
300-
if (args.some((arg, index) => arg === "-s" && args[index + 1] === "--")) {
382+
if (args.includes("-c")) {
301383
if (isTarget) {
302384
launches += 1;
303385
remoteRunning = true;

‎packages/ssh/src/tunnel.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,15 @@ export const launchOrReuseRemoteServer = Effect.fn("ssh/tunnel.launchOrReuseRemo
887887
stateKey,
888888
});
889889
const result = yield* runSshCommand(target, {
890-
remoteCommandArgs: ["sh", "-l", "-s", "--", stateKey],
890+
// Read login profiles with their own shell, then keep the bootstrap POSIX.
891+
// C shells cannot combine login mode with a command, so retain their sh fallback.
892+
remoteCommandArgs: [
893+
"sh",
894+
"-c",
895+
shellSingleQuote(
896+
`case "\${SHELL##*/}" in csh|tcsh) exec sh -l -s -- ${stateKey} ;; *) exec "\${SHELL:-/bin/sh}" -l -c ${shellSingleQuote(`exec sh -s -- ${stateKey}`)} ;; esac`,
897+
),
898+
],
891899
stdin: buildRemoteLaunchScript(runner),
892900
timeoutMs: isNodeScriptRunner(runner)
893901
? REMOTE_LAUNCH_TIMEOUT_MS

0 commit comments

Comments
 (0)