nrwl / nx

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

Vitest fails on CI with workspace generators >19.0.0 #27816

Open ghost opened 2 months ago

ghost commented 2 months ago

Current Behavior

We are using workspace code generators using @nx/plugin. Because we have updated prettier to v3 a while ago, we have switched to testing the generators with vitest instead of jest as it introduced some ESM stuff. Lately, we have upgraded Nx from v19.0.0 to v19.6.4. During the upgrade, we noticed issues with those tests timing out or failing in CI, using Github actions. Most code generators use Nx generators under the hood (e.g. @nx/js:lib, @nx/react:lib, etc.) I have tried diagnosing the problem, and these are some interesting outputs:

TypeError: Cannot destructure property 'resolver' of 'pending.get(...)' as it is undefined. ❯ createNodesResult ../../node_modules/nx/src/project-graph/plugins/isolation/plugin-pool.js:162:25 ❯ consumeMessage ../../node_modules/nx/src/project-graph/plugins/isolation/messaging.js:38:32 ❯ ../../node_modules/nx/src/project-graph/plugins/isolation/plugin-pool.js:77:47 ❯ Socket. ../../node_modules/nx/src/utils/consume-messages-from-socket.js:13:17


* After modifying the config with he following additions, no more errors, **but tests fail with the default testTimeout**. This is what we also used before, but without `sequence.hooks` addition:
```ts
poolOptions: {
      threads: {
        singleThread: true,
        isolate: true
      }
    },
    sequence: {
      hooks: 'list'
    }

Notes:

Expected Behavior

Either:

GitHub Repo

No response

Steps to Reproduce

  1. Have a repo with workspace generators using @nx/plugin
  2. Update prettier to v3 (3.3.3 to be precise)
  3. Switch the test configuration in the workspace generators to use vitest
  4. Use libraryGenerator from @nx/js in your code generator, with formatFiles too (not sure it has impact though)
  5. Write a test for the generator, for example:

Generator:

export async function chartGenerator(
  tree: Tree,
  options: ChartGeneratorSchema
) {
  const { name } = options
  const projectName = `charts-${name}`
  await generateReactLib(tree, {
    name: projectName,
    directory: `libs/dashboards/charts/${name}`,
    projectNameAndRootFormat: 'as-provided',
    importPath: `@island/charts/${name}`
  })

  const projects = getProjects(tree)
  const newProject = projects.get(projectName)
  if (!newProject) {
    throw new Error(`Could not find project with name charts-${options.name}`)
  }

  const chartNames = names(name)
  const componentName = `${chartNames.className}Chart`
  const widgetName = `${chartNames.className}Widget`

  generateFiles(tree, join(__dirname, 'files'), newProject.root, {
    componentName,
    widgetName,
    propsName: `${componentName}Props`,
    widgetPropsName: `${widgetName}Props`,
    cssClassName: `island-charts-${name}-chart`,
    chartName: chartNames.className,
    defaultWidgetName: `${chartNames.className} Chart`
  })

  await formatFiles(tree)
}

export default chartGenerator

Test:

describe('chart generator', () => {
  let tree: Tree

  beforeEach(() => {
    tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' })
  })

  it('should have all necessary files', async () => {
    await chartGenerator(tree, { name: 'area' })
    const config = readProjectConfiguration(tree, 'charts-area')

    expect(config).toBeTruthy()
    const root = config.sourceRoot
    expect(tree.exists(`${root}/chart/AreaChart.tsx`)).toBeTruthy()
    expect(
      tree.exists(`${root}/__stories__/AreaChart.stories.tsx`)
    ).toBeTruthy()
  })
})

Nx Report

NX   Report complete - copy this into the issue template

Node           : 20.15.1
OS             : darwin-arm64
Native Target  : aarch64-macos
yarn           : 4.4.1

nx                 : 19.6.4
@nx/js             : 19.6.4
@nx/jest           : 19.6.4
@nx/linter         : 19.6.4
@nx/eslint         : 19.6.4
@nx/workspace      : 19.6.4
@nx/cypress        : 19.6.4
@nx/devkit         : 19.6.4
@nx/eslint-plugin  : 19.6.4
@nx/node           : 19.6.4
@nx/plugin         : 19.6.4
@nx/react          : 19.6.4
@nx/storybook      : 19.6.4
@nrwl/tao          : 19.6.4
@nx/vite           : 19.6.4
@nx/web            : 19.6.4
@nx/webpack        : 19.6.4
typescript         : 5.5.4
---------------------------------------
Community plugins:
nx-stylelint : 17.1.5
---------------------------------------
Local workspace plugins:
         @island/workspace

Failure Logs

No response

Package Manager Version

4.4.1

Operating System

Additional Information

No response

jaysoo commented 2 months ago

@danr-za I'm investigating this. I just want to clarify if you are using Nx Agents, or if this is running directly on GitHub Actions?

Also, if you set NX_ISOLATE_PLUGINS=false in CI, does that resolve the original issue?

jaysoo commented 2 months ago

This is the repo that I'm trying to reproduce this with, but without any success so far: https://github.com/jaysoo/issue27816

ghost commented 2 months ago

Thanks @jaysoo . We are running directly on GitHub without Nx Agents (yet, we need to upgrade)

github-actions[bot] commented 2 months ago

This issue has been automatically marked as stale because no reproduction was provided within 7 days. Please help us help you. Providing a repository exhibiting the issue helps us diagnose and fix the issue. Any time that we spend reproducing this issue is time taken away from addressing this issue and other issues. This issue will be closed in 21 days if a reproduction is not provided. If a reproduction has been provided, please reply to keep it active. Thanks for being a part of the Nx community! 🙏

ghost commented 2 months ago

Not stale. @jaysoo could you add me as a collaborator to that repo? I'll try to play with it around. I see CI is set up

jaysoo commented 2 months ago

@danr-za Added you as a collaborator.