Title: Apps cannot build with AOT if a dependent library was built with barrels · Issue #23713 · angular/angular · GitHub
Open Graph Title: Apps cannot build with AOT if a dependent library was built with barrels · Issue #23713 · angular/angular
X Title: Apps cannot build with AOT if a dependent library was built with barrels · Issue #23713 · angular/angular
Description: This issue describes mysterious build failures for apps that depend on an npm-installed library that was built with barrels. A barrel is an index.ts file that consolidates exported symbols of several nested files. The problem lies somewh...
Open Graph Description: This issue describes mysterious build failures for apps that depend on an npm-installed library that was built with barrels. A barrel is an index.ts file that consolidates exported symbols of sever...
X Description: This issue describes mysterious build failures for apps that depend on an npm-installed library that was built with barrels. A barrel is an index.ts file that consolidates exported symbols of sever...
Opengraph URL: https://github.com/angular/angular/issues/23713
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Apps cannot build with AOT if a dependent library was built with barrels","articleBody":"This issue describes mysterious build failures for apps that depend on an npm-installed library that was built with barrels.\r\n\r\n>A barrel is an `index.ts` file that consolidates exported symbols of several nested files.\r\n\r\nThe problem lies somewhere in the interaction among barrels, AOT, and `ngPackagr`.\r\n\r\n**Workaround: Do not use barrels in your library. Always and everywhere make direct references to the source files.**\r\n\r\n## I'm submitting a...\r\n\u003c!-- Check one of the following options with \"x\" -->\r\n\u003cpre>\u003ccode>\r\n[ ] Regression (a behavior that used to work and stopped working in a new release)\r\n[x] Bug report \u003c!-- Please search GitHub for a similar issue or PR before submitting -->\r\n[ ] Performance issue\r\n[ ] Feature request\r\n[ ] Documentation issue or request\r\n[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question\r\n[ ] Other... Please describe:\r\n\u003c/code>\u003c/pre>\r\n\r\n## Current behavior\r\n\r\n`ng build --AOT` of an app fails with absurd errors concerning a library installed with npm.\r\n\r\nThat library was built with barrels. \r\n\r\nSpecifically, the top-level `index.ts` that tells `ngPackagr` which artifacts to include in the library's public API was built with barrels.\r\n\r\nThe library author (me) was able to build that library for production without any warnings or errors. \r\nWorse, it seemed to work when used in most applications that installed the library.\r\n\r\nBut occasionally you'd get weird errors when you `ng build --aot` the application, such as\r\n\r\n```\r\nERROR in ./src/client/app/test-users/test-users.module.ngfactory.js\r\nModule not found: Error: Can't resolve 'ngrx-data/selectors/index' in '/Users/Ward/_git/sp-x/src/client/app/test-users'\r\n```\r\nNote the reference in the error to `index` in 'ngrx-data/selectors/index'. That's a barrel file the wasn't directly exposed publicaly.\r\n```\r\nERROR in app/store/app-pluralizer.ts(17,13): Error during template compile of 'AppPluralizer'\r\n Only initialized variables and constants can be referenced in decorators because the value of this variable is needed by the template compiler in 'PLURAL_NAMES_TOKEN'\r\n 'PLURAL_NAMES_TOKEN' references 'PLURAL_NAMES_TOKEN'\r\n 'PLURAL_NAMES_TOKEN' references 'PLURAL_NAMES_TOKEN'\r\n 'PLURAL_NAMES_TOKEN' is not initialized at ../../ngrx-data/utils/interfaces.ts(13,22).\r\n```\r\nThe constant _was initialized_ ... on **line 16** in `ngrx-data/utils/interfaces.ts`.\r\nNotice the line number is **13** in the error message. Thats where the token is _declared_ in the `d.ts` type definition file (`interfaces.d.ts`), not the `.ts` source file. \r\n\r\nShuffling the order of exports in the top level `index.ts` would fix one use case but trigger another.\r\n\r\nThese are all clues that **you are using barrels and AOT + ngPackagr` do not like barrels**.\r\n\r\n## Expected behavior\r\n\r\nI think we should be able to use barrels.\r\n\r\nAOT + ngPackagr _should be able to determine the proper dependency order_ as is possible with JIT compilation.\r\n\r\n## Minimal reproduction of the problem with instructions\r\n\r\nThere is no easy way to reproduce this bad behavior because barrels often appear to work. The problem likely arises when a library's sub-files refer to files that are exported in other barrels (even when those references are direct to the dependent files, by-passing barrels).\r\n\r\nYou can see the horror story play-out in the `1.0.0-beta.12` and `1.0.0-beta.13` releases of https://github.com/johnpapa/angular-ngrx-data.\r\n\r\nThis repo has both a library (`/lib`) and an example that uses that library (`/src/client`).\r\n\r\nTo experience the problem:\r\n* clone `https://github.com/johnpapa/angular-ngrx-data`\r\n* go back to the [Beta 12 commit](https://github.com/johnpapa/angular-ngrx-data/releases/tag/1.0.0-beta.12)\r\n* `npm install`\r\n* `npm run build-all` # this should succeed.\r\n* Edit `lib/src/index.ts`\r\n* Move `export * from './utils';` to the bottom of the file\r\n* `npm run build-lib` # rebuilds the library\r\n* `ng build --aot` # now it fails with the strange `InjectionToken` error ^^^\r\n\r\nYou can build with AOT if you remove the following providers from `src/client/app/store/entity-store.module.ts`\r\n```\r\n { provide: Logger, useClass: AppLogger },\r\n { provide: Pluralizer, useClass: AppPluralizer }\r\n```\r\nThis succeeds because these providers reference the library's `Logger` and `Pluralizer` abstract classes in `./util` (the barrel).\r\nRemoving them by-passes the library reference lookup that produces the errors.\r\n\r\n>Pragmatically, this means that the library author (me) won't discover there is a problem until some consumer (you) tries to override the default logger and pluralizer behaviors by supplying alternative implementations through this official extension point.\r\n\r\n* revert change to `lib/src/index.ts`.\r\n* `npm run build-lib` # rebuilds the library\r\n* `ng build --aot` # build succeeds\r\n\r\nA dev build (e.g, `ng serve`) works fine at any point in this process.\r\n\r\nIf you now move forward to [Beta 13](https://github.com/johnpapa/angular-ngrx-data/releases/tag/1.0.0-beta.13) where barrels are eliminated, all of these problems go away and you can build the sample app with `ng build --aot` at any point.\r\n\r\n## What is the motivation / use case for changing the behavior?\r\n\r\nBarrels nicely encapsulate and minimize low-level details of a library's internal folder structure.\r\nThey work great with JIT. Obviously not with AOT at this time.\r\n\r\n## Environment\r\n\r\n\u003cpre>\u003ccode>\r\nAngular version: 5.2.10\r\nngPackagr version: 2.4.2\r\n\u003c/pre>\u003c/code>\r\n\r\nI do not know if it is still a problem in v.6. I don't have the time to find out.\r\n\r\nBrowser: _Irrelevant_\r\n\r\nFor Tooling issues: _irrelevant_\r\n\r\n","author":{"url":"https://github.com/wardbell","@type":"Person","name":"wardbell"},"datePublished":"2018-05-04T17:05:58Z","url":"https://github.com/angular/angular/issues/23713"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:de201b99-4e6a-0714-8ae8-6ef487880108 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C33E:1087D4:92EEF2:C5876D:6B00E89F |
| html-safe-nonce | 495410737878d2bf19f63e48288eba1219cc72f7a6448f9d3c225c717f767583 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzNFOjEwODdENDo5MkVFRjI6QzU4NzZEOjZCMDBFODlGIiwidmlzaXRvcl9pZCI6Ijc4NDc0NzM3NjM1MjMyOTEyOTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bf5b2674e66d883aef8e1d47ab64d325b6c97af12b807cde01913af8dd019558 |
| hovercard-subject-tag | issue:320351363 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/23713/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7798b5ef3feb7be6bbc3654a5a7e7d447962698fdf5a3f418311162989d7a9c0/angular/angular/issues/23713 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7798b5ef3feb7be6bbc3654a5a7e7d447962698fdf5a3f418311162989d7a9c0/angular/angular/issues/23713 |
| og:image:alt | This issue describes mysterious build failures for apps that depend on an npm-installed library that was built with barrels. A barrel is an index.ts file that consolidates exported symbols of sever... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | wardbell |
| hostname | github.com |
| expected-hostname | github.com |
| None | 27088b6977484d0d3321d9a11c907c6d6637b315cfa359ef7952dbf6292f9b14 |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 624179fb8b88d26ba5bba08894eb60a1c5a1d93d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width