cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.97k stars 3.18k forks source link

Unable to start Cypress with ELECTRON_RUN_AS_NODE=1 #23636

Open sillicon opened 2 years ago

sillicon commented 2 years ago

Current behavior

If you set env with ELECTRON_RUN_AS_NODE=1, with MacOS, you are not able to start the Cypress with

ELECTRON_RUN_AS_NODE=1 npx cypress run

You'll get error: Error: Cannot find module '/Users/<username>/Library/Caches/Cypress/10.6.0/Cypress.app/Contents/MacOS/Contents/Resources/app/index.js'

Checking the code, it seems that here is where it went wrong: https://github.com/cypress-io/cypress/blob/b615e8ac30b681b4dc8485afe9814ff453da1fbf/cli/lib/exec/spawn.js#L175

The correct path, at least for Mac, should be:

path.join(state.getBinaryPkgPath(path.join(executable, '..', '..', '..')), '..', 'index.js');

Desired behavior

Cypress should start correctly

Test code to reproduce

Try run with

ELECTRON_RUN_AS_NODE=1 npx cypress run

Cypress Version

10.6.0

Node version

16.0

Operating System

macOS

Debug Logs

No response

Other

Cypress 10.7.0 (latest) also has this problem

lmiller1990 commented 2 years ago

I got the same problem on MacOS. It's different on Linux:

$ ELECTRON_RUN_AS_NODE=1 yarn cypress run                                                                         main
yarn run v1.22.11
warning package.json: No license field
$ /home/lachlan/code/dump2/rhythm/node_modules/.bin/cypress run
Cannot find module 'electron'
Require stack:
- /home/lachlan/.cache/Cypress/10.7.0/Cypress/resources/app/packages/server/lib/makeDataContext.js
- /home/lachlan/.cache/Cypress/10.7.0/Cypress/resources/app/packages/server/lib/modes/index.js
- /home/lachlan/.cache/Cypress/10.7.0/Cypress/resources/app/packages/server/lib/cypress.js
- /home/lachlan/.cache/Cypress/10.7.0/Cypress/resources/app/packages/server/index.js
- /home/lachlan/.cache/Cypress/10.7.0/Cypress/resources/app/index.js
Error: Cannot find module 'electron'
Require stack:

Seems similar to #17412.

Looking at the docs...

Using the ELECTRON_RUN_AS_NODE=1 env var is experimental and not fully tested, so may not work in all environments.

To figure out the priority on fixing this bug - do you have a specific use case in mind where you need to launch with ELECTRON_RUN_AS_NODE?

sillicon commented 2 years ago

@lmiller1990 thanks for the reply - I'm just exploring possibilities to get rid of xvfb (seems impossible because Cypress smoke check relies on it) since it is causing issue in my environment and by chance found this bug. To me it is more like a low hanging fruit, just some path fixes so would be nice if it can be fixed in next major release.

lmiller1990 commented 2 years ago

I tried your fix but it wasn't working as is - if you want to verify the path is correct and the fix works, a PR is most welcome! You might end up hitting the same error we are having on Linux. If you do get something working, happy to give it a test and get it merged up.

Can you describe your environment more, and what about it requires ELECTRON_RUN_AS_NODE? Are you running on a headless MacOS CI or something like this?

sillicon commented 2 years ago

Ha, I just realized that actually your error message (Cannot find module 'electron') is the next error you'll encounter once you fixed the startScriptPath in MacOS. So it seems that the startScriptPath is actually correct on Linux, but needs some special treatment on MacOS. But I'm still not sure on win32 though, but I expect to be working similar to Linux. For now I haven't had some time to check if I can fix error Cannot find module 'electron' or not...

Can you describe your environment more, and what about it requires ELECTRON_RUN_AS_NODE? Are you running on a headless MacOS CI or something like this?

My CI environment is actually Linux, and Linux actually directly gave me that Cannot find module 'electron' error. I'm just testing locally on my Mac and found this. But yes I'm running with --browser chrome --headless with ELECTRON_RUN_AS_NODE=1.

