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

Cypress cannot write to the cache directory due to file permissions in Bitbucket pipeline #15025

Closed
pieterjandesmedt opened this issue Feb 10, 2021 · 12 comments
Labels
stale no activity on this issue for a long period

Comments

@pieterjandesmedt
Copy link

Current behavior

When running npm install in bitbucket pipelines, this happens:

> cypress@6.4.0 postinstall /opt/atlassian/pipelines/agent/build/node_modules/cypress
> node index.js --exec install
Cypress cannot write to the cache directory due to file permissions
See discussion and possible solutions at
https://github.com/cypress-io/cypress/issues/1281
----------
Failed to access /root/.cache/Cypress:
EACCES: permission denied, mkdir '/root/.cache/Cypress'
----------

Desired behavior

Don't write to the root directory. This should be fixed since 4.6.0, according to #1281

Test code to reproduce

Create a Bitbucket repository, enable pipelines, do npm i in the Bitbucket pipeline.

Versions

6.4.0

@maxsuelmarinho
Copy link

Hi @pieterjandesmedt

I solved this by adding unset NPM_CONFIG_USER into my bitbucket-pipelines.yml before the npm ci command. I found a similar issue here and that solution worked for me

- step:
      name: Install dependencies
      caches:
        - npm
        - cypress
        - node
      script:
        - unset NPM_CONFIG_USER
        - npm ci

@n1ngu
Copy link

n1ngu commented Jul 9, 2021

I reproduced this with a local node:alpine docker image, so this has nothing to do with Bitbucket Pipelines (but I found this issue there as well). I think it is more related to the default usage of the root user in docker containers, because sudoing into the "node" user fixes the issue.

@n1ngu
Copy link

n1ngu commented Jul 9, 2021

@maxsuelmarinho that wasn't working for me, but this might be due to my usage of npm@7

I also tried exporting CYPRESS_CACHE_FOLDER=/tmp/foo-cypress-bar but then it simply fails due to lack of permissions on this other folder. But this is not a permission issue because

  1. running the mkdir instructions in the shell does work (obviously)
  2. I am root! I can do literally anything!

Honestly I don't see the point for being that picky with the cache. I'd expect it to be disabled if it didn't work, not to fail the whole npm installation.

Can the cache be somehow manually disabled? The whole point for this package is to download a binary into that folder so it makes sense to fail if it is not writable, it's just that the writable check is not done properly.

@n1ngu
Copy link

n1ngu commented Jul 9, 2021

Downgrading to npm@6 also solves the problem. Therefore this issue is a combination of using npm@7 and the root user.

@n1ngu
Copy link

n1ngu commented Jul 9, 2021

For the record, this reproduces in Cypress version 7.7.0

@n1ngu
Copy link

n1ngu commented Jul 9, 2021

Ok, the final trigger that puzzled me is the working directory being owned by a different user. I think this instruction

docker run --rm --workdir=/mnt/cypress --user=root node:14-alpine sh -c 'npm install --global npm@7 && chown --recursive node:node . && npm install cypress'

or

docker run --rm --workdir=/home/node --user=root node:14-alpine sh -c 'npm install --global npm@7 && npm install cypress'

are the minimal reproduction of the issue. Also reproduces with node-16 and non-alpine images.

Based on this, I'd hold to the hypothesis that permissions are improperly checked. Also opens the following workarounds:

  1. Downgrade to npm@6
  2. Sudo into the working dir owner
  3. Chown the working dir to root

cc/ @pieterjandesmedt but notice the bitbucket-pipeline constrained docker-in-docker engine won't let you chown repository files and everything belongs to a spooky 65534 UID/GID. That is the linux "nobody" user and sudoing into it might unleash all kinds of other problems (no writable home folder and whatnot) that will require more and more workarounds.

@n1ngu
Copy link

n1ngu commented Jul 11, 2021

Another workaround: depending on your workflow, exporting CYPRESS_INSTALL_BINARY=0 will lead to a successful installation without the cypress binary. See https://docs.cypress.io/guides/getting-started/installing-cypress#Skipping-installation

You can later try to npx cypress install but it will fail because of the same issue.

