Proposal - Configurable workspace versions for pnpm add
#4887
Description
- I'd be willing to implement this feature (contributing guide)
Describe the solution you'd like
I think that pnpm add tree-utils
should default to "tree-utils": "workspace:^"
instead of "tree-utils": "workspace:^1.0.0"
because it's safer. Or provide a setting complementary to save-prefix
to allow specifying how workspace versions should be saved.
For instance, two settings could be introduced: save-workspace-prefix
(fallbacks to save-prefix
, default ^
) and save-workspace-version
(default rolling
).
save-workspace-prefix save-workspace-version package.json#dependencies
'any' *
'' 'none' 1.0.0
'^' 'none' ^1.0.0
'~' 'none' ~1.0.0
'' 'current' workspace:1.0.0
'^' 'current' workspace:^1.0.0
'~' 'current' workspace:~1.0.0
'' 'rolling' workspace:*
'^' 'rolling' workspace:^
'~' 'rolling' workspace:~
Describe the user story
This story shows why I think rolling
is safer
Let's create a repository with the following two packages
- my-pkg @ 1.0.0
-> tree-utils @ 1.0.0, exports{ root, leaf }
After both packages are created, add the following dependency
cd packages/my-pkg
pnpm add tree-utils
{
"name": "my-pkg",
"dependencies": {
"tree-utils": "workspace:^1.0.0"
}
}
After a few weeks, add a feature to my-pkg
which requires a new export named branch
from tree-utils
. No problem, just release a new minor version of both my-pkg
and tree-utils
.
- my-pkg @ 1.1.0
-> tree-utils @ 1.1.0, exports{ root, leaf, branch }
{
"name": "my-pkg",
"dependencies": {
"tree-utils": "workspace:^1.0.0"
}
}
Notice that no dependencies were updated, pnpm automatically resolves "tree-utils": "workspace:^1.0.0"
to tree-utils@1.1.0
which is the version in the workspace. All tests and prerelease checks are ok because they are using the latest tree-utils
available in the workspace. But when this package is published, it's not guaranteed that this will be the case. After publishing, another package manager or bundler could resolve "tree-utils": "^1.0.0"
to tree-utils@1.0.0
(e.g. to avoid duplicated dependencies when another package has a dependency on "tree-utils": "~1.0.0"
). This will result in a runtime error since my-pkg
requires a version of tree-utils
that exports branch
and only tree-utils@1.1.0
is capable of it.
The problem is that workspace:^1.0.0
is set in stone and not updated. If pnpm used workspace:^
by default, then my-pkg@1.1.0
would have been published with a dependency on "tree-utils": "^1.1.0"
which is safe because that semver range never lags behind and its lower bound is always tested by the pre-release checks.
Describe the drawbacks of your solution
- Slightly higher maintenance burden
- Changes could be backwards incompatible (unless
save-workspace-version
defaults tocurrent
)