nrwl / nx

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

Migration Issue: Angular related workspace generator/schematics don't work after migrating (possible issue in invoke-nx-generator.js) #4921

Closed Tre665 closed 3 years ago

Tre665 commented 3 years ago

Current Behavior

We migrated our workspace last week from NRWL/NX version 10 to the latest Version 11.3.1 All migration scripts could run and the process finished successfully (as a sidenote: big thumbsup to the new documention pages for the migration process 👍 )

I took the recommend migration path from the docs so migratet first to the latest 10.x version and after that to the latest version.

After it was merged in our main branch we discovered some issue when we wanted to add a new angular library to the project. It shows the following error on the console:

TypeError: Cannot read property '1' of undefined
    at /Users/USERNAME/repos/REPONAME/node_modules/@nrwl/devkit/src/utils/invoke-nx-generator.js:46:55
    at Generator.next (<anonymous>)
    at /Users/USERNAME/repos/REPONAME/node_modules/tslib/tslib.js:117:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/Users/USERNAME/repos/REPONAME/node_modules/tslib/tslib.js:113:16)
    at /Users/USERNAME/repos/REPONAME/node_modules/@nrwl/devkit/src/utils/invoke-nx-generator.js:40:39
    at MergeMapSubscriber.project (/Users/USERNAME/repos/REPONAME/node_modules/@angular-devkit/schematics/src/rules/call.js:74:24)
    at MergeMapSubscriber._tryNext (/Users/USERNAME/repos/REPONAME/node_modules/@angular-devkit/schematics/node_modules/rxjs/internal/operators/mergeMap.js:69:27)
    at MergeMapSubscriber._next (/Users/USERNAME/repos/REPONAME/node_modules/@angular-devkit/schematics/node_modules/rxjs/internal/operators/mergeMap.js:59:18)
    at MergeMapSubscriber.Subscriber.next (/Users/USERNAME/repos/REPONAME/node_modules/@angular-devkit/schematics/node_modules/rxjs/internal/Subscriber.js:66:18)
Cannot read property '1' of undefined
The terminal process "/bin/bash '-l', '-c', '/Users/USERNAME/repos/REPONAME/node_modules/.bin/ng generate @nrwl/angular:library --name=test --no-interactive --dry-run', 'ng generate @nrwl/angular:library --name=test --no-interactive --dry-run'" terminated with exit code: 1.

After lots of testing what could be wrong I recognised that if I change the Unit Test Task Runner to a different value then jest (Karma, none) it starts to work again. Unfortunately we are using jest so this is not an option for us.

I looked a little bit deeper into this issue and found the following code path which throws the error above in the file invoke-nx-generator.js in function invokeNxGenerator at around line 48:

  const root = context.engine.workflow
            ? context.engine.workflow.engineHost.paths[1] //throws error since paths is not defined
            : tree.root.path;

Here is a check for context.engine.workflow and when it succeed it tries to read from the paths array from engineHost. In our case the whole paths property is missing somehow so it errors on that access.

To solve this issue I could workaround it, if I explicitly check for the paths prop that is missing in our case:

  const root = context.engine.workflow.engineHost.paths
            ? context.engine.workflow.engineHost.paths[1] 
            : tree.root.path;

// or maybe it makes sense to have it explicitly check for the 2nd path since this is hardcoded

    const engineHostRootPath =  context.engine.workflow.engineHost.paths && context.engine.workflow.engineHost.paths[1];

    const root = !!engineHostRootPath
                ? engineHostRootPath
                : tree.root.path;

I also logged the the generator which calls this function and it is indeed the jestProjectGenerator. So maybe the root issue could potentially also be there.

Sidenote: I later saw the pinned issue from Victor Savkin and tried the migration as it's described there. But the outcome was unfortunately the same.

What i also could observe is that removing tslib from the dependencies made also the generator to run again. But this is most likely a side effect of the missing lib which leads to other unwanted behavior of course :D

On a freshly new created and empty workspace the generator works as expected. So it might be an issue as well on a implicit dependencies that we use. But i tried to remove / adjust the versions as much as I could but hadn't any luck.

Expected Behavior

That the generators work as before in Version 10

Seems so, it worked until the latest version 10.

Steps to Reproduce

Unfortunately I think it's hard to reproduce the issue. At least I have currently no idea how I could replicate it since it might be a dependency / implicit dependency issue that triggers that behaviour. But if you need more informations I will do my best to add informations if they are helpfull.

