cypress: cy.exec fails on windows

Current behavior:

image

From the bug, the path seems mangled:

  1. first part is UNIXy /c/Program
  2. There’s a : inserted into the (Program Files) folder name for some reason
  3. 2nd half is in windows format (backslashes)

How to reproduce:

cy.exec('echo 42');

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.

  • Operating System: Windows 7/x64
  • Cypress Version: 1.0.2
  • Browser Version: canary 64

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 11
  • Comments: 55 (15 by maintainers)

Most upvoted comments

The issue is STILL present with Cypress v10.3.0 when using Git Bash with VS Code on windows

Found a solution that does the trick for now. I’m using cypress.task to start a task which I define in the plugins file. There, I import shell-exec, a npm package that also supports windows. Since I only need a single command for now, this works fine.

Sharing full example for clearness. cypress/plugins/index.js

// 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.

execa has decided to not offer support for cmd.exe-incompatible shells, so this looks like something we would have to manually support.

Hello,

I am also having an issue running exec in Windows 10. My git bash install path is C:\dev-tools\Git\usr\bin. When I exec cypress logs: cypress:server shell C:\dev-tools\Git\usr\bin\bash.exe profile undefined +4ms cypress:server cy.exec found shell C:\dev-tools\Git\usr\bin\bash.exe +1ms cypress:server and is running command: echo +1ms

The command fails with Stderr: /usr/bin/bash: /s: No such file or directory

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.

Switch your default terminal, in VS Code, from Bash to PowerShell and the problem is solved.

Is there any update to this issue or any workaround? I can’t seem to get cy.exec to work on Windows at all, even via VSCode or cmd. Makes it pretty difficult to use Cypress at the moment sadly.

EDIT: @selvex’s solution does work for me as well, I did something wrong when I tried it the first time. This is a reasonable workaround for the moment, but obviously it would be great for cy.exec to work on Windows too.

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

@jaffrepaul Finally got around to try this again.

I can confirm we’re still seeing the same issue in 7.4.0

image

image

I was using git bash to run cypress, when i changed to windows powershell it worked for me

How about using ShellJS?

The issue is present with Cypress v9.4.1 when using Git Bash with VS Code.

image

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.

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.

A workaround I currently use. Just utilize Windows’ command line to invoke Git Bash, since we get those Windows flags from execa.

// Setup 'bash' and 'comSpec' [environment variables](https://docs.cypress.io/guides/guides/environment-variables)

const command = 'yarn run seed';

if (Cypress.platform !== 'win32') {
  cy.exec(command);
} else {
  // This is a hack!
  const sh = `"${Cypress.env('bash')}" --login -i -c`;
  cy.exec(`${sh} "${command}"`, { env: { SHELL: Cypress.env('comSpec') } });
}

Weirdly it fails on the first run…

I am not sure because I switched to an Ubuntu operating system.