forresttanaka added a commit to IGVF-DACC/igvf-ui that referenced this issue Mar 8, 2022
Downgrading node in case cypress-io/cypress#15025 is correct.
forresttanaka added a commit to IGVF-DACC/igvf-ui that referenced this issue Mar 8, 2022
# This is the 1st commit message:

IGVF-35-cypress-circleci

Try the cypress orb.

# This is the commit message #2:

Revert "IGVF-35-cypress-circleci"

This reverts commit fa1521f.

# This is the commit message #3:

IGVF-35-cypress-circleci

Revert last change and try a different tactic using docker-compose.test.yml.

# This is the commit message #4:

Revert "IGVF-35-cypress-circleci"

This reverts commit 2e7ad9c.

# This is the commit message #5:

IGVF-35-cypress-circleci

Another attempt to run Cypress in CircleCI.

# This is the commit message #6:

IGVF-35-cypress-circleci

Alter previous attempt.

# This is the commit message #7:

IGVF-35-cypress-circleci

Try two docker-compose files; one specifically for cypress.

# This is the commit message #8:

IGVF-35-cypress-circleci

Possibly the wrong command to start Cypress.

# This is the commit message #9:

IGVF-35-cypress-circleci

Try to solve EACCES error for Cypress cache.

# This is the commit message #10:

IGVF-35-cypress-circleci

Try setting environment variable for Cypress in through the docker-compose file.

# This is the commit message #11:

IGVF-35-cypress-circleci

Fix error setting the environment variable from the last fix.

# This is the commit message #12:

IGVF-35-cypress-circleci

Try a different cypress cache directory

# This is the commit message #13:

IGVF-35-cypress-circleci

Everyone seems to recommend a different syntax. Here’s yet another.

# This is the commit message #14:

IGVF-35-cypress-circleci

Downgrading node in case cypress-io/cypress#15025 is correct.

# This is the commit message #15:

IGVF-35-cypress-circleci

Try apt-get for cypress dependencies.

# This is the commit message #16:

Revert "IGVF-35-cypress-circleci"

This reverts commit f7b66a5.

# This is the commit message #17:

IGVF-35-cypress-circleci

Try using the Cypress orb.

# This is the commit message #18:

IGVF-35-cypress-circleci

Start the server before starting the Cypress tests.

# This is the commit message #19:

IGVF-35-cypress-circleci

Correction to last commit. Options can have very subtle effects on required syntax.

# This is the commit message #20:

Updated config.yml
# This is the commit message #21:

IGVF-35-cypress-circleci

Do a build and run of production build.
forresttanaka added a commit to IGVF-DACC/igvf-ui that referenced this issue Mar 8, 2022
# This is the 1st commit message:

IGVF-35-cypress-circleci

Try the cypress orb.

# This is the commit message #2:

Revert "IGVF-35-cypress-circleci"

This reverts commit fa1521f.

# This is the commit message #3:

IGVF-35-cypress-circleci

Revert last change and try a different tactic using docker-compose.test.yml.

# This is the commit message #4:

Revert "IGVF-35-cypress-circleci"

This reverts commit 2e7ad9c.

# This is the commit message #5:

IGVF-35-cypress-circleci

Another attempt to run Cypress in CircleCI.

# This is the commit message #6:

IGVF-35-cypress-circleci

Alter previous attempt.

# This is the commit message #7:

IGVF-35-cypress-circleci

Try two docker-compose files; one specifically for cypress.

# This is the commit message #8:

IGVF-35-cypress-circleci

Possibly the wrong command to start Cypress.

# This is the commit message #9:

IGVF-35-cypress-circleci

Try to solve EACCES error for Cypress cache.

# This is the commit message #10:

IGVF-35-cypress-circleci

Try setting environment variable for Cypress in through the docker-compose file.

# This is the commit message #11:

IGVF-35-cypress-circleci

Fix error setting the environment variable from the last fix.

# This is the commit message #12:

IGVF-35-cypress-circleci

Try a different cypress cache directory

# This is the commit message #13:

IGVF-35-cypress-circleci

