Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate library with --routing fails silently #742

Closed
luchsamapparat opened this issue Sep 3, 2018 · 7 comments
Closed

generate library with --routing fails silently #742

luchsamapparat opened this issue Sep 3, 2018 · 7 comments

Comments

@luchsamapparat
Copy link
Contributor

luchsamapparat commented Sep 3, 2018

Hi,

unfortunately, I cannot provide a project to reproduce the issue right now. However, I've been debugging the issue and maybe someone has an idea what's going on.

Basically, I try to create a new library with routing:

ng generate library administration --routing

I narrowed the issue down to the routing param and started debugging the schematic. The execution fails in insertAfterLastOccurrence which throws an empty error. However, the problem is not within that method. the nodes argument is an empty array.

export function insertAfterLastOccurrence(nodes: ts.Node[],
                                          toInsert: string,
                                          file: string,
                                          fallbackPos: number,
                                          syntaxKind?: ts.SyntaxKind): Change {
  // sort() has a side effect, so make a copy so that we won't overwrite the parent's object.
  let lastItem = [...nodes].sort(nodesByPosition).pop();
  if (!lastItem) {
    throw new Error();
  }

https://github.com/angular/angular-cli/blob/d202480a1707be6575b2c8cf0383cfe6db44413c/packages/schematics/angular/utility/ast-utils.ts#L195

This is the stack trace:

Error
    at insertAfterLastOccurrence (<project>\node_modules\@schematics\angular\utility\ast-utils.js:160:15)
    at Object.insertImport (<project>\node_modules\@schematics\angular\utility\ast-utils.js:69:12)
    at <project>\node_modules\@nrwl\schematics\src\collection\library\index.js:39:29
    at MergeMapSubscriber.input.pipe.operators_1.mergeMap.inputTree [as project] (<project>\node_modules\@angular-devkit\schematics\src\rules\call.js:74:24)
    at MergeMapSubscriber._tryNext (<project>\node_modules\rxjs\internal\operators\mergeMap.js:65:27)
    at MergeMapSubscriber._next (<project>\node_modules\rxjs\internal\operators\mergeMap.js:55:18)
    at MergeMapSubscriber.Subscriber.next (<project>\node_modules\rxjs\internal\Subscriber.js:64:18)
    at MergeMapSubscriber.notifyNext (<project>\node_modules\rxjs\internal\operators\mergeMap.js:84:26)
    at InnerSubscriber._next (<project>\node_modules\rxjs\internal\InnerSubscriber.js:25:21)
    at InnerSubscriber.Subscriber.next (<project>\node_modules\rxjs\internal\Subscriber.js:64:18)

It happens when addRouterConfiguration calls insertImport with this source file:

      import { NgModule } from '@angular/core';
      import { CommonModule } from '@angular/common';
      @NgModule({
        imports: [
          CommonModule
        ]
      })
      export class AdministrationModule { }

https://github.com/nrwl/nx/blob/master/packages/schematics/src/collection/library/index.ts#L90

The insertImport method then retrieves all imports via findNodes:

const allImports = findNodes(rootNode, ts.SyntaxKind.ImportDeclaration);

https://github.com/angular/angular-cli/blob/d202480a1707be6575b2c8cf0383cfe6db44413c/packages/schematics/angular/utility/ast-utils.ts#L24

However, that methods returns an empty result in the error thrown as mentioned initially.

What really confuses me is the SyntaxKind enumeration. While debugging, it says SyntaxKind.ImportDeclaration has a value of 239.

image

However, it seems that that number changes with every TypeScript release:

ImportDeclaration = 242,
https://github.com/Microsoft/TypeScript/blob/release-2.8/lib/typescript.d.ts#L306

ImportDeclaration = 243,
https://github.com/Microsoft/TypeScript/blob/release-2.9/lib/typescript.d.ts#L308

ImportDeclaration = 247,
https://github.com/Microsoft/TypeScript/blob/release-3.0/lib/typescript.d.ts#L322

Isn't that pretty fragile? Am I missing something here?

In my project I am using...

TypeScript 2.9.2
@nrwl/schematics 6.2.1
@schematics/angular 0.7.2

@luchsamapparat
Copy link
Contributor Author

luchsamapparat commented Sep 4, 2018

I had another look and the 239 value for ImportDeclaration comes from the typescript dependency of the @schematics/angular package which is 2.7.2.

node_modules/@schematics/angular/node_modules/typescript/lib/typescript.js

So it seems that for the generation of the AST another TypeScript version is used.

image

Looking at the actual kind values of the first-level children during runtime (243, 243 and 234) it seems to be TypeScript 2.9.

@luchsamapparat
Copy link
Contributor Author

luchsamapparat commented Sep 4, 2018

Okay, it seems my @schematics/angular is outdated… I fixed it by removing node_modules (not sure if necessary) and then running npm i @angular/[email protected]. Previously, it was 6.1.2.

Nevertheless, it would be an improvement to somehow notify the user that the dependencies don't match 😕

@skydever
Copy link
Contributor

hi @luchsamapparat

I also had this error and I realized that quite late after changing the dependency versions, did not create an app or lib soon afterwards (but I will test that in the future after updating a dependency). I had to play around with dependency versions concerning rxjs, @angular-devkit/build-angular, @angular/cli and typescript to get everything working. Once creating a lib worked creating an app did not, and I moved on ... I came across #755 and Angular CLI #12262 too on that journey.

Now I am finally using

Angular CLI: 6.1.3
Node: 8.10.0
OS: win32 x64
Angular: 6.1.8
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                            Version
------------------------------------------------------------
@angular-devkit/architect          0.7.5
@angular-devkit/build-angular      0.7.5
@angular-devkit/build-optimizer    0.7.5
@angular-devkit/build-webpack      0.7.5
@angular-devkit/core               0.7.5
@angular-devkit/schematics         0.7.3
@angular/cdk                       6.4.2
@angular/cli                       6.1.3
@angular/flex-layout               6.0.0-beta.18
@angular/material                  6.4.2
@angular/material-moment-adapter   6.4.2
@ngtools/webpack                   6.1.5
@schematics/angular                0.7.3
@schematics/update                 0.7.3
rxjs                               6.2.2
typescript                         2.9.2
webpack                            4.9.2

and everything seems ok. What versions are you using at the moment?

Having a list of compatible versions would be great, because somehow the semver workflow seems not to work as I would expect it to, or I missed something, I am interpreting it the wrong way - features depending on features from other libs without a a major upgrade (I guess that is the problem? or just unwanted bugs like mentioned issues from above - such a bug is also the case why I am on @angular/[email protected]), I did not change any major version ...

@bcabanes
Copy link
Member

bcabanes commented Oct 4, 2018

Hi @luchsamapparat,
Have you tried to update your Nx version to the latest? We have updated some dependencies recently.

If you still have the problem, could you please provide a reproduction of the issue?
Reproduction with the latest version of Nx.

Thank you.

@luchsamapparat
Copy link
Contributor Author

luchsamapparat commented Oct 12, 2018

@bcabanes I updated my project to Angular 6.1, Angular CLI 6.2.5 and Nx 6.4 and ng generate library dummy --routing runs smoothly.

However, now create-nx-workspace is failing silently.

PS C:\Users\XXX\playground-nx> create-nx-workspace playground-nx
Creating a sandbox with the CLI and Nx Schematics...
added 355 packages from 243 contributors and audited 15226 packages in 42.115s
found 0 vulnerabilities

ng new "playground-nx" --collection=@nrwl/schematics

However, the directory is empty after the command finishes.

I don't know if this is related or not.

My global packages:

Creating an Angular Workspace first and then adding Nx works…

ng new "playground-nx"
ng add @nrwl/schematics

@bcabanes
Copy link
Member

Hi @luchsamapparat!
Glad to read that your problem was resolved by updating your dependencies.

Concerning the new issue you encounter, I think we have new separate ticket available here #817 so I will close this one.

@github-actions
Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators Mar 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants