angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Angular 14 doesn't build library with multiple entry points

Open mgameover opened this issue 4 years ago • 10 comments

Which @angular/* package(s) are the source of the bug?

compiler-cli, compiler, upgrade

Is this a regression?

Yes

Description

Hi everyone, I am about to migrate from v13 to v14, but I faced with the following issue. When I try to build library project it fails on the first entry point which has reference to another entry with the error:

------------------------------------------------------------------------------
Building entry point 'xxx'
------------------------------------------------------------------------------
✖ Compiling with Angular sources in Ivy partial compilation mode.
PATH_TO_TS - error TS2305: Module '"yyy"' has no exported member 'xyz'.

But the thing is that everything is in place and it works fine with ng v13. Moreover ng serve is working fine, but not the build.

I created a repo for this issue, main branch contains working version (ng v13) and ng-14 branch has the same code after ng update @angular/core@14 @angular/cli@14 with the issue.

To reproduce the issue.

  1. Clone main branch
  2. Make sure that it builds the lib by running npm run build-components (ng build components)
  3. Update angular to v14 by running ng update @angular/core@14 @angular/cli@14
  4. Build component library npm run build-components

Result: failed to build

Any thoughts?

Please provide a link to a minimal reproduction of the bug

https://github.com/mgameover/app-n-lib/tree/ng-14

Please provide the exception or error you saw

> ng build components

------------------------------------------------------------------------------
Building entry point '@my/components'
------------------------------------------------------------------------------
✔ Compiling with Angular sources in Ivy partial compilation mode.
✔ Writing FESM bundles
✔ Copying assets
✔ Writing package manifest
✔ Built @my/components

------------------------------------------------------------------------------
Building entry point '@my/components/shared'
------------------------------------------------------------------------------
✔ Compiling with Angular sources in Ivy partial compilation mode.
✔ Writing FESM bundles
✔ Built @my/components/shared

------------------------------------------------------------------------------
Building entry point '@my/components/component'
------------------------------------------------------------------------------
✖ Compiling with Angular sources in Ivy partial compilation mode.
projects/components/component/components.component.ts:3:10 - error TS2305: Module '"@my/components/shared"' has no exported member 'isDefined'.

3 import { isDefined } from '@my/components/shared';

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 14.0.0
Node: 16.14.2
Package Manager: npm 8.5.5
OS: win32 x64

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

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1400.0
@angular-devkit/build-angular   14.0.0
@angular-devkit/core            14.0.0
@angular-devkit/schematics      14.0.0
@schematics/angular             14.0.0
ng-packagr                      14.0.0
rxjs                            7.5.5
typescript                      4.6.4

Anything else?

No response

mgameover avatar Jun 03 '22 21:06 mgameover

The cause of the problem here is that there is an index file (https://github.com/mgameover/app-n-lib/blob/ng-14/projects/components/shared/index.ts ) which exports all the symbols but it is not mark as the entry-point.

This causes a recursive exports of itself, as now in version 14 when the entry-point is something other than an index.ts an index file is generated.

We should handle this case. As a workaround for the time being, you can move the contents of the index in the public-api file.

alan-agius4 avatar Jun 04 '22 19:06 alan-agius4

Hi @alan-agius4, Thanks for the quick response.

The suggested workaround works for library but it breaks local app since there is no index.ts all imports cannot be resolved. You can reproduce that in the same repo by moving all exports from index.ts to public-api.ts and deleting index.ts from shared entry point:

./projects/components/component/components.component.ts:1:0-50 - Error: Module not found: Error: Can't resolve '@my/components/shared' in 'C:\Sources\app-n-lib\projects\components\component'

Error: projects/components/component/components.component.ts:3:27 - error TS2307: Cannot find module '@my/components/shared' or its corresponding type declarations.

3 import { isDefined } from '@my/components/shared'

It looks like index files are not generated when application is being built.

anyway I updated path section in tsconfig.app.json to

      "@my/components/*": [
        "./projects/components/*/public-api.ts"
      ]

and that helped.

mgameover avatar Jun 06 '22 14:06 mgameover

It looks like the path mapping is pointing to the source of the library and not the built version in the dist.

alan-agius4 avatar Jun 06 '22 14:06 alan-agius4

yes, path mapping points to source and that's done intentionally - changes in the lib are picked up automatically so you don't need to rebuild lib everyone.

mgameover avatar Jun 06 '22 17:06 mgameover

Hi @alan-agius4, As I can see from the changelog, the fix has been released in v14.0.1. However I don't see any changes. I tried to update the same repo, I can see that the version is changed to v14.0.1 but it doesn't solve the issue.

So my question is - what is a proper way to handle this?

  • Remove index.ts
  • or rename public-api.ts to index.ts and update ng-package.json
  • or there will be another fix

mgameover avatar Jun 10 '22 14:06 mgameover

In your particular case the best action would be to rename public-api.ts to index.ts and update ng-package.json

alan-agius4 avatar Jun 10 '22 14:06 alan-agius4

It actually looks like 14.0.1 didn't contain the change mentioned in the ng-packagr changelog. I will cut another ng-packagr release later this week.

On Fri, 10 Jun 2022 at 16:05, Alex @.***> wrote:

Hi @alan-agius4 https://github.com/alan-agius4, As I can see from the changelog https://github.com/ng-packagr/ng-packagr/blob/main/CHANGELOG.md, the fix has been released in v14.0.1. However I don't see any changes. I tried to update the same repo, I can see that the version is changed to v14.0.1 but it doesn't solve the issue.

So my question is - what is a proper way to handle this?

  • Remove index.ts
  • or rename public-api.ts to index.ts and update ng-package.json
  • or there will be another fix

— Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/23293#issuecomment-1152399068, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEF74WTJIXHW2IFUGP3GI3LVONDTDANCNFSM5X34YVVA . You are receiving this because you were mentioned.Message ID: @.***>

alan-agius4 avatar Jun 10 '22 14:06 alan-agius4

Ng-packagr version 14.0.2 has been released.

alan-agius4 avatar Jun 10 '22 18:06 alan-agius4

For anyone coming across this with a slightly different error

Entry point 'my-lib' has an 'index.ts' parallel to the 'entryFilePath'. The 'entryFilePath' should be updated to point to the 'index.ts' file.

Make sure you don't have internal imports in the library, that point to the main index.ts file of the lib.

dmitry-stepanenko avatar Jun 29 '22 11:06 dmitry-stepanenko

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.