Commit 6d415fe0 authored by 谢宇轩's avatar 谢宇轩

fix: ensure chromium error

parent c8b8bc46
......@@ -16,14 +16,6 @@
"articleUrlPattern": "theconversation\\.com/[\\w-]+-\\d+$",
"vaultFolder": "The Conversation",
"enabled": true
},
{
"id": "reuters-world",
"name": "路透社 World",
"homepageUrl": "https://www.reuters.com/world/",
"method": "browser",
"vaultFolder": "路透社 World",
"enabled": true
}
]
}
......@@ -189,6 +189,11 @@ function spawnBrowser(bin, opts) {
'--no-default-browser-check',
'--disable-extensions',
'--disable-gpu',
// Let Chromium auto-detect Wayland vs X11 on Linux. Without this it
// defaults to the X11 backend and crashes with "Missing X server or
// $DISPLAY" on Wayland-only sessions (common on Arch/GNOME/Sway). Harmless
// on macOS/Windows — they don't use the Ozone backend.
'--ozone-platform-hint=auto',
];
// --no-sandbox is only needed when running as root (typical in Linux/Docker
// containers, where Chromium refuses to start with the sandbox as root). On
......@@ -206,6 +211,7 @@ function spawnBrowser(bin, opts) {
detached: true,
stdio: ['ignore', 'ignore', logFd],
windowsHide,
env: { ...process.env },
});
child.on('error', (e) => {
console.error(`Chromium spawn error: ${e.message}`);
......@@ -222,6 +228,34 @@ function tailFile(filePath, lines = 25) {
} catch (e) { return '(no log file)'; }
}
// Diagnose display/X11 failures — common on Wayland-only Linux sessions where
// $DISPLAY is unset and Chromium wasn't told (or couldn't) use the Wayland
// backend. Returns a multi-line hint string.
function displayDiagnostic() {
const d = process.env.DISPLAY ? `set (${process.env.DISPLAY})` : 'unset';
const w = process.env.WAYLAND_DISPLAY ? `set (${process.env.WAYLAND_DISPLAY})` : 'unset';
const lines = [
` $DISPLAY: ${d}`,
` $WAYLAND_DISPLAY: ${w}`,
];
if (!process.env.DISPLAY && !process.env.WAYLAND_DISPLAY) {
lines.push(' ⚠️ Neither is set — this shell is not attached to a graphical session.');
lines.push(' Run --login from a terminal inside your desktop (GUI terminal), or');
lines.push(' export the variables from a graphical session, e.g.:');
lines.push(' export WAYLAND_DISPLAY=wayland-0 # Wayland');
lines.push(' export DISPLAY=:0 # X11 / XWayland');
} else if (!process.env.DISPLAY && process.env.WAYLAND_DISPLAY) {
lines.push(' (Wayland session. --ozone-platform-hint=auto is already passed; if it still');
lines.push(' fails, force Wayland by adding --ozone-platform=wayland to the browser args,');
lines.push(' or run in a GUI terminal that also exports $DISPLAY for XWayland.)');
}
return lines.join('\n');
}
function looksLikeDisplayFailure(log) {
return /Missing X server|\$DISPLAY|platform failed to initialize|ozone/i.test(log);
}
// ── Main ────────────────────────────────────────────────────────────────────
async function ensureChromium(config, portOverride) {
......@@ -248,12 +282,14 @@ async function ensureChromium(config, portOverride) {
const v = await waitForCdp(opts.cdpPort, 30000, 500);
return { port: opts.cdpPort, pid, browser: v.Browser, bin, logPath };
} catch (e) {
const logTail = tailFile(logPath);
const hint = looksLikeDisplayFailure(logTail) ? '\n' + displayDiagnostic() + '\n' : '';
throw new Error(
`Chromium did not become ready on port ${opts.cdpPort}.\n` +
` binary: ${bin}\n` +
` userData: ${opts.userDataDir}\n` +
` log: ${logPath}\n` +
` stderr tail:\n${tailFile(logPath)}`
` stderr tail:\n${logTail}${hint}`
);
}
}
......@@ -407,11 +443,15 @@ async function cmdLogin(config, flagUrl, flagPort, flagProfile) {
console.log(` profile: ${profile}`);
console.log(` log: ${logPath}`);
} catch (e) {
const logTail = tailFile(logPath);
console.error(`❌ Browser did not become ready on :${port} within 15s.`);
console.error(` binary: ${bin}`);
console.error(` profile: ${profile}`);
console.error(` log: ${logPath}`);
console.error(` stderr tail:\n${tailFile(logPath)}`);
console.error(` stderr tail:\n${logTail}`);
if (looksLikeDisplayFailure(logTail)) {
console.error(`\n ${displayDiagnostic()}`);
}
process.exit(1);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment