angular / angular-cli

CLI tool for Angular
https://cli.angular.dev
MIT License
26.79k stars 11.98k forks source link

Custom builders using @angular-devkit/build-angular cannot resolve downstream packages when compiled using tsc #27902

Open bowles-tyler-b opened 5 months ago

bowles-tyler-b commented 5 months ago

Command

other

Is this a regression?

The previous version in which this bug was not present was

17.3

Description

We have a custom builder for our angular project that has worked smoothly since Angular 8. We recently upgraded from Angular 15 to Angular 18 and tsc now throws errors when attempting to compile the builder. The errors thrown suggest changing the "moduleResolution" setting in tsconfig.builders.json, but each setting results in different downstream module resolution problems in the @angular-devkit/build-angular package.

Example error:

node_modules/@angular-devkit/build-angular/src/builders/browser/index.d.ts:8:36 - error TS2307: Cannot find module '@angular/build/private' or its corresponding type declarations. There are types at '<path/to/project>/node_modules/@angular/build/src/private.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'. 8 import { IndexHtmlTransform } from '@angular/build/private';

The full set of errors is pasted below.

I've attempted to change tsconfig.builders.json to use

But each of these yields a different set of errors.

Minimal Reproduction

Minimal reproduction repository: https://github.com/bowles-tyler-b/Angular8-failed-tsc-builder

After running "npm install", run the command "npm run compile" to reproduce the errors

Exception or Error

node_modules/@angular-devkit/build-angular/src/builders/browser/index.d.ts:8:36 - error TS2307: Cannot find module '@angular/build/private' or its corresponding type declarations.
  There are types at '<path/to/project>/node_modules/@angular/build/src/private.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.

8 import { IndexHtmlTransform } from '@angular/build/private';
                                     ~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@angular-devkit/build-angular/src/builders/dev-server/builder.d.ts:9:45 - error TS2307: Cannot find module '@angular/build' or its corresponding type declarations.
  There are types at '<path/to/project>/node_modules/@angular/build/src/index.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.

9 import type { DevServerBuilderOutput } from '@angular/build';
                                              ~~~~~~~~~~~~~~~~

node_modules/@angular-devkit/build-angular/src/builders/dev-server/builder.d.ts:10:41 - error TS2307: Cannot find module '@angular/build/private' or its corresponding type declarations.
  There are types at '<path/to/project>/node_modules/@angular/build/src/private.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.

10 import { type IndexHtmlTransform } from '@angular/build/private';
                                           ~~~~~~~~~~~~~~~~~~~~~~~~

node_modules/@angular-devkit/build-angular/src/builders/dev-server/index.d.ts:8:40 - error TS2307: Cannot find module '@angular/build' or its corresponding type declarations.
  There are types at '<path/to/project>/node_modules/@angular/build/src/index.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.

8 import { DevServerBuilderOutput } from '@angular/build';
                                         ~~~~~~~~~~~~~~~~

node_modules/@angular-devkit/build-angular/src/index.d.ts:11:61 - error TS2307: Cannot find module '@angular/build' or its corresponding type declarations.
  There are types at '<path/to/project>/node_modules/@angular/build/src/index.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.

11 export { buildApplication, ApplicationBuilderOptions } from '@angular/build';
                                                               ~~~~~~~~~~~~~~~~

node_modules/@angular-devkit/build-angular/src/tools/webpack/utils/stats.d.ts:8:40 - error TS2307: Cannot find module '@angular/build/private' or its corresponding type declarations.
  There are types at '<path/to/project>/node_modules/@angular/build/src/private.d.ts', but this result could not be resolved under your current 'moduleResolution' setting. Consider updating to 'node16', 'nodenext', or 'bundler'.

8 import { BudgetCalculatorResult } from '@angular/build/private';
                                         ~~~~~~~~~~~~~~~~~~~~~~~~

Found 6 errors in 5 files.

Errors  Files
     1  node_modules/@angular-devkit/build-angular/src/builders/browser/index.d.ts:8
     2  node_modules/@angular-devkit/build-angular/src/builders/dev-server/builder.d.ts:9
     1  node_modules/@angular-devkit/build-angular/src/builders/dev-server/index.d.ts:8
     1  node_modules/@angular-devkit/build-angular/src/index.d.ts:11
     1  node_modules/@angular-devkit/build-angular/src/tools/webpack/utils/stats.d.ts:8

Your Environment

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/

Angular CLI: 18.0.5
Node: 20.14.0
Package Manager: npm 10.7.0
OS: win32 x64

Angular: 18.0.4
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1800.5
@angular-devkit/build-angular   18.0.5
@angular-devkit/core            18.0.5
@angular-devkit/schematics      18.0.5
@angular/cli                    18.0.5
@schematics/angular             18.0.5
rxjs                            7.8.1
typescript                      5.4.5
zone.js                         0.14.7

Anything else relevant?

Adding skipLibCheck to the ts compiler options will suppress the error; this is a workaround now, but reduces type safety in the custom builder.

onechvoglod commented 4 months ago

Have you found a solution to this problem?

zwarag commented 4 months ago

Any plans or idea if/when this will be addressed?

Tsuna58 commented 2 months ago

I'm fully stuck because of this problem. Any idea of workaround, without downgrading angular to 17 ?

alan-agius4 commented 2 months ago

You could use skipLibCheck as a workaround.

bowles-tyler-b commented 2 months ago

You could use skipLibCheck as a workaround.

Yes, I mentioned that in the original post. This is what we're doing for now, but it does of course reduce type safety.

Tsuna58 commented 2 months ago

Thank you ! I hadn't seen it. You've just saved my evening.

shaheedac commented 3 weeks ago

Yo can use npm i --legacy-peer-deps true