Failure Logs

TypeError: Cannot read property '1' of undefined
    at /Users/USERNAME/repos/REPONAME/node_modules/@nrwl/devkit/src/utils/invoke-nx-generator.js:46:55
    at Generator.next (<anonymous>)
    at /Users/USERNAME/repos/REPONAME/node_modules/tslib/tslib.js:117:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/Users/USERNAME/repos/REPONAME/node_modules/tslib/tslib.js:113:16)
    at /Users/USERNAME/repos/REPONAME/node_modules/@nrwl/devkit/src/utils/invoke-nx-generator.js:40:39
    at MergeMapSubscriber.project (/Users/USERNAME/repos/REPONAME/node_modules/@angular-devkit/schematics/src/rules/call.js:74:24)
    at MergeMapSubscriber._tryNext (/Users/USERNAME/repos/REPONAME/node_modules/@angular-devkit/schematics/node_modules/rxjs/internal/operators/mergeMap.js:69:27)
    at MergeMapSubscriber._next (/Users/USERNAME/repos/REPONAME/node_modules/@angular-devkit/schematics/node_modules/rxjs/internal/operators/mergeMap.js:59:18)
    at MergeMapSubscriber.Subscriber.next (/Users/USERNAME/repos/REPONAME/node_modules/@angular-devkit/schematics/node_modules/rxjs/internal/Subscriber.js:66:18)
Cannot read property '1' of undefined
The terminal process "/bin/bash '-l', '-c', '/Users/USERNAME/repos/REPONAME/node_modules/.bin/ng generate @nrwl/angular:library --name=test --no-interactive --dry-run', 'ng generate @nrwl/angular:library --name=test --no-interactive --dry-run'" terminated with exit code: 1.

Environment

Old version (where it workded)

  nx : Not Found
  @nrwl/angular : 10.3.1
  @nrwl/cli : 10.3.1
  @nrwl/cypress : 10.3.1
  @nrwl/eslint-plugin-nx : Not Found
  @nrwl/express : Not Found
  @nrwl/jest : 10.3.1
  @nrwl/linter : 10.3.1
  @nrwl/nest : Not Found
  @nrwl/next : Not Found
  @nrwl/node : Not Found
  @nrwl/react : Not Found
  @nrwl/schematics : 8.12.9
  @nrwl/tao : 10.3.1
  @nrwl/web : Not Found
  @nrwl/workspace : 10.3.1
  typescript : 4.0.3

Updated Version (where it breaks):

  nx : Not Found
  @nrwl/angular : 11.3.1
  @nrwl/cli : 11.3.1
  @nrwl/cypress : 11.3.1
  @nrwl/devkit : 11.3.1
  @nrwl/eslint-plugin-nx : Not Found
  @nrwl/express : Not Found
  @nrwl/jest : 11.3.1
  @nrwl/linter : 11.3.1
  @nrwl/nest : Not Found
  @nrwl/next : Not Found
  @nrwl/node : Not Found
  @nrwl/react : Not Found
  @nrwl/schematics : 8.12.9
  @nrwl/tao : 11.3.1
  @nrwl/web : Not Found
  @nrwl/workspace : 11.3.1
  typescript : 4.0.3

our package.json deps:

