nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.56k stars 2.36k forks source link

LoadPluginError: Could not load plugin #23435

Closed decline closed 4 months ago

decline commented 5 months ago

Current Behavior

When using the libraryGenerator() method from @nx/angular/generators inside a (Jest) test, it will fail.

Example:

import { libraryGenerator } from "@nx/angular/generators";
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { Tree } from '@nx/devkit';

describe('my-generator generator', () => {
  let tree: Tree;

  beforeEach(() => {
    tree = createTreeWithEmptyWorkspace();
  });

  it('should run successfully', async () => {
    // this will fail:
    const result =   await libraryGenerator(tree, {
      name: 'test',
      directory: '',
    });

    expect(result).toBeDefined();
  });
});

The test will fail with the following error message:

LoadPluginError: Could not load plugin /app/node_modules/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json

Important:

This error only happens on Linux operating system. The same test passes on a MacOS operating system.

Expected Behavior

The test should run successfully on every operating system.

GitHub Repo

https://github.com/decline/nx-plugin-issue

Steps to Reproduce

  1. Checkout the repository
  2. Run docker compose up

Info:

Starting the docker container will execute the nx run my-plugin:test command which will fail the test. If you run the nx run my-plugin:test command on a MacOS system, it will pass!

Nx Report

NX   Report complete - copy this into the issue template

Node   : 20.10.0
OS     : linux-arm64
npm    : 10.2.3

nx                 : 19.0.4
@nx/js             : 19.0.4
@nx/jest           : 19.0.4
@nx/linter         : 19.0.4
@nx/eslint         : 19.0.4
@nx/workspace      : 19.0.4
@nx/angular        : 19.0.4
@nx/devkit         : 19.0.4
@nx/eslint-plugin  : 19.0.4
@nx/plugin         : 19.0.4
@nrwl/tao          : 19.0.4
@nx/web            : 19.0.4
@nx/webpack        : 19.0.4
typescript         : 5.4.5
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
---------------------------------------
Local workspace plugins:
         @plugin-issue/my-plugin

Failure Logs

