Permissions issue when cloning from git directly

What I Wanted to Do

Apparently in npm v6.11 it was a bug introduced that prevents installing dependencies from git repositories

A trivial example would be

{
    "devDependencies": {
        "qs": "git+ssh://git@github.com/ljharb/qs"
    }
}

When you npm install it it should successfully install that dependency.

What Happened Instead

It does not and fails with the fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-394e5f3d/.git' error

Reproduction Steps

An important note: I have found it only happens when the project is located in the root of the mounted volume!!

I created a repository with reproduction steps: https://github.com/zerkms/npm-6.11.3-git-mirror-bug

Details

Platform Info

$ npm --versions
<!-- paste output here -->
$ node -p process.platform
<!-- paste output here -->

Upgraded the repository with node 13 results.

It’s also surprising and disappointing this report had drawn no attention at all from the development team, wondering if using private git repositories is such an infrequent use case :man_shrugging:

My guess is the problem is not related to the git repository being private, but to running in the Dockerfile as root and the working directories used while preparing the git dependency for installing. There were big changes to file ownership when running as root in npm 6.11.0.

@shadowspawn in either case - it should report more clearly

npm ERR! Command failed: git clone --mirror -q ssh://git@github.com/ljharb/qs.git /root/.npm/_cacache/tmp/git-clone-ae6bf67a/.git
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-ae6bf67a/.git'

this error message looks like a genuine bug

On Docker setups (node:lts-alpine) I have currently “npm install -g npm@6.10.3” as temporary Dockerfile fix get packages installed correctly as root.