npm / cli

the package manager for JavaScript
https://docs.npmjs.com/cli/
Other
8.09k stars 2.92k forks source link

fix(view): dont unwrap arrays in json mode #7506

Closed lukekarrys closed 3 weeks ago

lukekarrys commented 3 weeks ago

The view command alters the data by default to unwrap single item arrays when in human readable mode (the default). This change makes it so those arrays are not altered when the --json flag is set.

Fixes #3611

# Before
❯ npm view tiny-tarball versions
1.0.0
❯ npm view tiny-tarball versions --json
"1.0.0"
# After
❯ npmlocal view tiny-tarball versions
1.0.0
❯ npmlocal view tiny-tarball versions --json
[
  "1.0.0"
]
npm-cli-bot commented 3 weeks ago

no statistically significant performance changes detected

timing results |**app-large**|clean|lock-only|cache-only|modules-only|no-lock|no-cache|no-modules|no-clean|show-version|run-script|cache-only
peer-deps|no-clean
audit| |---|---|---|---|---|---|---|---|---|---|---|---|---| |npm@latest|35.938 ±1.02|10.448 ±0.06|11.486 ±0.04|1.552 ±0.00|1.519 ±0.00|1.266 ±0.01|8.016 ±0.03|1.290 ±0.02|0.138 ±0.00|0.165 ±0.01|13.049 ±0.13|4.171 ±2.96| |#7506|39.301 ±0.37|10.557 ±0.05|11.589 ±0.07|1.538 ±0.04|1.524 ±0.01|1.260 ±0.00|8.147 ±0.07|1.277 ±0.01|0.137 ±0.00|0.163 ±0.00|14.469 ±0.14|2.112 ±0.02| |**app-medium**|clean|lock-only|cache-only|modules-only|no-lock|no-cache|no-modules|no-clean|show-version|run-script|cache-only
peer-deps|no-clean
audit| |---|---|---|---|---|---|---|---|---|---|---|---|---| |npm@latest|30.000 ±0.97|7.860 ±0.02|8.758 ±0.00|1.488 ±0.02|1.502 ±0.00|1.396 ±0.00|5.801 ±0.01|1.326 ±0.01|0.139 ±0.00|0.169 ±0.00|9.375 ±0.20|2.988 ±1.46| |#7506|29.355 ±0.96|7.845 ±0.04|8.810 ±0.04|1.492 ±0.02|1.487 ±0.00|1.395 ±0.02|5.777 ±0.01|1.288 ±0.03|0.135 ±0.00|0.164 ±0.00|9.640 ±0.05|1.947 ±0.00|
wraithgar commented 3 weeks ago

This feels like one of those incorrect behaviors folks have undoubtedly come to rely on, and this would be a breaking change.

lukekarrys commented 3 weeks ago

I'm not so sure, because currently this behavior is impossible to predict unless you know the data being returned. In the case of versions it will only unwrap the array if the package has only one version. It will properly keep it as an array for packages with multiple versions. So I think it's unlikely people are relying on this as they would still need to handle when versions returns an array.

# before
❯ npm view abbrev versions --json
[
  "1.0.3",
  "1.0.4",
  "1.0.5",
  "1.0.6",
  "1.0.7",
  "1.0.9",
  "1.1.0",
  "1.1.1",
  "2.0.0"
]
# after
❯ npmlocal view abbrev versions --json
[
  "1.0.3",
  "1.0.4",
  "1.0.5",
  "1.0.6",
  "1.0.7",
  "1.0.9",
  "1.1.0",
  "1.1.1",
  "2.0.0"
]
ljharb commented 3 weeks ago

If they did the smart thing and always wrapped the field in [].concat() then this wouldn’t break them, at least :-)