Skip to content

Commit 3af5018

Browse files
fix(server): open projects in a new VS Code window (pingdotgg#34480)
1 parent 60ec7fc commit 3af5018

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎apps/server/src/process/externalLauncher.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,46 @@ it.effect.skipIf(windowsHost)(
182182
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
183183
);
184184

185+
// A project opened from T3 must not replace the editor's last active window.
186+
it.effect.skipIf(windowsHost)(
187+
"opens a project folder in a new VS Code window and a file in the existing one",
188+
() =>
189+
Effect.gen(function* () {
190+
const fileSystem = yield* FileSystem.FileSystem;
191+
const path = yield* Path.Path;
192+
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
193+
const project = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-project-" });
194+
const codePath = path.join(binDir, "code");
195+
yield* fileSystem.writeFileString(codePath, "#!/bin/sh\n");
196+
yield* fileSystem.chmod(codePath, 0o755);
197+
const file = `${path.join(project, "file.ts")}:3:7`;
198+
199+
const spawned: ChildProcess.StandardCommand[] = [];
200+
yield* Effect.gen(function* () {
201+
const launcher = yield* ExternalLauncher.ExternalLauncher;
202+
yield* launcher.launchEditor({ editor: "vscode", cwd: project });
203+
yield* launcher.launchEditor({ editor: "vscode", cwd: file });
204+
}).pipe(
205+
Effect.provide(
206+
layerTest({
207+
platform: "linux",
208+
env: { PATH: binDir, DISPLAY: ":0" },
209+
onSpawn: (command) => spawned.push(command),
210+
}),
211+
),
212+
Effect.provideService(HostProcess.Environment, { PATH: binDir, DISPLAY: ":0" }),
213+
);
214+
215+
assert.deepEqual(
216+
spawned.map((command) => command.args),
217+
[
218+
["--new-window", project],
219+
["--goto", file],
220+
],
221+
);
222+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
223+
);
224+
185225
// Windows drive letters cannot resolve the POSIX editor stub.
186226
it.effect.skipIf(windowsHost)(
187227
"a Linux editor launch without a display takes the desktop session's display from the systemd user manager",

‎apps/server/src/process/externalLauncher.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,17 @@ function parseTargetPathAndPosition(target: string): Option.Option<TargetPathAnd
139139
function resolveCommandEditorArgs(
140140
editor: (typeof EDITORS)[number],
141141
target: string,
142+
isDirectory: boolean,
142143
): ReadonlyArray<string> {
143144
const parsedTarget = parseTargetPathAndPosition(target);
144145

145146
switch (editor.launchStyle) {
146147
case "direct-path":
147148
return [target];
148149
case "goto":
149-
return Option.isSome(parsedTarget) ? ["--goto", target] : [target];
150+
if (Option.isSome(parsedTarget)) return ["--goto", target];
151+
// Without a window flag, VS Code's window settings may replace the user's last active window.
152+
return isDirectory ? ["--new-window", target] : [target];
150153
case "line-column":
151154
return Option.match(parsedTarget, {
152155
onNone: () => [target],
@@ -485,11 +488,17 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* (
485488
});
486489
}
487490
const { command, baseArgs } = resolved.value;
491+
const isDirectory =
492+
editorDef.launchStyle === "goto" &&
493+
(yield* (yield* FileSystem.FileSystem).stat(input.cwd).pipe(
494+
Effect.map((info) => info.type === "Directory"),
495+
Effect.orElseSucceed(() => false),
496+
));
488497
return {
489498
editor: editorDef.id,
490499
target: input.cwd,
491500
command,
492-
args: [...baseArgs, ...resolveCommandEditorArgs(editorDef, input.cwd)],
501+
args: [...baseArgs, ...resolveCommandEditorArgs(editorDef, input.cwd, isDirectory)],
493502
};
494503
}
495504

0 commit comments

Comments
 (0)