nx: `nx migrate latest` fails on yarn 2

Current Behavior

When running YARN_NODE_LINKER="node-modules" yarn nx migrate latest in a yarn 2 nx workspace, the process ends with Command failed: yarn add @nrwl/workspace@latest.

Expected Behavior

The workspace should be migrated to the latest version of nx

Steps to Reproduce

  1. Run npm install -g yarn
  2. Clone https://github.com/bryanforbes/nx-migrate-problem
  3. Run yarn in the project
  4. Run YARN_NODE_LINKER="node-modules" yarn nx migrate latest

Explanation for failure

After creating the temporary directory for tao, tao migrate latest is run from that environment. This command creates a new temporary directory and attempts to run yarn add @nrwl/workspace@latest which fails because no package.json exists. In the repo I linked above, I added a script called tao-yarn-add which emulates this failure by running yarn tao-yarn-add. This must be run via yarn in order to emulate calling yarn add and getting the yarn 2 engine. I’ve also added a script to package.json called tao-yarn-add-fixed that adds a package.json to the empty directory before calling yarn add and this fixes the issue. Again, this script must be run with yarn tao-yarn-add-fixed.

Failure Logs

❯ YARN_NODE_LINKER="node-modules" yarn nx migrate latest
Fetching meta data about packages.
It may take a few minutes.
Fetching @nrwl/workspace@latest

 ERROR  The migrate command failed.

Command failed: yarn add @nrwl/workspace@latest
/Users/bryan/Projects/ng12-test/node_modules/yargs/build/lib/yargs.js:1132
                throw err;
                ^

Error: Command failed: /var/folders/3k/v_hqdqq57l38tll9dwv0sgl80000gn/T/tmp-36776-kG6GoQPMMs7X/node_modules/.bin/tao migrate latest
    at checkExecSyncError (node:child_process:707:11)
    at Object.execSync (node:child_process:744:15)
    at Object.handler (/Users/bryan/Projects/ng12-test/node_modules/@nrwl/workspace/src/command-line/nx-commands.js:90:25)
    at Object.runCommand (/Users/bryan/Projects/ng12-test/node_modules/yargs/build/lib/command.js:196:48)
    at Object.parseArgs [as _parseArgs] (/Users/bryan/Projects/ng12-test/node_modules/yargs/build/lib/yargs.js:1043:55)
    at Object.get [as argv] (/Users/bryan/Projects/ng12-test/node_modules/yargs/build/lib/yargs.js:986:25)
    at Object.initLocal (/Users/bryan/Projects/ng12-test/node_modules/@nrwl/cli/lib/init-local.js:26:79)
    at Object.<anonymous> (/Users/bryan/Projects/ng12-test/node_modules/@nrwl/cli/bin/nx.js:43:18)
    at Module._compile (node:internal/modules/cjs/loader:1109:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10) {
  status: 1,
  signal: null,
  output: [ null, null, null ],
  pid: 36778,
  stdout: null,
  stderr: null
}

Environment

Node : 16.1.0 OS : darwin x64 yarn : 2.4.1

nx : Not Found @nrwl/angular : 12.2.0 @nrwl/cli : 12.2.0 @nrwl/cypress : 12.2.0 @nrwl/devkit : 12.2.0 @nrwl/eslint-plugin-nx : 12.2.0 @nrwl/express : Not Found @nrwl/jest : 12.2.0 @nrwl/linter : 12.2.0 @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : Not Found @nrwl/react : Not Found @nrwl/schematics : Not Found @nrwl/tao : 12.2.0 @nrwl/web : Not Found @nrwl/workspace : 12.2.0 @nrwl/storybook : Not Found @nrwl/gatsby : Not Found typescript : 4.1.5

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 18
  • Comments: 17 (5 by maintainers)

Most upvoted comments

