diff --git a/packages/void/src/cli/auth.ts b/packages/void/src/cli/auth.ts index 76af704bb..b16ebb1f7 100644 --- a/packages/void/src/cli/auth.ts +++ b/packages/void/src/cli/auth.ts @@ -176,11 +176,38 @@ function page(title: string, message: string): string {

${title}

${message}

`; } +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 (process.env.WSL_DISTRO_NAME || process.env.WSL_INTEROP) { + 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 +266,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.'); });