[Bug?]: Unable to list a dependency as a regular and peer
Self-service
- [ ] I'd be willing to implement a fix
Describe the bug
Hi, this is a follow-up to #4359. I'm looking to migrate a mono-repo from yarn classic to yarn 3, and am trying to get around a design pattern we've relied upon. There's multiple instances of a "meta-package" of dependencies. From the docs, it sounds like the preferred solution is to add the dependencies as a direct and peer. However if I try directly adding the dependency as a peer, it will throw an error:
Package "typescript" is already listed as a regular dependency - remove the -D,-P flags or remove it from your dependencies firstΒ·
$ yarn add [--json] [-F,--fixed] [-E,--exact] [-T,--tilde] [-C,--caret] [-D,--dev] [-P,--peer] [-O,--optional] [--prefer-dev] [-i,--interactive] [--cached] [--mode #0] ...
The particular docs I'm looking at is under https://yarnpkg.com/advanced/rulebook#packages-should-only-ever-require-what-they-formally-list-in-their-dependencies
The preferred one is to list the dependency (in Next.js's case, webpack) as both a regular dependency and a peer dependency. Yarn will interpret this pattern as "peer dependency with a default", meaning that your users will be able to take ownership of the Webpack package if they need to, while still giving the package manager the ability to emit a warning if the provided version is incompatible with the one your package expects.
How do you go about achieving this? At the moment it is possible to add a dependency as a peer and dev, but not as peer and direct.
To reproduce
await packageJson({
name: 'parent',
workspaces: [
'packages/*'
]
});
await packageJson({
name: 'meta-package',
dependencies: {
"typescript": "4.6.3",
}
}, { cwd: './packages/meta-package' });
await packageJson({
name: 'child',
devDependencies: {
"meta-package": "workspace:*"
},
scripts: {
"build": "tsc --help"
}
}, { cwd: './packages/child' });
await yarn('config', 'set', 'nodeLinker', 'node-modules');
await yarn('install');
await expect(yarn('workspace', 'meta-package', 'add', '-P', '[email protected]')).resolves.toBeTruthy();
await yarn('workspace', 'child', 'add', '-D', '[email protected]');
await expect(yarn('workspace', 'child', 'run', 'build')).resolves.toBeTruthy();
Environment
System:
OS: macOS 12.3.1
CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Binaries:
Node: 12.13.0 - /private/var/folders/4x/pqh5fq0x3mx_b1drhv0s2r900000gp/T/xfs-eb8ded8c/node
Yarn: 3.2.0 - /private/var/folders/4x/pqh5fq0x3mx_b1drhv0s2r900000gp/T/xfs-eb8ded8c/yarn
npm: 6.12.0 - ~/.nvm/versions/node/v12.13.0/bin/npm
Additional context
No response
This issue reproduces on master:
Error: expect(received).resolves.toBeTruthy()
Received promise rejected instead of resolved
Rejected to value: [Error: Command failed: /usr/bin/node /github/workspace/scripts/actions/../run-yarn.js workspace meta-package add -P [email protected]
Usage Error: Package "typescript" is already listed as a regular dependency - remove the -D,-P flags or remove it from your dependencies first
$ yarn add [--json] [-F,--fixed] [-E,--exact] [-T,--tilde] [-C,--caret] [-D,--dev] [-P,--peer] [-O,--optional] [--prefer-dev] [-i,--interactive] [--cached] [--mode #0] ...
]
at expect (/github/workspace/.yarn/cache/expect-npm-24.8.0-8c7640c562-44ff9ab1e7.zip/node_modules/expect/build/index.js:138:15)
at module.exports (evalmachine.<anonymous>:29:7)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async /github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:57:13
at async executeInTempDirectory (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:18:16)
at async executeRepro (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:25:12)
at async ExecCommand.execute (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-2.0.3-558f52b79f-286d94b96d.zip/node_modules/@arcanis/sherlock/lib/commands/exec.js:26:38)
at async ExecCommand.validateAndExecute (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-4061026d74.zip/node_modules/clipanion/lib/advanced/Command.js:161:26)
at async Cli.run (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-4061026d74.zip/node_modules/clipanion/lib/advanced/Cli.js:74:24)
at async Cli.runExit (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-4061026d74.zip/node_modules/clipanion/lib/advanced/Cli.js:83:28)
Just adding that I'm also running into this issue. We have @org/testing package that has a dependency on jest and @types/jest. But those libraries aren't being made available to consuming packages
+1