Skip to content

Setting NODE_OPTIONS in environmentVariables breaks yarn PnP module resolution. #3196

@Jym77

Description

@Jym77

So, I'm trying to use ava in a brand new yarn PnP repository: https://github.com/Jym77/ava-test

Just ran yarn init, yarn set version berry (3.5.0), and yarn add --dev ava (5.2.0).


My package.json is:

{
  "name": "ava-test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "private": true,
  "packageManager": "yarn@3.5.0",
  "devDependencies": {
    "ava": "^5.2.0"
  },
  "scripts": {
    "test": "ava"
  },
  "ava": {
    "environmentVariables": {
      "NODE_OPTIONS": ""
    }
  }
}

and there is only one file test/foo.spec.js:

const test = require("ava")

test(`Foo`, (t) => {t.pass()})

When runnig yarn test, I get the following error:

$ yarn test

  ✘ test/foo.spec.js exited due to an error:

  Error: Cannot find module '/mnt/c/Projects/playground/ava-test/.yarn/__virtual__/ava-virtual-3b38b82913/0/cache/ava-npm-5.2.0-7f7d2440d8-d5991f1f3d.zip/node_modules/ava/lib/worker/base.js'

  Error: Cannot find module '/mnt/c/Projects/playground/ava-test/.yarn/__virtual__/ava-virtual-3b38b82913/0/cache/ava-npm-5.2.0-7f7d2440d8-d5991f1f3d.zip/node_modules/ava/lib/worker/base.js'
      at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1026:15)
      at Function.Module._load (node:internal/modules/cjs/loader:871:27)
      at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
      at MessagePort.<anonymous> (node:internal/main/worker_thread:197:24)
      at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:736:20)
      at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28)

  ─


node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^
Error: Cannot find module '/mnt/c/Projects/playground/ava-test/.yarn/__virtual__/ava-virtual-3b38b82913/0/cache/ava-npm-5.2.0-7f7d2440d8-d5991f1f3d.zip/node_modules/ava/lib/worker/base.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1026:15)
    at Function.Module._load (node:internal/modules/cjs/loader:871:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at MessagePort.<anonymous> (node:internal/main/worker_thread:197:24)
    at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:736:20)
    at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28) {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

When replacing the ava config by:

  "ava": {
    "environmentVariables": {
      "FOO": ""
    }
  }

(i.e. just changing the name of the variable I'm declaring), the test runs fine.

I do need to reset NODE_OPTIONS due to #3148 (and of course running this from a shell where I've pre-unset NODE_OPTIONS to not get the other error).


This seems to be an interaction between yarn PnP virtual directories, and ava reading the NODE_OPTIONS variable. I'm not fluent enough in module resolution to dig further.
Given that the problem disappears when changing part of ava config, I'm filling this issue with ava, but it might as well be yarn misunderstanding something 🤔

The problem seems to be here both with ava 5.0.0 and 5.2.0. I have another repo with ava 5.1.0 and yarn using node_modules (not PnP) where the NODE_OPTIONS variable doesn't break anything.


Trying to fool ava by changing:

  "scripts": {
    "test": "NODE_OPTIONS='' ava"
  },

still gets the error with the __virtual__ path not being found.


Calling a side script:

#!/usr/bin/env bash

NODE_OPTIONS=''
yarn ava

with

"scripts": {
    "test": "bash ./test.sh"
  }

in package.json is a workaround…

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions