Skip to content

Commit 3a25643

Browse files
fix(contracts): Open in VS Code keeps a plus in the SSH username (pingdotgg#34578)
1 parent eceeab4 commit 3a25643

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎apps/web/src/remoteOpen.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@ describe("buildRemoteOpenUrl", () => {
186186
).toBe("vscode://vscode-remote/ssh-remote+sol/C%3A/Users/theo");
187187
});
188188

189+
it("keeps a plus in the SSH username through Remote-SSH's authority parsing", () => {
190+
const url = buildRemoteOpenUrl({
191+
editor: "vscode",
192+
host: "ssh.example.com",
193+
username: "vm+demo",
194+
absolutePath: "/workspace/project",
195+
});
196+
// VS Code decodes the authority, then Remote-SSH (0.129) takes `authority.split("+")[1]` and
197+
// reads it as hex-encoded JSON `{ hostName, user }` or as `user@host`.
198+
const authority = decodeURIComponent(new URL(url ?? "").pathname.split("/")[1] ?? "");
199+
const destination = authority.split("+")[1] ?? "";
200+
const hexBytes = /^(?:[0-9a-f]{2})+$/i.test(destination)
201+
? destination.match(/../g)?.map((byte) => Number.parseInt(byte, 16))
202+
: undefined;
203+
const resolved =
204+
hexBytes === undefined
205+
? destination.match(/^(?:(?<user>.*)@)?(?<hostName>.+?)$/)?.groups
206+
: JSON.parse(new TextDecoder().decode(new Uint8Array(hexBytes)));
207+
expect({ user: resolved?.user, hostName: resolved?.hostName }).toEqual({
208+
user: "vm+demo",
209+
hostName: "ssh.example.com",
210+
});
211+
});
212+
189213
it("drops the Windows drive letter for Zed", () => {
190214
expect(
191215
buildRemoteOpenUrl({ editor: "zed", host: "sol", absolutePath: "C:\\Users\\theo" }),

‎packages/contracts/src/editor.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,16 @@ export const buildRemoteOpenUrl = (input: {
227227
return `${scheme}://ssh/${zedDestination}${encodedZedPath}`;
228228
}
229229
const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/");
230-
const destination = input.username === undefined ? input.host : `${input.username}@${input.host}`;
230+
const plainDestination =
231+
input.username === undefined ? input.host : `${input.username}@${input.host}`;
232+
// Remote-SSH splits the decoded authority on "+", so a destination containing one must use its
233+
// hex-encoded JSON form instead.
234+
const destination = plainDestination.includes("+")
235+
? Array.from(
236+
new TextEncoder().encode(JSON.stringify({ hostName: input.host, user: input.username })),
237+
(byte) => byte.toString(16).padStart(2, "0"),
238+
).join("")
239+
: plainDestination;
231240
// VS Code opens a bare vscode-remote path as a folder; a trailing position makes it open the file.
232241
return `${scheme}://vscode-remote/ssh-remote+${encodeURIComponent(destination)}${encodedPath}${input.isFile ? ":1:1" : ""}`;
233242
};

0 commit comments

Comments
 (0)