lmiller1990 commented 2 years ago

Yeah I was expecting you'd run into the same problem w.r.t electron.

Maybe it was broken in the 9->10 update, possibly here: https://github.com/cypress-io/cypress/pull/20932/files#r843424957

lmiller1990 commented 2 years ago

Hmm we only specify it once as a devDependency: https://github.com/cypress-io/cypress/blob/develop/package.json#L146.

I built the binary for both Cypress 9 & 10 though, and looked in there - neither has electron as a direct dependency.

We use electron-builder to build the binary app. I am not clear on why electron cannot be resolved in Cypress 10, but it runs fine with ELECTRON_RUN_AS_NODE=1 in Cypress 9. I may need to spend some more time digging, but as this is an experimental feature I cannot promise a solution in the near future.

If you'd like to take a stab, I can def point you at the right places in the repo -- feel free to ask questions, I'll do my best to answer.

RobertBleyl commented 2 years ago

We are having the exact same issue. This prevents us from using the latest version of Cypress in our CI environment. Is this issue still planned to be fixed?

lmiller1990 commented 1 year ago

Hi @RobertBleyl, sorry for the slow reply, was out of office in Oct.

We should fix this, there's a bit of info here, but looks like it still needs some work. As an aside, do you mind sharing your use case for this variable? Maybe there's a better way to work around this.

If you are interested in trying to fix it, happy to help guide you around the code base. Otherwise, it'll need to wait for someone to pick it up (internal or external).

XiaolongLyft commented 1 year ago

@lmiller1990 please take a look at this PR: #24557. It doesn't do much, but should unblock users from running ELECTRON_RUN_AS_NODE=1 with some browsers.

lmiller1990 commented 1 year ago

Took a look @XiaolongLyft, thanks. Posted a comment, if anyone else is interested in this patch please follow the PR or post the OS/env you want a binary for, I can build it and we can give it a test.

codesuman commented 1 year ago

Looks this issue is not yet resolved. Am getting same exact errors in Mac (Dev Env) & in Linux (Jenkins CI). Any update @lmiller1990 ? TIA.

lmiller1990 commented 1 year ago

I will rebase https://github.com/cypress-io/cypress/pull/24557 and try building a binary you can test. Edit: pushed to this branch, please wait a little. https://github.com/cypress-io/cypress/compare/lmiller-xiaolong/issue-23636?expand=1

lmiller1990 commented 1 year ago

@codesuman can you go to https://github.com/cypress-io/cypress/commit/310cf970bd77fbb6784294e7b6cb70ad732e85e6#comments and try one of the pre-releases binaries? It's the current version of Cypress + this fix. Let me know if this works for you. If it does, we can get it merged and it'll go in the next release.

lmiller1990 commented 1 year ago

Actually, looks like we still have the dep problem:

Cypress failed to start.

This may be due to a missing library or dependency. https://on.cypress.io/required-dependencies

Please refer to the error below for more details.

----------

/Users/lachlanmiller/Library/Caches/Cypress/beta-12.2.0-lmiller/ELECTRON_RUN_AS_NODE-310cf970/Cypress.app/Contents/MacOS/Cypress: bad option: --no-sandbox
/Users/lachlanmiller/Library/Caches/Cypress/beta-12.2.0-lmiller/ELECTRON_RUN_AS_NODE-310cf970/Cypress.app/Contents/MacOS/Cypress: bad option: --smoke-test
/Users/lachlanmiller/Library/Caches/Cypress/beta-12.2.0-lmiller/ELECTRON_RUN_AS_NODE-310cf970/Cypress.app/Contents/MacOS/Cypress: bad option: --ping=921

----------

Platform: darwin-arm64 (21.6.0)
Cypress Version: 12.2.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
$
lmiller1990 commented 1 year ago

It says missing dependency, but this might be a red herring.

lsantos10 commented 1 year ago