"dependencies": {
    "@angular/animations": "11.2.1",
    "@angular/cdk": "~11.2.1",
    "@angular/common": "11.2.1",
    "@angular/compiler": "11.2.1",
    "@angular/core": "11.2.1",
    "@angular/flex-layout": "^11.0.0-beta.33",
    "@angular/forms": "11.2.1",
    "@angular/material": "^11.2.1",
    "@angular/platform-browser": "11.2.1",
    "@angular/platform-browser-dynamic": "11.2.1",
    "@angular/router": "11.2.1",
    "@ng-select/ng-select": "^6.1.0",
    "@ngneat/error-tailor": "^1.4.2",
    "@ngneat/transloco": "^2.20.0",
    "@ngneat/transloco-locale": "^1.4.0",
    "@ngrx/component-store": "11.0.0",
    "@ngrx/effects": "11.0.0",
    "@ngrx/entity": "11.0.0",
    "@ngrx/router-store": "11.0.0",
    "@ngrx/store": "11.0.0",
    "@nrwl/angular": "11.3.1",
    "@redux-beacon/google-analytics": "^1.2.3",
    "@redux-beacon/logger": "^1.0.0",
    "@swimlane/ngx-charts": "^17.0.0",
    "angular-feather": "6.0.8",
    "autotrack": "^2.4.1",
    "babylonjs": "4.0.3",
    "babylonjs-gui": "4.0.3",
    "classlist.js": "^1.1.20150312",
    "core-js": "^2.5.4",
    "file-saver": "^2.0.2",
    "immer": "^7.0.8",
    "intersection-observer": "^0.5.1",
    "jquery": "^3.3.1",
    "ng2-signalr": "^10.1.0",
    "ngx-timeago": "^1.0.2",
    "oidc-client": "1.10.1",
    "pepjs": "^0.4.3",
    "redux-beacon": "^2.1.0",
    "rxjs": "6.5.5",
    "signalr": "^2.4.0",
    "whatwg-fetch": "^3.0.0",
    "zone.js": "~0.10.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "0.1102.1",
    "@angular-extensions/pretty-html-log": "^1.0.1",
    "@angular/cli": "11.2.1",
    "@angular/compiler-cli": "11.2.1",
    "@angular/language-service": "11.2.1",
    "@babel/core": "7.8.3",
    "@commitlint/cli": "8.0.0",
    "@commitlint/config-conventional": "8.0.0",
    "@ngneat/spectator": "^7.0.0",
    "@ngrx/schematics": "11.0.0",
    "@ngrx/store-devtools": "11.0.0",
    "@nrwl/cli": "11.3.1",
    "@nrwl/cypress": "11.3.1",
    "@nrwl/jest": "11.3.1",
    "@nrwl/schematics": "^8.12.0",
    "@nrwl/storybook": "11.3.1",
    "@nrwl/tao": "11.3.1",
    "@nrwl/workspace": "11.3.1",
    "@storybook/addon-actions": "^5.3.12",
    "@storybook/addon-knobs": "5.3.9",
    "@storybook/addon-storysource": "^5.3.13",
    "@storybook/angular": "5.3.9",
    "@types/file-saver": "^2.0.1",
    "@types/jasmine": "3.6.4",
    "@types/jasminewd2": "~2.0.3",
    "@types/jest": "26.0.8",
    "@types/jquery": "3.3.6",
    "@types/node": "12.12.38",
    "babel-loader": "8.0.6",
    "codelyzer": "6.0.1",
    "cypress": "^3.8.2",
    "cz-conventional-changelog": "^2.1.0",
    "dotenv": "6.2.0",
    "husky": "2.4.0",
    "jasmine-core": "3.6.0",
    "jasmine-marbles": "0.4.0",
    "jasmine-spec-reporter": "5.0.2",
    "jest": "26.2.2",
    "jest-preset-angular": "8.3.2",
    "jest-ts-auto-mock": "^1.0.10",
    "json-server": "^0.16.1",
    "karma": "5.0.9",
    "karma-chrome-launcher": "3.1.0",
    "karma-coverage-istanbul-reporter": "3.0.3",
    "karma-jasmine": "4.0.1",
    "karma-jasmine-html-reporter": "1.5.4",
    "ng-mocks": "8.0.0",
    "ng-packagr": "11.2.3",
    "prettier": "2.2.1",
    "protractor": "7.0.0",
    "ts-auto-mock": "^1.5.7",
    "ts-jest": "26.4.0",
    "ts-mockito": "^2.5.0",
    "ts-node": "9.1.1",
    "tsickle": ">=0.29.0",
    "tslib": "^2.0.1",
    "tslint": "6.1.3",
    "tslint-import-group-ordering": "^2.1.2",
    "ttypescript": "^1.5.10",
    "typescript": "4.0.3"
  },

As closing notes I want to thank all contributors for their hard work! It tremendously improved our way to work. And that you support such a vast amount of different technologies and environments with such little trouble for us consumers seems to be some kind of magic from the outside :D

Tre665 commented 3 years ago

Ok, I tried some more and found the issue on my side. Somehow there was still an old package in that lead to the issue "@nrwl/schematics": "^8.12.0", Found it out when I wanted to move our solution to a new workspace.
I am very sorry if the creation of that issue has lead to confusion/inconveniences for you :(

I am not sure if that is the regular way how it should be done, but I would close that issue now.

github-actions[bot] commented 1 year 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.