-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
cy.exec fails on windows #789
Comments
Hmm, I wonder if the |
Hello, I am also having an issue running exec in Windows 10. My git bash install path is The command fails with I'm not sure if this is an issue with my git-bash install or the way cypress is calling it. Any help would be greatly appreciated. |
I haven't got a solution for this (yet) but I do know what the problem is.
Basically the above section in the dependency "execa" is the problem. If your platform is windows it makes the assumption that your shell is cmd and it structures the resulting command as follows:
As you can see the flags |
@GuyBransgrove Thanks for looking into this and opening the issue with execa - hopefully they get a fix in and we can update the package. |
execa has decided to not offer support for cmd.exe-incompatible shells, so this looks like something we would have to manually support. |
That's great news. Is there a solution as of yet to have this working at all on windows at all? I seem to be getting this error regardless of the shell used being on windows |
How about using ShellJS? |
This same issue: #1034 |
Found a solution that does the trick for now. Sharing full example for clearness. // This function is called when a project is opened or re-opened (e.g. due to
// the project's config changing)
require('dotenv').config();
const shell = require('shell-exec');
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on('task', {
'env': () => {
return new Promise(resolve => {
resolve(process.env);
});
},
// relevant part
'db:clean': () => {
return shell('npx gulp db:clean');
}
})
}; And in a test: it('can register a new account', () => {
cy.task('db:clean');
cy.visit('/register');
}); It returns a promise, so it is then-able. However cypress waits for the completion out-of-the-box. Hope that helps. |
The reason this issue occurs is because Git Bash sets the Since the docs don't say anything about what shell should be used in That would mean moving lines 88-92 of this cypress/packages/server/lib/util/shell.js Lines 75 to 95 in 029ed01
Tested and this fixes the issue on Windows 10. Don't believe it would change existing behavior, since the current behavior is "use cmd.exe or fail". |
That would mean we won't be able to use UNIX shell programs, which means that cross-platform tests written for UNIX target would stop working. Is this issue still outstanding though? It works for me at the moment (I've also tried using |
Would it though? I only say that because AFAIK, currently, you cannot launch Cypress from a non- The change I'm proposing would make Let me know if this is mistaken or you see a breaking change with this.
I just experienced the issue in CI using
|
Interesting. You're right, I've just tested. Previously it worked for me because I've run the test from the vscode terminal (which I have configured to use Weirdly enough, it seems that I have two
That being said, it seems this error comes down to improperly handling the path (not sure whose fault that is, but I believe it's Cypress').
|
I see that @jennifer-shehane commented in February about capturing errors with the current Cypress version and we have some confirmation (v5.10 - v6.5.0) with varying degrees of workaround effort included. Just curious, @Tobbe which version you are currently running and if you still have this issue in 7.4.0? I would assume yes given the nature of the issue thus far. The outstanding issue may just be deciding which path to take. |
@jaffrepaul Thanks for giving this issue some attention. I gave up on trying to get this to work for a while, but I could never truly let it go, I keep thinking about it :) Thanks to your nudge I'll try to get the project spun up locally again and give it another go. Hopefully I can report back with answers to your questions tomorrow |
@jaffrepaul Finally got around to try this again. I can confirm we're still seeing the same issue in 7.4.0 |
I also bumped into this issue with Cypress 8.0.0. ...
"env": {
"bash": "C:\\Program Files\\Git\\bin\\bash.exe",
"comSpec": "C:\\Windows\\system32\\cmd.exe",
export function fetchSecrets() {
cy.log(`Platform: ${Cypress.platform}`)
cy.log(`Architecture: ${Cypress.arch}`)
const command = 'pathToScript/fetch.secrets.sh secretName secretKeyvault';
if (Cypress.platform !== 'win32') {
cy.exec(command);
} else {
// This is a workaround against cy.exec not working on windows machine
// https://github.com/cypress-io/cypress/issues/789
// To make this code work you need to setup 2 cypress env variables
// - bash (pointing to the bash shell executable on your machine)
// - comSpec (pointing to cmd.exe on your machine)
cy.log('Bash path: ' + Cypress.env('bash'))
cy.log('cmd.exe path: ' + Cypress.env('comSpec'))
const sh = `"${Cypress.env('bash')}" --login -c`;
const fullCommand = `${sh} "${command}"`
//for some reason cypress cannot find shell on the first try so it throws
//this is a retry hack allowing cypress to gracefully deal with error
executeCypressCommand(fullCommand, false)
executeCypressCommand(fullCommand, true)
}
function executeCypressCommand(command: string, failOnError: boolean = true) {
cy.exec(command, {env: {SHELL: Cypress.env('comSpec')}, failOnNonZeroExit: failOnError}).then(result => {
cy.log('Exit code: ' + result.code.toString())
if (result.stdout.toString().length > 0) {
cy.log('Stdout: ' + result.stdout.toString())
}
if (result.stderr.toString().length > 0) {
cy.log('Stderr: ' + result.stderr.toString())
}
})
}
import {fetchSecrets} from './helpers/helper'
before(() => {
fetchSecrets()
}) I think there might be a room for improvement/optimisation, but I haven't explored it yet (hashtag worksonmymachinenow 🤣 ). Will try to update my comment if I will end up spending more time on it. Update: |
Switch your default terminal, in VS Code, from Bash to PowerShell and the problem is solved. |
Upgraded to Cypress |
You switched to PowerShell and you're still having the issue? |
To clarify: the error still happens when running Cypress from a Bash terminal in VS Code. It doesn't happen when using either CMD or PowerShell. But, I would assume that any terminal should work. |
There is an issue when using Bash. Yes, it needs to be resolved but switching to CMD or PowerShell solves the issue and you can do what you need to do, yes? I'm not sure why you downvoted my response. It solved the issue and you can proceed, no? Did I not provide helpful information so that someone may avoid hours of screwing with something before they realize that it's something specific to Bash? |
Although you provided a valid workaround (which I was already aware of) the problem is not really "solved". No animosity, just reporting back that this issue still happens on the latest version. |
I wasn't attempting to solve the issue for Cypress. I was attempting to provide information that would, hopefully, prevent others from wasting hours of their lives messing with this issue not realizing that it was something specific to Bash. Anyway, whatever, glad you know about the workaround. |
The issue is STILL present with Cypress v10.3.0 when using Git Bash with VS Code on windows |
Had this, fiexed by resinstalling Git Bash in "C:/Git" rather than "C:/Program Files/Git". Took some additional effort to make vscode understand where shell is now installed, but it all worked out in the end. |
@strowk worked for me as well, thanks for tip! Maybe it's about a space character in the path :/ |
This is still a problem with 10.10.0, but some how it has changed from no space a while back, to colon and then space.
I see all these work arounds, but for open source projects we really need the repo to just install and run. I'm using git bash from vs code. One other observation, from the bash terminal in vscode the path to bash is:
So shouldn't cy.exec be using the environment variables from where it is being run from? |
Just started running into this on Cypress 12.7. The |
I noticed running the example cypress-io/cypress-example-kitchensink on Microsoft Windows 11 with an out-of-the-box configuration, that the test cypress/e2e/2-advanced-examples/misc.cy.js with That should probably be documented somewhere. |
It still doesn't work well. I managed to fix it temporarily by switching to Powershell terminal in Webstorm. It worked for a week or two and after that it started to not work again. My friend has the exact same tests but he is running them on Linux and everything works just fine there. |
I am facing the same issue on Cypress 12.13.0, where cy. exec(command) fails on Github Action runs on ubuntu machine, but works locally from Machine Terminal in MAC OS. |
I'm facing the same issue on Cypress 13.6.4. When can this be solved? |
Which version of Windows and which shell / terminal window application are you using? Running https://github.com/cypress-io/cypress-example-kitchensink/blob/master/cypress/e2e/2-advanced-examples/misc.cy.js om Windows 11, I found that |
I'm using Windows 11 22H2 with Git Bash. The whole company uses the git-bash and no one uses the normal cmd (for nvm for example). So I need it to work with git-bash on Windows. |
Thanks for confirming that you are using Git Bash where it's a known issue. Looking back at the history, this issue has been open for over 6 years and different people have suggested different workarounds and solutions, although none have made their way into Cypress as a standard resolution. |
Current behavior:
From the bug, the path seems mangled:
/c/Program
:
inserted into the (Program Files
) folder name for some reasonHow to reproduce:
Additional Info (images, stack traces, etc)
On my machine is installed (and in env variables) the git-for-windows bash, which is the bash the cypress is trying to invoke.
The text was updated successfully, but these errors were encountered: