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

INIT_CWD env variable should be set to path of where pnpm was invoked #7042

Open
2 of 4 tasks
ejdaly opened this issue Sep 4, 2023 · 2 comments
Open
2 of 4 tasks

Comments

@ejdaly
Copy link

ejdaly commented Sep 4, 2023

Verify latest release

  • I verified that the issue exists in the latest pnpm release

pnpm version

No response

Which area(s) of pnpm are affected? (leave empty if unsure)

CLI, Package manager compatibility

Link to the code that reproduces this issue or a replay of the bug

No response

Reproduction steps

Create a new package in your workspace, with:

package.json:

{
  "name": "test_init_cwd",
  "version": "0.0.1",
  "bin": {
    "print_init_cwd": "./print_init_cwd.js"
  }
}

print_init_cwd.js:

#!/usr/bin/env node
console.log(`INIT_CWD: ${process.env.INIT_CWD}`);

Install the package as a dependency to another workspace package:

$ cd /path/to/other_package
$ pnpm add test_init_cwd

Exec the installed script:

$ pnpm exec print_init_cwd
INIT_CWD: undefined

Exec the installed script (npm):

$ npx print_init_cwd
INIT_CWD: /path/to/other_package

$ cd ./a/b/c
$ npx print_init_cwd
INIT_CWD: /path/to/other_package/a/b/c

Another example, would be to have typescript installed in a package, and then do:

$ cd path/to/package
$ mkdir -p ./a/b/c
$ cd ./a/b/c
$ touch index.ts
$ pnpm exec tsc ./index.ts
error TS6053: File 'index.ts' not found
$ npx tsc ./index.ts
<works..>

Describe the Bug

npm / yarn define INIT_CWD as being the directory from which you run the script.

I think pnpm should be doing the same for consistency.

https://docs.npmjs.com/cli/v9/commands/npm-run-script#description
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

https://yarnpkg.com/advanced/lifecycle-scripts#environment-variables
$INIT_CWD: Directory from which the script has been invoked. This isn't the same as the cwd, which for scripts is always equal to the closest package root.

Related:
#2897
#5464
#4441

Expected Behavior

pnpm to set INIT_CWD in the same way that npm / yarn set INIT_CWD

Which Node.js version are you using?

18.14.1

Which operating systems have you used?

  • macOS
  • Windows
  • Linux

If your OS is a Linux based, which one it is? (Include the version if relevant)

Ubuntu22.04

@ejdaly
Copy link
Author

ejdaly commented Sep 4, 2023

Tried a quick hack to see what the effect would be...

.bashrc

function pnpm() {
  INIT_CWD=$PWD command pnpm "$@";
}
export -f pnpm;

This example now works correctly (as you would expect...)

$ pnpm exec print_init_cwd
INIT_CWD: <works...>

Interestingly, the tsc example above still doesn't work.

It seems npm is leaving the PWD variable as being the path from which npm was invoked;
pnpm has PWD set to the root of the current package (the closest package.json).
I guess tsc is checking that variable to find current context...
But that's a separate issue; it's debatable which is more correct here - and not something that needs to be consistent in my opinion (it's up to the script itself to work out it's context - which is probably why the INIT_CWD was added to npm in the first place).

@antl3x
Copy link

antl3x commented Jan 22, 2024

@ejdaly thanks for the workaround. I'm facing the same problem using zx + pnpm scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants