Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

fix(add): properly save new deps where they go #273

Merged
merged 1 commit into from
May 6, 2021
Merged

Conversation

wraithgar
Copy link
Member

This consolidates and standardizes the logic around where in the package.json we save added packages. We explicitly obey the saveType if it's given, properly infer it now if it isn't, and enforce exclusivity between things like dependencies and devDependencies

We also now log a warning when we've ignored one for the other.

References

Closes: npm/cli#2647
Related to: npm/cli#3096
Related to: npm/cli#2647

@wraithgar wraithgar force-pushed the gar/save-type-bugfixes branch 5 times, most recently from 70bb60a to 29314bc Compare May 5, 2021 15:59
@wraithgar wraithgar marked this pull request as ready for review May 5, 2021 15:59
@isaacs
Copy link
Contributor

isaacs commented May 5, 2021

test/arborist/reify.js Outdated Show resolved Hide resolved
Copy link
Contributor

@isaacs isaacs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything here looks good, but I'd recommend adding this:

diff --git a/tap-snapshots/test/arborist/reify.js.test.cjs b/tap-snapshots/test/arborist/reify.js.test.cjs
index 513c7485..8a69c358 100644
--- a/tap-snapshots/test/arborist/reify.js.test.cjs
+++ b/tap-snapshots/test/arborist/reify.js.test.cjs
@@ -14982,6 +14982,10 @@ exports[`test/arborist/reify.js TAP no saveType: peer only > must match snapshot
 {"peerDependencies":{"abbrev":"^1.1.1"}}
 `
 
+exports[`test/arborist/reify.js TAP no saveType: prod w/ compatible peer, saves prod only > must match snapshot 1`] = `
+{"dependencies":{"abbrev":"^1.1.1"}}
+`
+
 exports[`test/arborist/reify.js TAP omit peer deps > finished timers 1`] = `
 Array [
   "arborist:ctor",
diff --git a/test/arborist/reify.js b/test/arborist/reify.js
index b8323a8b..e1eb5361 100644
--- a/test/arborist/reify.js
+++ b/test/arborist/reify.js
@@ -1752,17 +1752,17 @@ t.test('no saveType: dev w/ incompatible optional', async t => {
   t.matchSnapshot(fs.readFileSync(path + '/package.json', 'utf8'))
 })
 
-// t.test('no saveType: prod w/ compatible peer', async t => {
-//   const path = t.testdir({
-//     'package.json': JSON.stringify({
-//       peerDependencies: { abbrev: '*' },
-//       dependencies: { abbrev: '*' },
-//     }),
-//   })
-//   const arb = newArb({ path })
-//   await arb.reify({ add: ['abbrev'] })
-//   t.matchSnapshot(fs.readFileSync(path + '/package.json', 'utf8'))
-// })
+t.test('no saveType: prod w/ compatible peer, saves prod only', async t => {
+  const path = t.testdir({
+    'package.json': JSON.stringify({
+      peerDependencies: { abbrev: '*' },
+      dependencies: { abbrev: '*' },
+    }),
+  })
+  const arb = newArb({ path })
+  await arb.reify({ add: ['abbrev'] })
+  t.matchSnapshot(fs.readFileSync(path + '/package.json', 'utf8'))
+})
 
 t.test('no saveType: peer only', async t => {
   const path = t.testdir({

Ie, uncomment the test, and name it based on what is happening, so if it changes we notice. Or just delete the test if it's not necessary.

Right now where we end up saving a dep is not very well coupled to where
the user requested it.  We are inferring more than we are checking for
intention.  This moves us to more heavily lean on the `saveType` if
given when adding a package into the dependencies.

We also were not consistently inferring the saveType.  There was code in
two places trying to do this, and either place used slightly different
logic. This moves the logic into one place.

Finally, we are more consistently making certain dependency types
mutually exclusive, for instance dependencies and devDependencies.

PR-URL: #273
Credit: @wraithgar
Close: #273
Reviewed-by: @nlf
@nlf nlf force-pushed the gar/save-type-bugfixes branch from d289203 to cbcccc9 Compare May 6, 2021 17:01
@nlf nlf closed this in cbcccc9 May 6, 2021
@nlf nlf merged commit cbcccc9 into main May 6, 2021
@nlf nlf deleted the gar/save-type-bugfixes branch May 6, 2021 17:08
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

--save-dev updates peer dep also
3 participants