@fatihdogmus I encountered the same error and it turned out to be the Angular CLI not supporting my installed version of Node (v16, see issue: https://github.com/angular/angular-cli/issues/20796 ). Running nx migrate latest with Node v14 resolved it for me.

Here’s a recipe that worked for me:

First, MAKE SURE YOU DON’T HAVE a “.npmrc” file in your repo, otherwise, it won’t work.

  1. Applying this patch (which creates the temp file in the repo):
diff --git a/src/commands/migrate.js b/src/commands/migrate.js
index fe11e0558eeede06642d21f307ee18c2ca0d24d5..a1e4874846bc9e4a67fd18b9db5021de243bd751 100644
--- a/src/commands/migrate.js
+++ b/src/commands/migrate.js
@@ -317,7 +317,7 @@ function createFetcher() {
     return function f(packageName, packageVersion) {
         return tslib_1.__awaiter(this, void 0, void 0, function* () {
             if (!cache[`${packageName}-${packageVersion}`]) {
-                const dir = tmp_1.dirSync().name;
+                const dir = tmp_1.dirSync({ tmpdir:process.cwd() }).name;
                 const npmrc = checkForNPMRC();
                 if (npmrc) {
                     // Creating a package.json is needed for .npmrc to resolve

Like so:

"@nrwl/tao": "patch:@nrwl/tao@13.1.3#patches/nx-tao-patch.patch",

(provided the patch above is saved under “patches/nx-tao-patch.patch” in the repo)

AND

  1. call the migration like this (thanks @sushruth ):
NX_MIGRATE_USE_LOCAL=true yarn nx migrate latest

Works for me!

@luejerry yes! that was exactly the reason! I downgraded the version to 14.17.0 and it worked. Thx a lot

For what its worth, using NX_MIGRATE_USE_LOCAL=true variable helped use the local version of nx for this. This may help you unblock until the next error.

EDIT: I also had to unplug @nrwl/tao and modify it to get it to work - modify migrate.js (from the error message) to create package.json (content: {name:"temp"}) in the temp directory and I was able to migrate to next.

I also removed unplug metadata from my main package.json and run yarn again and it all worked out.

Y’all might be interested in feat(core): implement Yarn v3

Here is how I am using Yarn v3 successfully.

Start with yarn install using Yarn v1

Then run yarn set version berry and follow that up with yarn set version 3.x (because berry has a bug)

Addd nodeLinker: node-modules to .yarnrc.yml

Run yarn install and update your .gitignore with

.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

Then commit.

I am having no issues with Yarn v3 and updating Nx 👯

Crack a beer.

For the record, the reproduction repo still fails with node 14.17.0.

Although my repo is not using yarn 2 workspaces, just regular yarn, I get the a similar error as above. It is just a toy project, but we will be using nx and angular in a project so encountering this problem could be a problem in the future.

Eror message:

> yarn nx migrate latest
yarn run v1.22.10
$ nx migrate latest
C:\Users\fatih.dogmus\IdeaProjects\angular-learning\node_modules\yargs\build\lib\yargs.js:1132
                throw err;
                ^

Error: Command failed: yarn
    at checkExecSyncError (node:child_process:682:11)
    at Object.execSync (node:child_process:719:15)
    at taoPath (C:\Users\fatih.dogmus\IdeaProjects\angular-learning\node_modules\@nrwl\workspace\src\command-line\nx-commands.js:312:21)
    at Object.handler (C:\Users\fatih.dogmus\IdeaProjects\angular-learning\node_modules\@nrwl\workspace\src\command-line\nx-commands.js:86:19)
    at Object.runCommand (C:\Users\fatih.dogmus\IdeaProjects\angular-learning\node_modules\yargs\build\lib\command.js:196:48)
    at Object.parseArgs [as _parseArgs] (C:\Users\fatih.dogmus\IdeaProjects\angular-learning\node_modules\yargs\build\lib\yargs.js:1043:55)
    at Object.get [as argv] (C:\Users\fatih.dogmus\IdeaProjects\angular-learning\node_modules\yargs\build\lib\yargs.js:986:25)
    at Object.initLocal (C:\Users\fatih.dogmus\IdeaProjects\angular-learning\node_modules\@nrwl\cli\lib\init-local.js:24:13)
    at Object.<anonymous> (C:\Users\fatih.dogmus\IdeaProjects\angular-learning\node_modules\@nrwl\cli\bin\nx.js:43:18)
    at Module._compile (node:internal/modules/cjs/loader:1108:14) {
  status: 1,
  signal: null,
  output: [ null, null, null ],
  pid: 26920,
  stdout: null,
  stderr: null
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Enviorenment: OS: Windows 10 Enterprise Version 20H2 Yarn: V1.2210 Node.js: v15.6.0

Depndencies nx : Not Found @nrwl/angular : 12.0.6 @nrwl/cli : 12.0.6 @nrwl/cypress : 12.0.6 @nrwl/devkit : 12.0.6 @nrwl/eslint-plugin-nx : 12.0.6 @nrwl/express : Not Found @nrwl/jest : 12.0.6 @nrwl/linter : 12.0.6 @nrwl/nest : Not Found @nrwl/next : Not Found @nrwl/node : Not Found @nrwl/react : Not Found @nrwl/schematics : Not Found @nrwl/tao : 12.0.6 @nrwl/web : Not Found @nrwl/workspace : 12.0.6 @nrwl/storybook : Not Found @nrwl/gatsby : Not Found typescript : 4.1.5

I think this particular issue has been fixed, since we now create package.json for any folder that receives packages temporarily as it is required to obey .npmrc.

There may still be things broken w/ yarn 2 and pnp, those are tracked via #2386.

I’m going to close this out in favor of that issue. I can’t provide any updates as to yarn 2 / pnp support, but it is something that we are aware of.