nx icon indicating copy to clipboard operation
nx copied to clipboard

Yarn Berry generateLockfile error

Open veimox opened this issue 1 year ago • 1 comments

Current Behavior

When using generateLockfile on the @nx/next:build fails with

Original error: Cannot read properties of undefined (reading 'has')
   TypeError: Cannot read properties of undefined (reading 'has')
    at <secret>/node_modules/nx/src/plugins/js/lock-file/yarn-parser.js:238:47
    at Map.forEach (<anonymous>)
    at mapSnapshots (<secret>/node_modules/nx/src/plugins/js/lock-file/yarn-parser.js:229:17)
    at stringifyYarnLockfile (<secret>/node_modules/nx/src/plugins/js/lock-file/yarn-parser.js:139:23)
    at createLockFile (<secret>/node_modules/nx/src/plugins/js/lock-file/lock-file.js:133:60)
    at <secret>/node_modules/@nx/next/src/executors/build/build.impl.js:64:54
    at Generator.next (<anonymous>)
    at <secret>/node_modules/tslib/tslib.js:167:75
    at new Promise (<anonymous>)
    at Object.__awaiter (<secret>/node_modules/tslib/tslib.js:163:16)

Research

I have put a breakpoint to debug the issue and the problem comes when it tries to resolve the package string-width-cjs. The problem is that in our yarn.lock the entry is such:

"string-width-cjs@npm:string-width@^4.2.0, string-width@npm:^1.0.2 || 2 || 3 || 4, string-width@npm:^4.0.0, string-width@npm:^4.1.0, string-width@npm:^4.2.0, string-width@npm:^4.2.2, string-width@npm:^4.2.3":
  version: 4.2.3
  resolution: "string-width@npm:4.2.3"
  dependencies:
    emoji-regex: ^8.0.0
    is-fullwidth-code-point: ^3.0.0
    strip-ansi: ^6.0.1
  checksum: e52c10dc3fbfcd6c3a15f159f54a90024241d0f149cf8aed2982a2d801d2e64df0bf1dc351cf8e95c3319323f9f220c16e740b06faecd53e2462df1d2b5443fb
  languageName: node
  linkType: hard

As it can be seen the package is string-width-cjs but the resolution is done with string-width@npm:4.2.3. Note the package name difference. The object existingKeys does not contain the -cjs one and when it tries to access it here by name it fails. That outputs an undefined that happens to lack the .has() method.

I guess this is a bit of an edge case with conflicting package names and resolutions and I can resolve it locally but I guess that it exposes some issue with the existing population logic of existingKeys.

Expected Behavior

Generates an pruned package.json without errors

GitHub Repo

No response

Steps to Reproduce

I cannot share the yarn.lock or a reproduceable repo

Nx Report

>  NX   Report complete - copy this into the issue template

   Node   : 16.19.0
   OS     : darwin x64
   yarn   : 3.6.0
   Hasher : Native
   
   nx                 : 16.3.2
   @nx/js             : 16.3.2
   @nx/jest           : 16.3.2
   @nx/linter         : 16.3.2
   @nx/workspace      : 16.3.2
   @nx/cypress        : 16.3.2
   @nx/devkit         : 16.3.2
   @nx/eslint-plugin  : 16.3.2
   @nx/nest           : 16.3.2
   @nx/next           : 16.3.2
   @nx/node           : 16.3.2
   @nx/plugin         : 16.3.2
   @nx/react          : 16.3.2
   @nx/storybook      : 16.3.2
   @nrwl/tao          : 16.3.2
   @nx/web            : 16.3.2
   @nx/webpack        : 16.3.2
   nx-cloud           : 16.0.5
   typescript         : 4.9.5

Failure Logs

No response

Operating System

  • [X] macOS
  • [ ] Linux
  • [ ] Windows
  • [ ] Other (Please specify)

Additional Information

No response

veimox avatar Jun 06 '23 13:06 veimox

Seems to be related to the issue I reported here: https://github.com/nrwl/nx/issues/17329

I can confirm that I have the same package dependency in my yarn.lock file as mentioned in the other issue.

cwoebker avatar Jun 07 '23 17:06 cwoebker

@cwoebker can you provide a minimal repo where this can be produced?

meeroslav avatar Jun 23 '23 11:06 meeroslav

@meeroslav Try with this one

https://github.com/veimox/nx-17440

veimox avatar Jun 23 '23 13:06 veimox

@meeroslav i just updraded the test project with 16.4.0 that contains https://github.com/nrwl/nx/pull/17752 but the problem persist. I think we should reopen this issue. It is reproduceable for you to test out.

veimox avatar Jun 23 '23 19:06 veimox

@veimox can you please make that repo public, because I can't access it?

meeroslav avatar Jun 25 '23 20:06 meeroslav

@meeroslav oh sorry! It is now public

veimox avatar Jun 26 '23 05:06 veimox

@FrozenPandaz @meeroslav this issue still persists now reported in #17440, #17752 and #17816. Can we re-open them until they are actually fixed?

veimox avatar Jun 27 '23 20:06 veimox

Yes sorry. I will look into it as soon as possible, now that the repro is public.

meeroslav avatar Jun 28 '23 11:06 meeroslav

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.

github-actions[bot] avatar Jul 30 '23 00:07 github-actions[bot]