Everyone seems to recommend a different syntax. Here’s yet another.

# This is the commit message #14:

IGVF-35-cypress-circleci

Downgrading node in case cypress-io/cypress#15025 is correct.

# This is the commit message #15:

IGVF-35-cypress-circleci

Try apt-get for cypress dependencies.

# This is the commit message #16:

Revert "IGVF-35-cypress-circleci"

This reverts commit f7b66a5.

# This is the commit message #17:

IGVF-35-cypress-circleci

Try using the Cypress orb.

# This is the commit message #18:

IGVF-35-cypress-circleci

Start the server before starting the Cypress tests.

# This is the commit message #19:

IGVF-35-cypress-circleci

Correction to last commit. Options can have very subtle effects on required syntax.

# This is the commit message #20:

Updated config.yml
# This is the commit message #21:

IGVF-35-cypress-circleci

Do a build and run of production build.

# This is the commit message #22:

IGVF-35-cypress-circleci

Try anything.
@gustavodamazio
Copy link

Try using yarn package manager or downgrading npm to version 7.
For me it worked correctly.

I'm using Jenkins in an alpine linux container with node 16.

    stage('Prepare-build') {
    steps {
        sh 'node --version'
        sh 'npm --version'
        sh 'ls -p'
        sh 'git config --global --add safe.directory "*"'
        sh 'yarn install'
    }
}

The error I was getting is the following:

npm ERR! code 1
npm ERR! path /home/jenkins/agent/workspace/project/app/project-web-manager/node_modules/cypress
npm ERR! command failed
npm ERR! command sh /tmp/postinstall-716e113e.sh
npm ERR! Cypress cannot write to the cache directory due to file permissions
npm ERR! 
npm ERR! See discussion and possible solutions at
npm ERR! https://github.com/cypress-io/cypress/issues/1281
npm ERR! 
npm ERR! ----------
npm ERR! 
npm ERR! Failed to access /root/.cache/Cypress:
npm ERR! 
npm ERR! EACCES: permission denied, mkdir '/root/.cache/Cypress'
npm ERR! 
npm ERR! ----------
npm ERR! 
npm ERR! Platform: linux-x64 (Alpine Linux - 3.16.1)
npm ERR! Cypress Version: 10.6.0

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2022-08-18T17_09_27_887Z-debug-0.log

In my same case export the environment variable cypress cache folder did not work as expected, the error was even made mentioned above.

export CYPRESS_CACHE_FOLDER="/home/jenkins/agent/workspace/project/app/.cache"

@marcosaguilera-glober
Copy link

I was getting same issue:
# EACCES: permission denied, mkdir '/root/.cache/Cypress'

I've fixed like this:

  1. In my Dockerfile
#Cypress version using
FROM cypress/included:10.6.0

# Installing sudo for superower installation required
# into entrypoint.sh
RUN apt-get install sudo

# Directories actions
WORKDIR /home/pwsh/tempVolume

ENTRYPOINT ["/bin/sh", "./entrypoint.sh"]
  1. In my entrypoint.sh script file
#!/bin/sh
echo "I'm into the sh file"

## Install packages defined into package.json > dependencies node
##npm install
sudo npm install --unsafe-perm=true --allow-root

## Run cypress
cypress run

I messed up a time with this issue, and I tried with permissions chown node:node, also some chmod -R 777, but nothing worked for me, so I installed sudo apt-get into Linux distro.

Hope this help anyone.

@Rodeoclash
Copy link

I'm not sure if this will work around the problem for others, but seems to have for me:

FROM node:16-bullseye as development

EXPOSE 3000

ENV APP_HOME=/usr/src/app \
  CYPRESS_CACHE_FOLDER="/cypress/.cache" \
  LANG=C.UTF-8

WORKDIR $APP_HOME

RUN mkdir -p $CYPRESS_CACHE_FOLDER
COPY ./package.json ./
COPY ./package-lock.json ./

RUN npm install

The key here being that I made the cache folder first before running the npm install

YMMV!

@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label Aug 21, 2023
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale no activity on this issue for a long period
Projects
None yet
Development

No branches or pull requests

7 participants