LoadPluginError: Could not load plugin /app/node_modules/nx/src/plugins/project-json/build-nodes/package-json-next-to-project-json

  12 |   it('should run successfully', async () => {
  13 |     // this will fail:
> 14 |     const result =   await libraryGenerator(tree, {
     |                      ^
  15 |       name: 'test',
  16 |       directory: '',
  17 |     });

  at loadNxPluginAsync (../node_modules/nx/src/project-graph/plugins/loader.js:207:15)
  at loadNxPlugin (../node_modules/nx/src/project-graph/plugins/loader.js:180:9)
  at loadNxPlugins (../node_modules/nx/src/project-graph/plugins/internal-api.js:49:48)
  at async buildProjectGraphAndSourceMapsWithoutDaemon (../node_modules/nx/src/project-graph/project-graph.js:75:32)
  at async createProjectGraphAndSourceMapsAsync (../node_modules/nx/src/project-graph/project-graph.js:202:25)
  at async createProjectGraphAsync (../node_modules/nx/src/project-graph/project-graph.js:191:39)
  at async initEsLint (../node_modules/@nx/eslint/src/generators/init/init.js:41:19)
  at async lintInitGenerator (../node_modules/@nx/eslint/src/generators/init/init.js:80:12)
  at async lintProjectGeneratorInternal (../node_modules/@nx/eslint/src/generators/lint-project/lint-project.js:25:22)
  at async addLintingGenerator (../node_modules/@nx/angular/src/generators/add-linting/add-linting.js:14:22)
  at async addLinting (../node_modules/@nx/angular/src/generators/library/library.js:123:5)
  at async libraryGeneratorInternal (../node_modules/@nx/angular/src/generators/library/library.js:69:5)
  at async libraryGenerator (../node_modules/@nx/angular/src/generators/library/library.js:25:12)
  at async Object.<anonymous> (src/generators/my-generator/generator.spec.ts:14:22)
Cause:
TypeError: performance.mark is not a function

  12 |   it('should run successfully', async () => {
  13 |     // this will fail:
> 14 |     const result =   await libraryGenerator(tree, {
     |                      ^
  15 |       name: 'test',
  16 |       directory: '',
  17 |     });

  at loadNxPluginAsync (../node_modules/nx/src/project-graph/plugins/loader.js:198:21)
  at loadNxPlugin (../node_modules/nx/src/project-graph/plugins/loader.js:180:9)
  at loadNxPlugins (../node_modules/nx/src/project-graph/plugins/internal-api.js:49:48)
  at async buildProjectGraphAndSourceMapsWithoutDaemon (../node_modules/nx/src/project-graph/project-graph.js:75:32)
  at async createProjectGraphAndSourceMapsAsync (../node_modules/nx/src/project-graph/project-graph.js:202:25)
  at async createProjectGraphAsync (../node_modules/nx/src/project-graph/project-graph.js:191:39)
  at async initEsLint (../node_modules/@nx/eslint/src/generators/init/init.js:41:19)
  at async lintInitGenerator (../node_modules/@nx/eslint/src/generators/init/init.js:80:12)
  at async lintProjectGeneratorInternal (../node_modules/@nx/eslint/src/generators/lint-project/lint-project.js:25:22)
  at async addLintingGenerator (../node_modules/@nx/angular/src/generators/add-linting/add-linting.js:14:22)
  at async addLinting (../node_modules/@nx/angular/src/generators/library/library.js:123:5)
  at async libraryGeneratorInternal (../node_modules/@nx/angular/src/generators/library/library.js:69:5)
  at async libraryGenerator (../node_modules/@nx/angular/src/generators/library/library.js:25:12)
  at async Object.<anonymous> (src/generators/my-generator/generator.spec.ts:14:22)

Package Manager Version

No response

Operating System

Additional Information

No response

digitalhank commented 5 months ago

experiencing the same but with the applicationGenerator from '@nx/next' on v18.3.4

pedroyan commented 5 months ago

I'm experiencing the same problem. Tests work on MacOS but not on our Linux CI.

It happens when I call the libraryGenerator function from @nx/nest@18.3.4 on a Jest test

SuneRadich commented 5 months ago

I have a similar issue with a Jest test also using the libraryGenerator. However, I see the error on both Github Actions / nx agents as well as on my Mac developer machine.

I also tried to copy paste the test in this issue and run the test, and it fails on my mac running Sonoma 14.4.1

SuneRadich commented 5 months ago

I got my test to run by following a workaround from https://github.com/mswjs/msw/issues/1851

Object.defineProperties(globalThis, {
  performance: { value: performance, writable: true },
});

Not quite sure why it works, but my test runs again with this 'fix'.

dasco144 commented 5 months ago

I'm having the same issue when wrapping the service schematic from @schematics/angular, and executing that.

Although, when running locally (MacOS) I get this error

ReferenceError: TextDecoder is not defined

      18 |
      19 |   const serviceGenerator = wrapAngularDevkitSchematic('@schematics/angular', 'service');
    > 20 |   await serviceGenerator(tree, serviceOptions);
         |   ^
      21 |

      at Object.<anonymous> (../../node_modules/@angular-devkit/schematics/src/rules/template.js:33:17)
      at Object.<anonymous> (../../node_modules/@angular-devkit/schematics/src/index.js:56:14)
      at Object.<anonymous> (../../node_modules/@angular-devkit/schematics/tools/file-system-engine-host-base.js:16:15)
      at Object.<anonymous> (../../node_modules/@angular-devkit/schematics/tools/index.js:27:14)
      at createWorkflow (../../node_modules/nx/src/adapter/ngcli-adapter.js:165:26)
      at ../../node_modules/nx/src/adapter/ngcli-adapter.js:697:26
      at serviceGenerator (src/generators/service/generator.ts:20:3)
      at Object.<anonymous> (src/generators/service/generator.spec.ts:34:5)

but on CI I get the same as OP.

sonikasharma1403 commented 5 months ago

facing the same issue, but only in the TeamCity environment.

sonikasharma1403 commented 5 months ago

@AgentEnder , any update on this?

hunhavoc commented 4 months ago

Having the same issue. Tests run well on local Mac, but does not work on Linux env in CI.

AgentEnder commented 4 months ago

Hi there 👋 - so this is a bit more complex than one may imagine. Most typically getting this error during a unit test is a result of a test doing something that is causing the project graph to be created. You'll need to mock the project graph during these tests, and while that sounds rough its also going to give you much more accurate and repeatable tests.

Without mocking the graph, the tests will be running generators which create the project graph of the current Nx workspace and make some decisions about that. That's almost always not really what you want. We just recently fixed that behavior on the Nx repo.

This suddenly became a problem for some folks because several of our published generators do rely on the graph now.

You can see how we are mocking this in our unit test setup: https://github.com/nrwl/nx/blob/master/scripts/unit-test-setup.js

github-actions[bot] commented 3 months ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.