Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw better error when spawning snap Chromium from another snap app #317

Merged
merged 3 commits into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

### Fixed

- Throw better error when spawning snap Chromium from another snap app ([#317](https://github.com/marp-team/marp-cli/pull/317))

## v0.23.0 - 2020-12-05

### Changed
Expand Down
1 change: 1 addition & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum CLIErrorCode {
GENERAL_ERROR = 1,
NOT_FOUND_CHROMIUM = 2,
LISTEN_PORT_IS_ALREADY_USED = 3,
CANNOT_SPAWN_SNAP_CHROMIUM = 4,
}

export function error(
Expand Down
26 changes: 22 additions & 4 deletions src/utils/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { isWSL, resolveWindowsEnv } from './wsl'
let executablePath: string | undefined | false = false
let wslTmp: string | undefined

const isSnapBrowser = (path: string | undefined) =>
!!(process.platform === 'linux' && path?.startsWith('/snap/'))

export const generatePuppeteerDataDirPath = async (
name: string,
{ wslHost }: { wslHost?: boolean } = {}
Expand Down Expand Up @@ -63,7 +66,7 @@ export const generatePuppeteerLaunchArgs = () => {
return {
executablePath,
args: [...args],
pipe: !isWSL(),
pipe: !(isWSL() || isSnapBrowser(executablePath)),

// Workaround to avoid force-extensions policy for Chrome enterprise (SET CHROME_ENABLE_EXTENSIONS=1)
// https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md#chrome-headless-doesnt-launch-on-windows
Expand All @@ -75,8 +78,8 @@ export const generatePuppeteerLaunchArgs = () => {
}
}

export const launchPuppeteer = (
...args: Parameters<typeof puppeteer['launch']>
export const launchPuppeteer = async (
...[options]: Parameters<typeof puppeteer['launch']>
) => {
const { arch } = os

Expand All @@ -88,7 +91,22 @@ export const launchPuppeteer = (
return arch()
}

return puppeteer.launch(...args)
return await puppeteer.launch(options)
} catch (e) {
if (e instanceof Error) {
if (
options?.executablePath &&
isSnapBrowser(options.executablePath) &&
/^need to run as root or suid$/im.test(e.message)
) {
error(
'Marp CLI has detected trying to spawn Chromium browser installed by snap, from the confined environment like another snap app. At least either of Chrome/Chromium or the shell environment must be non snap app.',
CLIErrorCode.CANNOT_SPAWN_SNAP_CHROMIUM
)
}
}

throw e
} finally {
os.arch = arch
}
Expand Down