-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(desktop): open SSH worktrees in local Zed #4362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DeclanRiedel
wants to merge
3
commits into
pingdotgg:main
Choose a base branch
from
DeclanRiedel:feature/zed-remote-worktree-open
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| import * as NodeServices from "@effect/platform-node/NodeServices"; | ||
| import { assert, describe, it } from "@effect/vitest"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Path from "effect/Path"; | ||
| import * as Sink from "effect/Sink"; | ||
| import * as Stream from "effect/Stream"; | ||
| import * as ChildProcess from "effect/unstable/process/ChildProcess"; | ||
| import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; | ||
|
|
||
| import * as DesktopZedLauncher from "./DesktopZedLauncher.ts"; | ||
|
|
||
| function makeDetachedHandle(onUnref: () => void): ChildProcessSpawner.ChildProcessHandle { | ||
| return ChildProcessSpawner.makeHandle({ | ||
| pid: ChildProcessSpawner.ProcessId(1), | ||
| exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)), | ||
| isRunning: Effect.succeed(true), | ||
| kill: () => Effect.void, | ||
| unref: Effect.sync(() => { | ||
| onUnref(); | ||
| return Effect.void; | ||
| }), | ||
| stdin: Sink.drain, | ||
| stdout: Stream.empty, | ||
| stderr: Stream.empty, | ||
| all: Stream.empty, | ||
| getInputFd: () => Sink.drain, | ||
| getOutputFd: () => Stream.empty, | ||
| }); | ||
| } | ||
|
|
||
| describe("DesktopZedLauncher", () => { | ||
| it("builds Zed's encoded SSH URI from the connection target", () => { | ||
| assert.equal( | ||
| DesktopZedLauncher.remoteZedSshUri({ | ||
| target: { | ||
| alias: "devbox", | ||
| hostname: "devbox.example.com", | ||
| username: "declan", | ||
| port: 2222, | ||
| }, | ||
| path: "~/code/project alpha", | ||
| }), | ||
| "ssh://declan@devbox:2222/~/code/project%20alpha", | ||
| ); | ||
| assert.equal( | ||
| DesktopZedLauncher.remoteZedSshUri({ | ||
| target: { | ||
| alias: "", | ||
| hostname: "devbox.example.com", | ||
| username: null, | ||
| port: null, | ||
| }, | ||
| path: "/srv/project", | ||
| }), | ||
| "ssh://devbox.example.com/srv/project", | ||
| ); | ||
| assert.equal( | ||
| DesktopZedLauncher.remoteZedSshUri({ | ||
| target: { | ||
| alias: "", | ||
| hostname: "2001:db8::1", | ||
| username: "declan", | ||
| port: 2222, | ||
| }, | ||
| path: "/srv/project", | ||
| }), | ||
| "ssh://declan@[2001:db8::1]:2222/srv/project", | ||
| ); | ||
| }); | ||
|
|
||
| it.effect.each<{ | ||
| availableCommands: ReadonlyArray<"zed" | "zeditor">; | ||
| expectedCommand: "zed" | "zeditor"; | ||
| }>([ | ||
| { availableCommands: ["zed", "zeditor"], expectedCommand: "zed" }, | ||
| { availableCommands: ["zeditor"], expectedCommand: "zeditor" }, | ||
| ])( | ||
| "launches and detaches $expectedCommand when available", | ||
| ({ availableCommands, expectedCommand }) => | ||
| Effect.gen(function* () { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
| const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-zed-" }); | ||
| for (const command of availableCommands) { | ||
| const commandPath = path.join(binDir, command); | ||
| yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n"); | ||
| yield* fileSystem.chmod(commandPath, 0o755); | ||
| } | ||
|
|
||
| let spawned: ChildProcess.StandardCommand | undefined; | ||
| let didUnref = false; | ||
| const spawnerLayer = Layer.succeed( | ||
| ChildProcessSpawner.ChildProcessSpawner, | ||
| ChildProcessSpawner.make((command) => | ||
| Effect.sync(() => { | ||
| assert.equal(ChildProcess.isStandardCommand(command), true); | ||
| if (!ChildProcess.isStandardCommand(command)) { | ||
| throw new Error("Expected a standard command"); | ||
| } | ||
| spawned = command; | ||
| return makeDetachedHandle(() => { | ||
| didUnref = true; | ||
| }); | ||
| }), | ||
| ), | ||
| ); | ||
| const previousPath = process.env.PATH; | ||
| process.env.PATH = binDir; | ||
|
|
||
| yield* Effect.gen(function* () { | ||
| const launcher = yield* DesktopZedLauncher.DesktopZedLauncher; | ||
| yield* launcher.openRemoteWorkspace({ | ||
| target: { | ||
| alias: "devbox", | ||
| hostname: "devbox.example.com", | ||
| username: null, | ||
| port: null, | ||
| }, | ||
| path: "/srv/project", | ||
| }); | ||
| }).pipe( | ||
| Effect.ensuring( | ||
| Effect.sync(() => { | ||
| process.env.PATH = previousPath; | ||
| }), | ||
| ), | ||
| Effect.provide( | ||
| DesktopZedLauncher.layer.pipe( | ||
| Layer.provide(Layer.merge(NodeServices.layer, spawnerLayer)), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| assert.ok(spawned); | ||
| assert.equal(spawned.command, expectedCommand); | ||
| assert.deepEqual(spawned.args, ["-r", "ssh://devbox/srv/project"]); | ||
| assert.equal(spawned.options.detached, true); | ||
| assert.equal(didUnref, true); | ||
| }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), | ||
| ); | ||
|
|
||
| it("keeps launch errors free of SSH connection details", () => { | ||
| const error = new DesktopZedLauncher.DesktopZedLaunchError({ | ||
| argumentCount: 2, | ||
| cause: new Error("spawn failed"), | ||
| }); | ||
|
|
||
| assert.equal(error.message, "Failed to open remote workspace in Zed."); | ||
| assert.equal(error.argumentCount, 2); | ||
| }); | ||
|
|
||
| it.effect("fails clearly when no local Zed CLI is available", () => | ||
| Effect.gen(function* () { | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-no-zed-" }); | ||
| const previousPath = process.env.PATH; | ||
| process.env.PATH = binDir; | ||
|
|
||
| const error = yield* Effect.gen(function* () { | ||
| const launcher = yield* DesktopZedLauncher.DesktopZedLauncher; | ||
| return yield* launcher.openRemoteWorkspace({ | ||
| target: { | ||
| alias: "devbox", | ||
| hostname: "devbox.example.com", | ||
| username: null, | ||
| port: null, | ||
| }, | ||
| path: "/srv/project", | ||
| }); | ||
| }).pipe( | ||
| Effect.flip, | ||
| Effect.ensuring( | ||
| Effect.sync(() => { | ||
| process.env.PATH = previousPath; | ||
| }), | ||
| ), | ||
| Effect.provide(DesktopZedLauncher.layer.pipe(Layer.provide(NodeServices.layer))), | ||
| ); | ||
|
|
||
| assert.equal(error._tag, "DesktopZedCommandNotFoundError"); | ||
| assert.equal(error.message, "Zed CLI not found."); | ||
| }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import { | ||
| type DesktopOpenRemoteZedInput, | ||
| type DesktopSshEnvironmentTarget, | ||
| } from "@t3tools/contracts"; | ||
| import { isCommandAvailable, resolveSpawnCommand } from "@t3tools/shared/shell"; | ||
| import * as Context from "effect/Context"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as FileSystem from "effect/FileSystem"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Path from "effect/Path"; | ||
| import * as Schema from "effect/Schema"; | ||
| import * as ChildProcess from "effect/unstable/process/ChildProcess"; | ||
| import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; | ||
|
|
||
| const ZED_COMMANDS = ["zed", "zeditor"] as const; | ||
|
|
||
| export class DesktopZedCommandNotFoundError extends Schema.TaggedErrorClass<DesktopZedCommandNotFoundError>()( | ||
| "DesktopZedCommandNotFoundError", | ||
| {}, | ||
| ) { | ||
| override get message(): string { | ||
| return "Zed CLI not found."; | ||
| } | ||
| } | ||
|
|
||
| export class DesktopZedLaunchError extends Schema.TaggedErrorClass<DesktopZedLaunchError>()( | ||
| "DesktopZedLaunchError", | ||
| { | ||
| argumentCount: Schema.Number, | ||
| cause: Schema.Defect(), | ||
| }, | ||
| ) { | ||
| override get message(): string { | ||
| return "Failed to open remote workspace in Zed."; | ||
| } | ||
| } | ||
|
|
||
| function remoteAuthority(target: DesktopSshEnvironmentTarget): string { | ||
| const rawHost = target.alias.trim() || target.hostname.trim(); | ||
| const host = rawHost.includes(":") && !rawHost.startsWith("[") ? `[${rawHost}]` : rawHost; | ||
| const username = target.username?.trim(); | ||
| const port = target.port === null ? "" : `:${target.port}`; | ||
| return `${username ? `${username}@` : ""}${host}${port}`; | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
| } | ||
|
cursor[bot] marked this conversation as resolved.
|
||
|
|
||
| function remoteUriPath(path: string): string { | ||
| const normalized = path === "~" ? "/~" : path.startsWith("~/") ? `/~/${path.slice(2)}` : path; | ||
| const absolute = normalized.startsWith("/") ? normalized : `/${normalized}`; | ||
| return absolute | ||
| .split("/") | ||
| .map((segment) => encodeURIComponent(segment)) | ||
| .join("/"); | ||
| } | ||
|
|
||
| export function remoteZedSshUri(input: DesktopOpenRemoteZedInput): string { | ||
| return `ssh://${remoteAuthority(input.target)}${remoteUriPath(input.path)}`; | ||
| } | ||
|
|
||
| export class DesktopZedLauncher extends Context.Service< | ||
| DesktopZedLauncher, | ||
| { | ||
| readonly openRemoteWorkspace: ( | ||
| input: DesktopOpenRemoteZedInput, | ||
| ) => Effect.Effect<void, DesktopZedCommandNotFoundError | DesktopZedLaunchError>; | ||
| } | ||
| >()("@t3tools/desktop/shell/DesktopZedLauncher") {} | ||
|
|
||
| export const make = Effect.gen(function* () { | ||
| const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; | ||
| const fileSystem = yield* FileSystem.FileSystem; | ||
| const path = yield* Path.Path; | ||
|
|
||
| const openRemoteWorkspace = Effect.fn("desktop.zed.openRemoteWorkspace")(function* ( | ||
| input: DesktopOpenRemoteZedInput, | ||
| ) { | ||
| let command = Option.none<string>(); | ||
| for (const candidate of ZED_COMMANDS) { | ||
| if ( | ||
| yield* isCommandAvailable(candidate).pipe( | ||
| Effect.provideService(FileSystem.FileSystem, fileSystem), | ||
| Effect.provideService(Path.Path, path), | ||
| ) | ||
| ) { | ||
| command = Option.some(candidate); | ||
| break; | ||
| } | ||
| } | ||
| if (Option.isNone(command)) { | ||
| return yield* new DesktopZedCommandNotFoundError(); | ||
| } | ||
|
|
||
| const args = ["-r", remoteZedSshUri(input)]; | ||
| const resolved = yield* resolveSpawnCommand(command.value, args).pipe( | ||
| Effect.provideService(FileSystem.FileSystem, fileSystem), | ||
| Effect.provideService(Path.Path, path), | ||
| ); | ||
| const process = ChildProcess.make(resolved.command, resolved.args, { | ||
| detached: true, | ||
| shell: resolved.shell, | ||
| stdin: "ignore", | ||
| stdout: "ignore", | ||
| stderr: "ignore", | ||
| }); | ||
| yield* spawner.spawn(process).pipe( | ||
| Effect.flatMap((handle) => handle.unref), | ||
| Effect.asVoid, | ||
| Effect.scoped, | ||
| Effect.mapError( | ||
| (cause) => | ||
| new DesktopZedLaunchError({ | ||
| argumentCount: resolved.args.length, | ||
| cause, | ||
| }), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| return DesktopZedLauncher.of({ openRemoteWorkspace }); | ||
| }); | ||
|
|
||
| export const layer = Layer.effect(DesktopZedLauncher, make); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.