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] npm run env echo stopped working after 8.13 #5729

Closed
2 tasks done
viters opened this issue Oct 20, 2022 · 4 comments
Closed
2 tasks done

[BUG] npm run env echo stopped working after 8.13 #5729

viters opened this issue Oct 20, 2022 · 4 comments
Labels
Bug thing that needs fixing Priority 1 high priority issue Release 8.x work is associated with a specific npm 8 release

Comments

@viters
Copy link

viters commented Oct 20, 2022

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

On npm 8.12.1 I was running npm -s run env echo '$npm_package_version' to get current package.json version. It does not work anymore. There are no traces of changes regarding that in https://github.com/npm/cli/blob/v8.19.2/CHANGELOG.md

image
image

Expected Behavior

image
image

Steps To Reproduce

No response

Environment

  • npm: 8.13.2 and beyond
  • Node.js: 18.8.0
  • OS Name: MacOS / Linux
@viters viters added Bug thing that needs fixing Needs Triage needs review for next steps Release 8.x work is associated with a specific npm 8 release labels Oct 20, 2022
@viters viters changed the title [BUG] npm run env echo stopped working between 8.12 and 8.18 [BUG] npm run env echo stopped working between 8.12 and 8.13 Oct 20, 2022
@viters viters changed the title [BUG] npm run env echo stopped working between 8.12 and 8.13 [BUG] npm run env echo stopped working after 8.13 Oct 20, 2022
@wraithgar
Copy link
Member

wraithgar commented Oct 20, 2022

Looks like #5064 started quoting/escaping environment variable params too aggressively.

If I run npm run env echo \$PATH then sh gets the following args [ '-c', '--', "env echo Hello '$PATH'" ]

@wraithgar wraithgar added Priority 1 high priority issue and removed Needs Triage needs review for next steps labels Oct 20, 2022
@nlf
Copy link
Contributor

nlf commented Nov 2, 2022

this is behaving as intended.

when an argument is passed to a run-script the goal is to ensure that end script sees the exact input the user provided, and does not unexpectedly execute code. unfortunately what you were doing was taking advantage of the lack of escaping we had in the past, so now that the arguments are actually being escaped it no longer works. we do not intend to revert this behavior.

as a workaround, you have a few options though.

  1. npm run env | grep '^npm_package_version' | cut -d= -f2 - kind of long, involves three chained commands. you could probably use awk or sed instead of grep and cut to make it a bit shorter.
  2. npm exec -c 'echo $npm_package_version' this one runs a shell in your project with the environment variables defined, and the string passed to -c is executed. this one works because npm exec explicitly runs a shell and runs your command in it. this is what i would suggest as it's most similar to what you're doing currently.
  3. we'd have to go through our rfc process but potentially we could add an intentional feature that makes the npm run env builtin a bit smarter, or even make it a dedicated command. this could potentially allow you to do things like npm env npm_package_version. i think this idea is worth discussing, especially seeing as it can be done without having to spawn an extra shell like npm run and npm exec do

@nlf nlf closed this as not planned Won't fix, can't repro, duplicate, stale Nov 2, 2022
@viters
Copy link
Author

viters commented Nov 2, 2022

@nlf
Thanks for your comment and the solutions. I like the idea about (3), I will try to post RRFC for that.
I see also another solution, npm/rfcs#634

@moos
Copy link

moos commented Feb 7, 2023

This has broken our build -- it seems other package content like npm_package_dependencies_foo are no longer exposed. We used this to pick up values from package.json for use in dotenv config. Is there a recommended workaround?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Priority 1 high priority issue Release 8.x work is associated with a specific npm 8 release
Projects
None yet
Development

No branches or pull requests

4 participants