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.json#/config/*: Array changed behaviour to joining items with double new-line #3775

Closed
1 task done
askirmas opened this issue Sep 19, 2021 · 2 comments · Fixed by npm/run-script#194
Closed
1 task done
Assignees
Labels
Bug thing that needs fixing Priority 1 high priority issue Release 7.x work is associated with a specific npm 7 release

Comments

@askirmas
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

package.json

{
"config": {
    "array": [
      "item1",
      "item2", "item3"
    ]
}}

process.env

{
  "npm_package_config_array": "item1\n\nitem2\n\nitem3"
}

Expected Behavior

process.env (Like it was at v6):

{
  "npm_package_config_array_0": "item1",
  "npm_package_config_array_1": "item2",
  "npm_package_config_array_2": "item3"
}

Steps To Reproduce

  1. In this environment...
  2. Clone bug repo - https://github.com/askirmas/npm7-bug-package_config/
  3. Change npm version to 7
  4. Run npm start
  5. See error...
$ npm start 

> npm-bug-config@1.0.0 start
> node index.js

node:assert:123
throw new AssertionError(obj);
^

AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
+ actual - expected

{
+   npm_package_config_array: 'item1\n\nitem2\n\nitem3'
-   npm_package_config_array_0: 'item1',
-   npm_package_config_array_1: 'item2',
-   npm_package_config_array_2: 'item3'
}
  at Object.<anonymous> (/Users/kirmasy/code/npm-bug-config/index.js:7:1)
  at Module._compile (node:internal/modules/cjs/loader:1101:14)
  at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
  at Module.load (node:internal/modules/cjs/loader:981:32)
  at Function.Module._load (node:internal/modules/cjs/loader:822:12)
  at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
  at node:internal/main/run_main_module:17:47 {
generatedMessage: true,
code: 'ERR_ASSERTION',
actual: { npm_package_config_array: 'item1\n\nitem2\n\nitem3' },
expected: {
  npm_package_config_array_0: 'item1',
  npm_package_config_array_1: 'item2',
  npm_package_config_array_2: 'item3'
},
operator: 'deepStrictEqual'
}

Environment

  • OS: MacOS 11.5.2
  • Node: 16.9.1
  • npm: 7.21.1
@askirmas askirmas 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 Sep 19, 2021
@askirmas askirmas changed the title [BUG] <title> [BUG] package.json#/config/*: Array changed behaviour to joining items with double new-line Sep 19, 2021
@askirmas askirmas changed the title [BUG] package.json#/config/*: Array changed behaviour to joining items with double new-line [BUG] package.json#/config/*: Array changed behaviour to joining items with double new-line Sep 19, 2021
@askirmas
Copy link
Author

askirmas commented Sep 19, 2021

Same for npm v7.24.0

Nothing in docs https://docs.npmjs.com/cli/v7/configuring-npm/package-json#config

@siemhesda
Copy link
Contributor

Hi @askirmas, After looking deeply into this issue here's a few things I found out...

Findings

The v7 changes to the config have actually been mentioned on the release/v7 changelog under the All Lifecycle Scripts section.

The section references RFC 21 (Reduce the environment variables provided to lifecycle scripts) which talks about the changes and the motivation behind them.

Workaround

For your case, you can achieve the config you are trying to by having the values as suggested below:

1. Previously:

{
  "config": {
    "array": ["item1", "item2", "item3"]
  }
}

process.env

{
  "npm_package_config_array": "item1\n\nitem2\n\nitem3"
}

2. Equivalent (solution)

{
  "config": {
    "array": {
      "0": "item1",
      "1": "item2",
      "2": "item3"
    }
  }
}

process.env (desired)

{
  "npm_package_config_array_0": "item1",
  "npm_package_config_array_1": "item2",
  "npm_package_config_array_2": "item3"
}

Progress

There's however need to have these changes reflected on the documentation (still exploring the justification for the change), or revert the part that causes the double new-line. I'm currently actively working on this issue and these are currently in progress.

wraithgar added a commit to npm/run-script that referenced this issue Jan 22, 2024
Reverts environment parsing of config arrays to use indexed names
instead of joining them with double newlines.  That change appears to
have been introduced when the enviromnent values were added back into
npm, but not in the same way they were done before.

Fixes: npm/cli#3775

---------

Co-authored-by: siemhesda <143130929+siemhesda@users.noreply.github.com>
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 7.x work is associated with a specific npm 7 release
Projects
None yet
3 participants