Skip to content

Commit 427bbd2

Browse files
committed
Enhance browser launching with WSL support and detailed error logging
- in wsl, browser is correctly downloaded but it never runs
1 parent c4aa5ed commit 427bbd2

1 file changed

Lines changed: 93 additions & 5 deletions

File tree

‎apps/editor/src/views/prompt/backend/message-handlers/handle-run-browser.ts‎

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,115 @@ import {
1111
} from '@puppeteer/browsers'
1212
import { t } from '@/i18n'
1313

14+
const is_wsl = () =>
15+
process.platform === 'linux' &&
16+
(vscode.env.remoteName === 'wsl' || /microsoft/i.test(os.release()))
17+
18+
const get_log_tail = (log_path: string) => {
19+
try {
20+
return fs
21+
.readFileSync(log_path, 'utf-8')
22+
.trim()
23+
.split('\n')
24+
.slice(-5)
25+
.join('\n')
26+
} catch {
27+
return ''
28+
}
29+
}
30+
31+
const show_launch_error = (reason: string, log_path: string) => {
32+
const log_tail = get_log_tail(log_path)
33+
const hint = /shared libraries/i.test(log_tail)
34+
? '\nSystem libraries required by Chrome are missing. Install them, e.g. on Ubuntu/Debian: sudo apt install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libasound2 (libasound2t64 on newer releases).'
35+
: ''
36+
vscode.window.showErrorMessage(
37+
`Failed to launch Browser: ${reason}${hint}\nLog: ${log_path}${
38+
log_tail ? `\n${log_tail}` : ''
39+
}`
40+
)
41+
}
42+
1443
const launch_browser = (
1544
executable_path: string,
1645
user_data_dir: string,
1746
extension_path: string
18-
) => {
47+
): Promise<void> => {
1948
const args = [
2049
`--user-data-dir=${user_data_dir}`,
2150
`--load-extension=${extension_path}`
2251
]
52+
const env: NodeJS.ProcessEnv = { ...process.env }
2353

2454
if (process.platform === 'darwin') {
2555
args.push('--use-mock-keychain')
2656
} else if (process.platform === 'linux') {
2757
args.push('--password-store=basic')
58+
59+
if (is_wsl()) {
60+
args.push('--no-sandbox', '--disable-dev-shm-usage')
61+
env.DISPLAY ??= ':0'
62+
env.WAYLAND_DISPLAY ??= 'wayland-0'
63+
env.XDG_RUNTIME_DIR ??= '/mnt/wslg/runtime-dir'
64+
}
2865
}
2966

30-
const child = spawn(executable_path, args, {
31-
detached: true,
32-
stdio: 'ignore'
67+
const log_path = path.join(os.homedir(), '.cwc', 'browser.log')
68+
69+
return new Promise<void>((resolve) => {
70+
let settled = false
71+
let timer: NodeJS.Timeout | undefined
72+
73+
const settle = () => {
74+
if (settled) return
75+
settled = true
76+
if (timer) clearTimeout(timer)
77+
resolve()
78+
}
79+
80+
let log_fd: number
81+
try {
82+
log_fd = fs.openSync(log_path, 'w')
83+
} catch {
84+
log_fd = -1
85+
}
86+
87+
try {
88+
const child = spawn(executable_path, args, {
89+
detached: true,
90+
stdio: log_fd >= 0 ? ['ignore', log_fd, log_fd] : 'ignore',
91+
env
92+
})
93+
94+
child.on('error', (error) => {
95+
if (settled) return
96+
show_launch_error(error.message, log_path)
97+
settle()
98+
})
99+
100+
child.on('exit', (code, signal) => {
101+
if (settled) return
102+
if (code !== 0) {
103+
show_launch_error(
104+
`process exited early (${signal ? `signal ${signal}` : `code ${code}`})`,
105+
log_path
106+
)
107+
}
108+
settle()
109+
})
110+
111+
timer = setTimeout(() => {
112+
// Still running after 3s, so startup succeeded
113+
child.unref()
114+
settle()
115+
}, 3000)
116+
} catch (error: any) {
117+
show_launch_error(error.message, log_path)
118+
settle()
119+
} finally {
120+
if (log_fd >= 0) fs.closeSync(log_fd)
121+
}
33122
})
34-
child.unref()
35123
}
36124

37125
export const handle_run_browser = async () => {

0 commit comments

Comments
 (0)