[Bug?]: yarn constraints
exits with fatal error if yarn.config.cjs
imports from ESM, unless executed with DISABLE_V8_COMPILE_CACHE=1 yarn constraints
#5987
Labels
bug
Something isn't working
Hey! Thanks for all your work on Yarn 4. :) I love it so far, and writing constraints in JS is a nice change. I've encountered a bug, but there's a workaround...
Self-service
Describe the bug
Running
yarn constraints
throws a fatal error ("A dynamic import callback was not specified
") whenyarn.config.cjs
attempts toimport()
code from an ECMAScript Module. This is a bug, because dynamicimport()
should be available in all CommonJS modules (in fact, when you try torequire()
from an ESM, the error message even tells you to switch toimport()
).However, the script executes successfully when executed with
DISABLE_V8_COMPILE_CACHE=1 yarn constraints
. This indicates the bug is likely in thev8-compile-cache
package (possibly the one referenced in this open issue from 2020). However, I have not investigated the root cause. For me, the workaround is fine, and I simply added a script in mypackage.json
to set the variable:"scripts": { "check-constraints": "DISABLE_V8_COMPILE_CACHE=1 yarn constraints" }
.Possibly related: I am using the
node-modules
linker.To reproduce
1. Create a new Yarn project with
node-modules
linkerFirst, create a new Yarn project. I've noticed this bug with the
node-modules
linker. I haven't attempted to replicate it with PnP. This is my.yarnrc.yml
:2. Install the
is-exact-version
packageThe package I am attempting to import is
is-exact-version
, which is installed in thedependencies
of my root workspace. You can reproduce it by installing the same version I have:This is what the main export of that package looks like (note that it's using
import
statements):3. Sanity check:
require()
fails, as expectedPut this into
yarn.config.cjs
and runyarn constraints
:As expected, we get an error telling us to
import
instead:4. Reproduce the bug:
import()
failsTry the simplest possible dynamic
import()
:This fails with error:
4. Reproduce the workaround
Setting
DISABLE_V8_COMPILE_CACHE=1
resolves the issue and the script runs successfully:Appendix: Potential fix that does not work
Given the error about the unresolved callback, I tried wrapping the import safely. This makes no difference - it produces the same error:
Environment
❯ yarn dlx -q envinfo --preset jest System: OS: macOS 13.5 CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz Binaries: Node: 20.9.0 - /private/var/folders/np/djbv9lnn5wd62yrs60zxh_p40000gn/T/xfs-fae2114e/node Yarn: 4.0.1 - /private/var/folders/np/djbv9lnn5wd62yrs60zxh_p40000gn/T/xfs-fae2114e/yarn npm: 10.1.0 - ~/.nvm/versions/node/v20.9.0/bin/npm
Additional context
As mentioned, I am using the
node-modules
linker.The only reason I noticed that
DISABLE_V8_COMPILE_CACHE=1
fixed the bug is because when I was switching back and forth between attempts to fix the script, I would sometimes encounter a different error, "invalid host defined options
." Googling that led me tov8-compile-cache
. There are some recent issues with similar errors in other projects, e.g. pyodide/pyodide#4267 and prettier/prettier-vscode#3114 (Then I asked ChatGPT if I could opt out of v8-compile-cache, and it told me I could setDISABLE_V8_COMPILE_CACHE=1
, which sounded so convenient I thought it was hallucinating, but it worked!)The text was updated successfully, but these errors were encountered: