Skip to content

Commit

Permalink
feat: use the versions from overrides when adding deps without specs (#…
Browse files Browse the repository at this point in the history
…4355)

* feat: use the versions from overrides when adding deps without specs

close #4313

* docs: fix changeset
  • Loading branch information
zkochan committed Feb 19, 2022
1 parent 08e67f2 commit 9c36c0f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .changeset/wicked-sheep-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
"@pnpm/core": minor
"pnpm": minor
---

When adding a new dependency, use the version specifier from the overrides, when present [#4313](https://github.com/pnpm/pnpm/issues/4313).

Normally, if the latest version of `foo` is `2.0.0`, then `pnpm add foo` installs `foo@^2.0.0`. This behavior changes if `foo` is specified in an override:

```json
{
"pnpm": {
"overrides": {
"foo": "1.0.0"
}
}
}
```

In this case, `pnpm add foo` will add `foo@1.0.0` to the dependency. However, if a version is explicitly specifying, then the specified version will be used and the override will be ignored. So `pnpm add foo@0` will install v0 and it doesn't matter what is in the overrides.

1 change: 1 addition & 0 deletions packages/core/src/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ export async function mutateModules (
optionalDependencies,
updateWorkspaceDependencies: opts.update,
preferredSpecs,
overrides: opts.overrides,
})
projectsToInstall.push({
pruneDirectDependencies: false,
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/parseWantedDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default function parseWantedDependencies (
devDependencies: Dependencies
optional: boolean
optionalDependencies: Dependencies
overrides?: Record<string, string>
updateWorkspaceDependencies?: boolean
preferredSpecs?: Record<string, string>
}
Expand Down Expand Up @@ -56,6 +57,13 @@ export default function parseWantedDependencies (
raw: `${rawWantedDependency}@${opts.preferredSpecs[alias]}`,
}
}
if (alias && opts.overrides?.[alias]) {
return {
...result,
pref: opts.overrides[alias],
raw: `${alias}@${opts.overrides[alias]}`,
}
}
return {
...result,
pref: opts.defaultTag,
Expand Down
16 changes: 16 additions & 0 deletions packages/core/test/install/overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,19 @@ test('versions are replaced with versions specified through overrides option', a
)
)
})

test('when adding a new dependency that is present in the overrides, use the spec from the override', async () => {
prepareEmpty()

await addDistTag({ package: 'bar', version: '100.0.0', distTag: 'latest' })

const overrides = {
bar: '100.1.0',
}
const manifest = await addDependenciesToPackage({},
['bar'],
await testDefaults({ overrides })
)

expect(manifest.dependencies?.bar).toBe(overrides.bar)
})

0 comments on commit 9c36c0f

Please sign in to comment.