-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug?]: Cannot require @yarnpkg/types to use TS in yarn.config.cjs #5878
Comments
yarn.config.cjs loaded before PnP engine, so currently you can only import types (in PnP mode), not wrapper. |
I'm not sure the specific reasons that makes
And leave off the Note that |
…ig (#5954) **What's the problem this PR addresses?** We added support for JS constraints thanks to the `yarn.config.cjs` file. However this file is currently executed from within the Yarn process, so it doesn't have access to the dependencies when operating under the PnP installation mode. Fixes #5878 **How did you fix it?** I'm still on the fence on the right solution. This PR automatically loads the `.pnp.cjs` file when the `loadUserConfig` file is called. An alternative would be a post-install hook to allow each linker to inject their runtime inside the process post-install, but that was more involved and I figured it's worth more consideration. Another alternative was to not do anything and expect users to setup the PnP runtime themselves, but it feels something that Yarn should be able to handle. A third alternative would be to evaluate this file within a worker thread, which would be started with the proper `execArgv` flags. It's less intrusive than the post-install hook so I kinda like this idea, but again - worth more consideration. I'm working on enabling tests, and to this effect I'm checking whether we can remove the `NODE_OPTIONS` values from the `makeTemporaryEnv` subprocesses. **Checklist** <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [x] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed.
Here's how I got the expected type support in Notes:
Changes:
First, just for clarity, here's the diff between the diff --git a/yarn.config.cjs b/yarn.config.cjs
index 6c3bd36..52823fd 100644
--- a/yarn.config.cjs
+++ b/yarn.config.cjs
@@ -1,12 +1,13 @@
-// @ts-check
-
-/** @type {import('@yarnpkg/types')} */
-const { defineConfig } = require(`@yarnpkg/types`);
+/*** @typedef { import('@yarnpkg/types').Yarn.Config } YarnConfig */
+/*** @typedef { import('@yarnpkg/types').defineConfig } DefineConfig */
+/*** @type { { defineConfig: DefineConfig }} _ */
+const { defineConfig } = require("@yarnpkg/types");
/**
* This rule will enforce that a workspace MUST depend on the same version of
* a dependency as the one used by the other workspaces.
*
+ * @type YarnConfig["constraints"]
*/
function enforceConsistentDependenciesAcrossTheProject({ Yarn }) {
for (const dependency of Yarn.dependencies()) {
@@ -24,10 +25,11 @@ function enforceConsistentDependenciesAcrossTheProject({ Yarn }) {
dependency.update(otherDependency.range);
}
}
+ return Promise.resolve();
}
module.exports = defineConfig({
constraints: async (ctx) => {
- enforceConsistentDependenciesAcrossTheProject(ctx);
+ await enforceConsistentDependenciesAcrossTheProject(ctx);
},
});
And here's the full code of /*** @typedef { import('@yarnpkg/types').Yarn.Config } YarnConfig */
/*** @typedef { import('@yarnpkg/types').defineConfig } DefineConfig */
/*** @type { { defineConfig: DefineConfig }} _ */
const { defineConfig } = require("@yarnpkg/types");
/**
* This rule will enforce that a workspace MUST depend on the same version of
* a dependency as the one used by the other workspaces.
*
* @type YarnConfig["constraints"]
*/
function enforceConsistentDependenciesAcrossTheProject({ Yarn }) {
for (const dependency of Yarn.dependencies()) {
if (dependency.type === `peerDependencies`) {
continue;
}
for (const otherDependency of Yarn.dependencies({
ident: dependency.ident,
})) {
if (otherDependency.type === `peerDependencies`) {
continue;
}
dependency.update(otherDependency.range);
}
}
return Promise.resolve();
}
module.exports = defineConfig({
constraints: async (ctx) => {
await enforceConsistentDependenciesAcrossTheProject(ctx);
},
}); |
@milesrichardson it's a bit overcomplicated. What about your particular example, you just need to use async functions instead of manually created promises: |
It's not quite true - code doesn't become more safe: relationship between |
Self-service
Describe the bug
When trying to write typed constraints as shown in the documentation I get an error message. This is behavior is consistent in our mono-repo project as well as newly created empty project.
To reproduce
yarn init -y
yarn set version berry
echo "const { defineConfig } = require('@yarnpkg/types');" > yarn.config.cjs
yarn add @yarnpkg/types
yarn constraints
Environment
Additional context
No response
The text was updated successfully, but these errors were encountered: