avajs/ava

Support `.ava` cacheDir for better compatibility with Yarn PnP

andrewzey opened this issue · 3 comments

Hello,

We're using Yarn 3 with PnP in our mono-repo, and find that ava is creating unwanted node_modules directories in our workspace (with node_modules/.cache/ava/failing-tests.json) This is a result of

ava/lib/cli.js

Line 256 in f047694

const cacheDir = path.join(projectDir, 'node_modules', '.cache', 'ava');

Sometimes the existence of node_modules confuses the pnp patched executables (eg. eslint), so the mere existence of node_modules can sometimes cause issues with things working reliable.

It would be great if you supported some manner of specifying the cache directory or simply defaulted to projectDir/.ava and recommended adding .ava to the .gitignore.

Thanks!

I was thinking about this the other day actually, and yes I agree that should be an option. Placing it under node_modules by default does provide the best out-of-the box experience though.

cache is currently configurable as a boolean, I propose we add '.ava' as a valid value which would then use path.join(projectDir, '.ava') as the cache dir. All other values should result in a configuration error, I don't think we should make this super configurable just yet.


However this does leave us with the following code:

ava/lib/worker/base.js

Lines 210 to 217 in 4dc385b

let importFromProject = async ref => {
// Do not use the cacheDir since it's not guaranteed to be inside node_modules.
const avaCacheDir = joinPath(projectDir, 'node_modules', '.cache', 'ava');
await mkdir(avaCacheDir, {recursive: true});
const stubPath = joinPath(avaCacheDir, 'import-from-project.mjs');
await writeFileAtomic(stubPath, 'export const importFromProject = ref => import(ref);\n');
({importFromProject} = await import(pathToFileURL(stubPath)));
return importFromProject(ref);

The way this could work is:

  • If cache is not false, use the computed cacheDir
  • Otherwise, insist on using this node_modules/.cache/ava directory

And then we'd have to document that as a caveat.

What do you think?