angular / angular/angular-cli

ng add fails to find app.module.ts when using custom tsconfig.json paths

オープン
#12,740 コメント 11 件 リアクション 12 件 担当者 0 名 GitHub で見る
area: @schematics/angular freq1: low severity3: broken type: bug/fix
主要言語
TypeScript
スター
27k
フォーク
11.8k
平均マージ
14時間 23分
マージ済み PR(30日)
162

説明

### Bug Report or Feature Request (mark with an `x`)
```
- [X ] bug report -> please search issues before submitting
- [X ] feature request
```
Whether this is a bug or a feature depends on whether or not custom `paths` in `tsconfig.json` are supported with the Cli.

### Command (mark with an `x`)
```
- [X ] add
```

### Versions
Angular CLI: 7.0.2
Node: 10.8.0
OS: win32 x64
Angular: 7.0.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

### Repro steps
- `ng new app-name` (pick defaults)
- Add `"paths": { "@app/*": ["src/app/*"] }` to `tsconfig.json`
- Update `main.ts` to use this new path
`import { AppModule } from '@app/app.module';`
`import { environment } from './environments/environment';`
- `ng add @angular/material`
- Select `Yes` for `? Set up browser animations for Angular Material?`

Repository all set up to reproduce this error is available [here](https://github.com/seangwright/ng-cli-add-error-with-tsconfig-paths).
Just clone and type `ng add @angular/material`.

### The log given by the failure
`Could not read Angular module file: /src/@app/app.module.ts`

### Desired functionality
Cli handles custom paths in `tsconfig.json`.

### Mention any other details that might be useful
Here is where material tries to get the `app.module.ts` path
https://github.com/angular/material2/blob/985774a4eaa14d1dcbf1ad96ab176043d38f433e/src/lib/schematics/ng-add/setup-project.ts#L56

Here is where the path to `app.module.ts` is being calculated in the schematics code
https://github.com/angular/angular-cli/blob/82f2bda2f59e4665611d1a75b51a62bae21ac340/packages/schematics/angular/utility/ng-ast-utils.ts#L78

I believe the above link is what is causing the problem.

I've always preferred absolute urls in Angular apps compared to the relative paths (which become very difficult to read for nested components).

Adding custom `paths` to `tsconfig.json` works pretty well to solve this problem. VSCode works fine with it and the Cli has no other issues I've run into.

If using `paths` isn't officially supported does the Angular team recommend using relative paths like `import { MyApiService } from '../../../../../my-api.service'`?

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

packages/schematics/angular/utility/ng-ast-utils.ts から始め、次に material2 の setup-project.ts が計算された app.module.ts のパスをどのように使用しているかを比較します。tsconfig.json のパスを設定した後に ng add @angular/material を実行して、リンク先のリポジトリで失敗を再現します。main.ts が @app/app.module を通じてモジュールをインポートする場合に、コマンドがモジュールを正常に見つけられれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
angular, typescript
領域
cli, tooling
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。