I'm facing this same problem on WSL 2. As soon as I add the ELECTRON_RUN_AS_NODE=1 option for the tests fail to execute with the DEBUG log as:

/home/user/.cache/Cypress/12.7.0/Cypress/Cypress: bad option: --ping=188
    at makeError (/home/user/ProjectPath/node_modules/execa/lib/error.js:59:11)
    at handlePromise (/home/user/ProjectPath/node_modules/execa/index.js:114:26)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  shortMessage: 'Command failed with exit code 9: /home/user/.cache/Cypress/12.7.0/Cypress/Cypress --no-sandbox --smoke-test --ping=188',
  command: '/home/user/.cache/Cypress/12.7.0/Cypress/Cypress --no-sandbox --smoke-test --ping=188',
  exitCode: 9,
  signal: undefined,
  signalDescription: undefined,
  stdout: '',
  stderr: '/home/user/.cache/Cypress/12.7.0/Cypress/Cypress: bad option: --no-sandbox\n' +
    '/home/user/.cache/Cypress/12.7.0/Cypress/Cypress: bad option: --smoke-test\n' +
    '/home/user/.cache/Cypress/12.7.0/Cypress/Cypress: bad option: --ping=188',
  failed: true,
  timedOut: false,
  isCanceled: false,
  killed: false

As soon as I remove the flag the tests start running

MikeMcC399 commented 1 year ago

I can get around the issue mentioned by @lsantos10 ^^ if I run npx cypress verify in CI first without ELECTRON_RUN_AS_NODE: 1, however it still doesn't work.

After running npx cypress verify first, I then see a different error message on Ubuntu 22.04 / Cypress 12.13.0 / Node.js 18.16.0

"Error: Cannot find module './start-cypress'"

Edit: Still reproducible on Cypress 13.4.0 with Node.js 18.18.2.

See repo https://github.com/MikeMcC399/cy-xvfb-test

Workflow chrome_as_node.yml

Run npx cypress run --browser chrome
  npx cypress run --browser chrome
  shell: /usr/bin/bash -e {0}
  env:
    ELECTRON_RUN_AS_NODE: 1
Error: Cannot find module './start-cypress'
Require stack:
- /home/runner/.cache/Cypress/12.13.0/Cypress/resources/app/packages/server/index.jsc
    at Module._resolveFilename (node:internal/modules/cjs/loader:940:15)
    at Module._load (node:internal/modules/cjs/loader:785:27)
    at c._load (node:electron/js2c/asar_bundle:5:13339)
    at Module.require (node:internal/modules/cjs/loader:1012:19)
    at i (/home/runner/.cache/Cypress/12.13.0/Cypress/resources/app/index.js:1:2232)
    at startCypress (evalmachine.<anonymous>:1:770705)
    at evalmachine.<anonymous>:1:770965
    at v (/home/runner/.cache/Cypress/12.13.0/Cypress/resources/app/index.js:1:2573)
    at Object.<anonymous> (/home/runner/.cache/Cypress/12.13.0/Cypress/resources/app/index.js:1:2764)
    at Module._compile (node:internal/modules/cjs/loader:1120:14) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/runner/.cache/Cypress/12.13.0/Cypress/resources/app/packages/server/index.jsc'
  ]
}
Error: Process completed with exit code 1.
hanna-becker commented 1 year ago

After running npx cypress verify first, I then see a different error message on Ubuntu 22.04 / Cypress 12.13.0 / Node.js 18.16.0 "Error: Cannot find module './start-cypress'"

Same here. Running on cypress v12.14.0.

NicoForce commented 1 year ago

After running npx cypress verify first, I then see a different error message on Ubuntu 22.04 / Cypress 12.13.0 / Node.js 18.16.0 "Error: Cannot find module './start-cypress'"

Same error message on Ubuntu 20.04.1 LTS / Cypress 12.16.0 / Node.js 18.14.2.

Is there an easy way to overcome this issue? xvfb is causing issues on my ci pipeline.

MikeMcC399 commented 8 months ago