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] Package variable replacement in package scripts fails with names containing hyphens #2541

Open
roydukkey opened this issue Jan 25, 2021 · 9 comments
Labels
Bug thing that needs fixing Priority 2 secondary priority issue Release 7.x work is associated with a specific npm 7 release

Comments

@roydukkey
Copy link

Current Behavior:

project.json vars do not work with hyphens within package.json scripts interpolation.

Expected Behavior:

I would expect variable with hyphens which are used from with package script to be properly accessible.

Steps To Reproduce:

  1. With this config:
    package.json
{
	"name": "my-package",
	"bin": {
		"my-package": "./bin/my-package.js"
	},
	"scripts": {
		"prepack": "$npm_package_bin_my-package"
	}
}
  1. Run npm run prepack.
  2. Results in the following error:
% npm run prepack

> my-package@1.0.0 prepack
> $npm_package_bin_my-package

sh: -package: command not found
npm ERR! code 127
npm ERR! path /.../my-package
npm ERR! command failed
npm ERR! command sh -c $npm_package_bin_my-package

npm ERR! A complete log of this run can be found in:
npm ERR!     /.../.npm/_logs/2021-01-25T19_43_07_416Z-debug.log
  1. The process.env actually passed to the script is as follows:
{
  TERM_PROGRAM: 'Apple_Terminal',
  NODE: '/usr/local/bin/node',
  INIT_CWD: '/.../my-package',
  SHELL: '/bin/zsh',
  TERM: 'xterm-256color',
  npm_config_metrics_registry: 'https://registry.npmjs.org/',
  TMPDIR: '/var/folders/pf/rmb77nvd2t99vjmq9z1xc6100000gn/T/',
  TERM_PROGRAM_VERSION: '440',
  COLOR: '1',
  TERM_SESSION_ID: '...',
  USER: '...',
  npm_config_globalconfig: '/usr/local/etc/npmrc',
  SSH_AUTH_SOCK: '/private/tmp/com.apple.launchd.JpMQQSNA9S/Listeners',
  __CF_USER_TEXT_ENCODING: '0x1F5:0x0:0x0',
  npm_execpath: '/usr/local/lib/node_modules/npm/bin/npm-cli.js',
  'npm_config_init.module': '/.../.npm-init.js',
  PATH: '...',
  npm_package_json: '/.../my-package/package.json',
  _: './bin/my-package.js',
  npm_config_userconfig: '/.../.npmrc',
  npm_config_init_module: '/.../.npm-init.js',
  __CFBundleIdentifier: 'com.apple.Terminal',
  npm_command: 'run-script',
  PWD: '/.../my-package',
  npm_lifecycle_event: 'prepack',
  EDITOR: 'vi',
  npm_package_name: 'my-package',
  LANG: 'en_US.UTF-8',
  XPC_FLAGS: '0x0',
  npm_config_node_gyp: '/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js',
  npm_package_version: '1.0.1',
  XPC_SERVICE_NAME: '0',
  SHLVL: '2',
  HOME: '/...',
  'npm_package_bin_my-package': 'bin/my-package.js',
  npm_config_cache: '/.../.npm',
  LOGNAME: '...',
  npm_lifecycle_script: './bin/my-package.js',
  PREFIX: '/usr/local',
  npm_config_user_agent: 'npm/7.4.0 node/v15.6.0 darwin x64',
  npm_node_execpath: '/usr/local/bin/node',
  npm_config_prefix: '/usr/local'
}

Environment:

  • OS: MacOS 11.1 (20C69)
  • Node: 15.6.0
  • npm: 7.4.0
@roydukkey roydukkey 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 Jan 25, 2021
@darcyclarke
Copy link
Contributor

@roydukkey shell vars don't support dash casing; That said, you can still pull out the env config with something like:

  "prepack": "node -pe \"process.env['npm_package_bin_my-package']\""

^ as you alluded to.

@darcyclarke darcyclarke removed the Needs Triage needs review for next steps label Feb 2, 2021
@roydukkey
Copy link
Author

roydukkey commented Feb 2, 2021

@darcyclarke Sure, but why can't npm interpolate $npm_package_bin_my-package? Or, some transformation or escape be available to access the data, such as $npm_package_bin_my_package or $npm_package_bin_my$-package or $npm_package_bin_my--package.

@darcyclarke
Copy link
Contributor

darcyclarke commented Feb 2, 2021

@roydukkey I was going to note it would be a breaking change but this truly looks like it inadvertently stopped interpolating between v6 & v7 (ie. it worked differently before, so it's definitely a bug). Apologize for closing so quickly there, I'll reopen & we'll investigate (ref. the value should be $npm_package_bin_my_package)

@darcyclarke darcyclarke reopened this Feb 2, 2021
@darcyclarke darcyclarke added the Priority 2 secondary priority issue label Feb 2, 2021
@darcyclarke darcyclarke added this to the OSS - Sprint 23 milestone Feb 2, 2021
@ljharb
Copy link
Collaborator

ljharb commented Feb 2, 2021

I was under the impression that virtually all of the npm package env vars were intentionally removed in npm 7, since you can hardcode or read from package.json directly.

In this case, probably the simplest would be duplicating the path to the bin.

@darcyclarke
Copy link
Contributor

darcyclarke commented Feb 2, 2021

@ljharb the RFC that was created last year, about changes to environment variables, specifically outlines that $npm_package_bin_* values shouldn't be affected: https://github.com/npm/rfcs/blob/latest/implemented/0021-reduce-lifecycle-script-environment.md#detailed-explanation

@roydukkey
Copy link
Author

@liharb I am specifically trying to avoid duplication.

@ljharb
Copy link
Collaborator

ljharb commented Feb 2, 2021

Thanks for clarifying, good to know.

@thedoublejay
Copy link

I have the same issue with windows using %npm_package_config_key%. The variable is not interpolated anymore in npm@7.5.4. It was working on npm@6.14.11.

@KayBeSee
Copy link

+1 on this

KayBeSee added a commit to Lily-Technologies/lnrpc that referenced this issue Jan 12, 2022
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 2 secondary priority issue Release 7.x work is associated with a specific npm 7 release
5 participants