From ff82a05b05e76bccd539dafc72c0f957692e5806 Mon Sep 17 00:00:00 2001 From: shulaoda <165626830+shulaoda@users.noreply.github.com> Date: Wed, 6 May 2026 10:58:01 +0800 Subject: [PATCH 1/2] fix(cli): show login URL and support WSL2 browser opening --- packages/void/src/cli/auth.ts | 42 +++++++++++++++---- packages/void/src/cli/login.ts | 8 +++- packages/void/test/unit/init.test.ts | 2 +- packages/void/test/unit/login-command.test.ts | 4 +- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/packages/void/src/cli/auth.ts b/packages/void/src/cli/auth.ts index 76af704bb..e4ca639f8 100644 --- a/packages/void/src/cli/auth.ts +++ b/packages/void/src/cli/auth.ts @@ -176,11 +176,42 @@ function page(title: string, message: string): string {

${title}

${message}

`; } +function isWsl(): boolean { + return Boolean(process.env.WSL_DISTRO_NAME || process.env.WSL_INTEROP); +} + +function openBrowser(url: string, exec: typeof import('node:child_process').exec): void { + if (process.platform === 'win32') { + // `start` treats the first quoted arg as window title, pass empty title + exec(`start "" "${url}"`); + return; + } + if (process.platform === 'darwin') { + exec(`open "${url}"`); + return; + } + // Linux — including WSL2 where `xdg-open` typically has no usable handler. + // Try `wslview` (wslu), then `cmd.exe /c start`, then fall back to `xdg-open`. + if (isWsl()) { + exec(`wslview "${url}" 2>/dev/null || cmd.exe /c start "" "${url}" 2>/dev/null`); + return; + } + exec(`xdg-open "${url}"`); +} + /** * Start a local HTTP server, open the browser to the platform login page, * and wait for the OAuth callback with a JWT token. + * + * `onUrl` is invoked with the login URL once the local server is listening, + * so callers can show the URL to the user as a manual fallback when the + * browser fails to open (common on WSL2, headless SSH, Docker, etc.). */ -export async function runLogin(root: string = process.cwd(), provider = 'github'): Promise { +export async function runLogin( + root: string = process.cwd(), + provider = 'github', + onUrl?: (url: string) => void, +): Promise { const http = await import('node:http'); const { exec } = await import('node:child_process'); @@ -239,13 +270,8 @@ export async function runLogin(root: string = process.cwd(), provider = 'github' const redirect = `http://localhost:${port}/callback`; const loginUrl = `${apiUrl}/auth/login?redirect=${encodeURIComponent(redirect)}&provider=${provider}`; - // Open browser with platform detection - if (process.platform === 'win32') { - // `start` treats the first quoted arg as window title, pass empty title - exec(`start "" "${loginUrl}"`); - } else { - exec(`${process.platform === 'darwin' ? 'open' : 'xdg-open'} "${loginUrl}"`); - } + onUrl?.(loginUrl); + openBrowser(loginUrl, exec); }); // 5-minute timeout diff --git a/packages/void/src/cli/login.ts b/packages/void/src/cli/login.ts index 2450cbc67..a5005fa2c 100644 --- a/packages/void/src/cli/login.ts +++ b/packages/void/src/cli/login.ts @@ -8,7 +8,7 @@ import { isStagingMode, } from './auth.ts'; import { isExpiredTokenError, PlatformClient } from './client.ts'; -import { cliTitle, createSpinner } from './output.ts'; +import { cliTitle, createSpinner, pc } from './output.ts'; export async function promptForLoginToken(root: string = process.cwd()): Promise { const provider = await select({ @@ -29,7 +29,11 @@ export async function promptForLoginToken(root: string = process.cwd()): Promise s.start(`Opening browser for ${providerName} login...`); try { - const token = await runLogin(root, provider); + const token = await runLogin(root, provider, (url) => { + s.message( + `Waiting for ${providerName} login. If your browser didn't open, visit:\n${pc.cyan(url)}`, + ); + }); saveToken(token, root); s.stop('Logged in!'); return token; diff --git a/packages/void/test/unit/init.test.ts b/packages/void/test/unit/init.test.ts index bf23c8980..db2626447 100644 --- a/packages/void/test/unit/init.test.ts +++ b/packages/void/test/unit/init.test.ts @@ -1908,7 +1908,7 @@ export default config; expect(mockConfirm.mock.calls[1]?.[0]).toMatchObject({ message: 'Set up your Void project now? (log in + link project)', }); - expect(mockRunLogin).toHaveBeenCalledWith(root, 'github'); + expect(mockRunLogin).toHaveBeenCalledWith(root, 'github', expect.any(Function)); expect(mockSaveToken).toHaveBeenCalledWith('token_123', root); expect(mockListProjects).toHaveBeenCalledOnce(); expect(mockCreateProject).toHaveBeenCalledWith('my-live-app'); diff --git a/packages/void/test/unit/login-command.test.ts b/packages/void/test/unit/login-command.test.ts index e9e9f6349..d78a24832 100644 --- a/packages/void/test/unit/login-command.test.ts +++ b/packages/void/test/unit/login-command.test.ts @@ -111,7 +111,7 @@ describe('runLoginCommand', () => { expect(mockRemoveToken).toHaveBeenCalledTimes(1); expect(mockWarn).toHaveBeenCalledWith('Stored auth token expired. Starting a new login.'); - expect(mockRunLogin).toHaveBeenCalledWith(root, 'github'); + expect(mockRunLogin).toHaveBeenCalledWith(root, 'github', expect.any(Function)); expect(mockSaveToken).toHaveBeenCalledWith('fresh-token', root); expect(mockSpinnerStop).toHaveBeenCalledWith('Logged in!'); }); @@ -128,7 +128,7 @@ describe('runLoginCommand', () => { expect(mockWarn).toHaveBeenCalledWith( '`VOID_TOKEN` is invalid or expired. Continuing with a new login.', ); - expect(mockRunLogin).toHaveBeenCalledWith(root, 'github'); + expect(mockRunLogin).toHaveBeenCalledWith(root, 'github', expect.any(Function)); expect(mockSaveToken).toHaveBeenCalledWith('fresh-token', root); expect(mockOutro).toHaveBeenCalledWith('Token saved. Unset `VOID_TOKEN` to use it.'); }); From dbf26c4e222088af97dd881ac43cfaae2681612c Mon Sep 17 00:00:00 2001 From: shulaoda <165626830+shulaoda@users.noreply.github.com> Date: Wed, 6 May 2026 11:00:05 +0800 Subject: [PATCH 2/2] u --- packages/void/src/cli/auth.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/void/src/cli/auth.ts b/packages/void/src/cli/auth.ts index e4ca639f8..b16ebb1f7 100644 --- a/packages/void/src/cli/auth.ts +++ b/packages/void/src/cli/auth.ts @@ -176,10 +176,6 @@ function page(title: string, message: string): string {

${title}

${message}

`; } -function isWsl(): boolean { - return Boolean(process.env.WSL_DISTRO_NAME || process.env.WSL_INTEROP); -} - function openBrowser(url: string, exec: typeof import('node:child_process').exec): void { if (process.platform === 'win32') { // `start` treats the first quoted arg as window title, pass empty title @@ -192,7 +188,7 @@ function openBrowser(url: string, exec: typeof import('node:child_process').exec } // Linux — including WSL2 where `xdg-open` typically has no usable handler. // Try `wslview` (wslu), then `cmd.exe /c start`, then fall back to `xdg-open`. - if (isWsl()) { + if (process.env.WSL_DISTRO_NAME || process.env.WSL_INTEROP) { exec(`wslview "${url}" 2>/dev/null || cmd.exe /c start "" "${url}" 2>/dev/null`); return; }