Skip to content

Commit

Permalink
Merge pull request #317 from marp-team/better-error-for-snap-environment
Browse files Browse the repository at this point in the history
Throw better error when spawning snap Chromium from another snap app
  • Loading branch information
yhatt authored Feb 6, 2021
2 parents 270e214 + 1fd6bc2 commit 3957bd4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
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

0 comments on commit 3957bd4

Please sign in to comment.