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

[BUG] INIT_CWD missing in v7 #2033

Closed
Geod24 opened this issue Oct 24, 2020 · 14 comments
Closed

[BUG] INIT_CWD missing in v7 #2033

Geod24 opened this issue Oct 24, 2020 · 14 comments
Assignees
Labels
Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release

Comments

@Geod24
Copy link

Geod24 commented Oct 24, 2020

Did a search on INIT_CWD, nothing relevant showed up. I also searched through the changelog and didn't find anything relevant.

Current Behavior:

In order to get the directory the user was in when the command was run, one can use process.env.INIT_CWD, as documented here.
This was introduced in v5.4.0 according to the changelog.

However, when upgrading from node 14 to node 15, INIT_CWD is not defined anymore. The upgrade hit me today, as I ran brew upgrade on my machine. I could reproduce on our Github CI.

Expected Behavior:

I did not expect INIT_CWD to disappear, or if it does, I expect a way to access similar information.

Steps To Reproduce:

I created a simple repository to see the regression: https://github.com/Geod24/npm-init-cwd-regression
The repository essentially consist of a package.json that does npm start, and index.js is as follow:

const process = require('process');
console.info(process.env);

I setup a Github action to test on Ubuntu with Node 10, 12, 14, and 15.
One can see, when looking at the environment, that the one in 15 is quite different.

Environment:

Can reproduce on Mac and Ubuntu, and other details are linked above.

@Geod24 Geod24 added Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release labels Oct 24, 2020
@tobua
Copy link

tobua commented Nov 1, 2020

Did a bit of research and found that the variable was set in a separate package called npm-lifecycle that has been removed from the npm CLI starting with version 7: INIT_CWD in npm-lifecycle.

@ljharb
Copy link
Contributor

ljharb commented Nov 2, 2020

What would INIT_CWD contain that wouldn't be in process.cwd()?

@tobua
Copy link

tobua commented Nov 3, 2020

Usually these are the same. The only case I know of is in the case of a postinstall script being run. There process.cwd() (and env.PWD) is set to the package where the script runs but INIT_CWD still points to the initial package where the npm install was run.

I was able to detect whether a script runs in postinstall without the INIT_CWD variable and so am fine with it being deprecated. I don't know if there are any plugins that cannot be implemented without this variable.

I like that the console output for postinstall scripts is being hidden since version 7. Don't remember having seen anything useful there.

@ljharb
Copy link
Contributor

ljharb commented Nov 3, 2020

Seems like this can be closed?

@Geod24
Copy link
Author

Geod24 commented Nov 4, 2020

Usually these are the same.

What ?

Scripts are run from the root of the module, regardless of what your current working directory is when you call npm run. If you want your script to use different behavior based on what subdirectory you're in, you can use the INIT_CWD environment variable, which holds the full path you were in when you ran npm run.

@jsbarrett
Copy link

I use INIT_CWD for building simple npm scripts to scaffold out new components.
In these scenarios, process.cwd() does not help me
because that will return the root of my project rather than the subdirectory I'm in.

This is a breaking change and annoying enough that I'm going to revert versions until it's fixed.

marcosvega91 added a commit to marcosvega91/user-event that referenced this issue Dec 6, 2020
because of an issue with node 15 npm/cli#2033. In any case husky is not really helpful in CI
marcosvega91 added a commit to testing-library/user-event that referenced this issue Dec 6, 2020
because of an issue with node 15 npm/cli#2033. In any case husky is not really helpful in CI
@isaacs isaacs self-assigned this Dec 10, 2020
@isaacs
Copy link
Contributor

isaacs commented Dec 10, 2020

This was an oversight and is a bug in @npmcli/config. Will be fixed shortly.

@Geod24
Copy link
Author

Geod24 commented Dec 11, 2020

@isaacs : I don't see any test in the above commit, so this could regress in the future.

@isaacs
Copy link
Contributor

isaacs commented Dec 15, 2020

@Geod24 The tests are in npm/config@7070885 This is a bug in @npmcli/config, not in the cli itself.

@shanoaice
Copy link

This issue still appears on my machine. I'm using npm@7.3.0. When I tried to install sharp, it does not run sharp's postinstall script to setup the bindings.

@tobua
Copy link

tobua commented Jan 3, 2021

@KsRyY I've checked with 7.3.0 and the variable was set. If you're trying to debug note that the console output is hidden as of npm@7 so you may simply not see the familiar messages anymore, but the script is still being run in the background.

@StephenCleary
Copy link

I also repro this with 7.3.0. My script is literally echo %INIT_CWD% (on Windows). It's not defined. None of the config settings from package.json are defined, either.

@shanoaice
Copy link

@KsRyY I've checked with 7.3.0 and the variable was set. If you're trying to debug note that the console output is hidden as of npm@7 so you may simply not see the familiar messages anymore, but the script is still being run in the background.

I probably need to open an new issue. My problem, specifically, is about lifecycle script not being run (could be related to this problem though). When I installed sharp on my project, the postinstall script must be run in order to download sharp's nodejs binding, but npm just simply extracts its wrapper code and not running the postinstall script, causing sharp to be not usable. I've tested npm with several other packages that has postinstall script (such as level) and none of the postinstall script is being run, so I'm sure it's not a package-specific problem.

@ljharb
Copy link
Contributor

ljharb commented Jan 8, 2021

It should be running them, but all success output is suppressed, so you’ll only see output if there’s a failure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release
Projects
None yet
Development

No branches or pull requests

7 participants