You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lately I am contributing a lot to pnpm which is an alternative package manager for Node with tons of interesting features (and it's also very fast).
One of its peculiarity is the node_modules structure it creates. Unfortunately, pre-commit expects node_modules to be handled by npm, so it calculates the root variable in install.js as just __dirname + '/../..'.
Would it be possible to replace this logic with another one that is compatible with pnpm?
For example: root might be the first directory up the tree in which there is a package.json file.
Why wouldn't it be compatible tho? Because I don't recall node_modules being a npm specific thing. It's what the node module system assumes to exists. Both yarn and npm use this. What makes pnpm different here?
@3rd-Edenpnpm actually installs modules to a global store, ~/.pnpm-store. It then uses links, hard links if possible, to build the node_modules directory within the target project. This reduces disk usage across multiple projects. So, when pre-commit gets installed it resolves the path __dirname + '/../..' to ~/.pnpm-store/2/ since the real current working directory is ~/.pnpm-store/2/registry.npmjs.org/pre-commit.
@3rd-Eden, it's more or less how @jsumners explained but not quite. pnpm uses a global store, but it still hardlinks all the dependencies inside node_modules. The problem is that it doesn't copy them directly to their final destination. Example: pre-commit would end up in node_modules/.registry.npmjs.org/pre-commit/1.2.2/node_modules/pre-commit. Then pnpm makes a symlink from that location to node_modules/pre-commit.
This means that ../../ is node_modules/.registry.npmjs.org/pre-commit/1.2.2.
Going up the tree you can still get to the project directory.
I suspect it is better to use process.env.INIT_CWD because process.cwd() will have the pre-commit's package location in node_modules, not the dependent project's location.
EDIT:
there was a similar issue with husky. I contributed this fix: typicode/husky#185
but they fixed it by then in the "next" version. I don't know what was the fix but the "next" version of husky works perfectly with pnpm
Activity
jsumners commentedon Feb 27, 2018
As a reference, I do this sort of thing in
nixconfig-- https://github.com/jsumners/nixconfig/blob/master/lib/findpkg.js3rd-Eden commentedon Feb 27, 2018
Why wouldn't it be compatible tho? Because I don't recall
node_modulesbeing anpmspecific thing. It's what the node module system assumes to exists. Bothyarnandnpmuse this. What makespnpmdifferent here?jsumners commentedon Feb 27, 2018
@3rd-Eden
pnpmactually installs modules to a global store,~/.pnpm-store. It then uses links, hard links if possible, to build thenode_modulesdirectory within the target project. This reduces disk usage across multiple projects. So, whenpre-commitgets installed it resolves the path__dirname + '/../..'to~/.pnpm-store/2/since the real current working directory is~/.pnpm-store/2/registry.npmjs.org/pre-commit.etamponi commentedon Feb 27, 2018
@3rd-Eden, it's more or less how @jsumners explained but not quite.
pnpmuses a global store, but it still hardlinks all the dependencies insidenode_modules. The problem is that it doesn't copy them directly to their final destination. Example:pre-commitwould end up innode_modules/.registry.npmjs.org/pre-commit/1.2.2/node_modules/pre-commit. Then pnpm makes a symlink from that location tonode_modules/pre-commit.This means that
../../isnode_modules/.registry.npmjs.org/pre-commit/1.2.2.Going up the tree you can still get to the project directory.
jsumners commentedon Feb 27, 2018
That makes sense. That you for the clarification.
jsumners commentedon Feb 27, 2018
Wouldn't changing:
pre-commit/install.js
Line 10 in f25888f
To:
Solve this problem?
zkochan commentedon Feb 27, 2018
I suspect it is better to use
process.env.INIT_CWDbecauseprocess.cwd()will have thepre-commit's package location in node_modules, not the dependent project's location.EDIT:
there was a similar issue with husky. I contributed this fix: typicode/husky#185
but they fixed it by then in the "next" version. I don't know what was the fix but the "next" version of husky works perfectly with pnpm
3rd-Eden commentedon Feb 27, 2018
@zkochan You're confusing
process.cwd()with__dirnamewhere__dirnamewould be the directory ofpre-commitzkochan commentedon Feb 28, 2018
@3rd-Eden no, I know it for sure because I was configuring lifecycle scripts in pnpm and I learned how they work with npm
here's the lib that npm and pnpm use for running lifecycle script: https://github.com/npm/lifecycle
wdis what is set as cwd for the scripts when they are executed. wd is the location of the dependency, not the project.process.cwd()is assigned toINIT_CWDhttps://github.com/npm/lifecycle/blob/latest/index.js#L67jsumners commentedon May 7, 2018
It is possible to work around this issue with pnpm >= 1.39.0 -- pnpm/pnpm#1055 (comment)