Title: unit-test builder: "sideEffects": false elides statically-referenced barrel modules, component reads as undefined in TestBed · Issue #33910 · angular/angular-cli · GitHub
Open Graph Title: unit-test builder: "sideEffects": false elides statically-referenced barrel modules, component reads as undefined in TestBed · Issue #33910 · angular/angular-cli
X Title: unit-test builder: "sideEffects": false elides statically-referenced barrel modules, component reads as undefined in TestBed · Issue #33910 · angular/angular-cli
Description: Which @angular/* package(s) are the source of the bug? build Is this a regression? Yes, this behaviour used to work in version 21.2.19 Description In a multi-entry-point library built with ng-packagr, @angular/build:unit-test (Vitest + j...
Open Graph Description: Which @angular/* package(s) are the source of the bug? build Is this a regression? Yes, this behaviour used to work in version 21.2.19 Description In a multi-entry-point library built with ng-packa...
X Description: Which @angular/* package(s) are the source of the bug? build Is this a regression? Yes, this behaviour used to work in version 21.2.19 Description In a multi-entry-point library built with ng-packa...
Opengraph URL: https://github.com/angular/angular-cli/issues/33910
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"unit-test builder: \"sideEffects\": false elides statically-referenced barrel modules, component reads as undefined in TestBed","articleBody":"# Which @angular/* package(s) are the source of the bug?\n\nbuild\n\n# Is this a regression?\n\nYes, this behaviour used to work in version 21.2.19\n\n# Description\n\nIn a multi-entry-point library built with ng-packagr, `@angular/build:unit-test` (Vitest + jsdom) elides module bodies from the primary entry point's `export *` barrel graph when the library's `package.json` declares `\"sideEffects\": false`. The namespace re-export accessors for those modules survive and return `undefined`.\n\nWhen one of the dropped exports is a standalone component referenced from a **retained** `imports: [...]` array literal of another component, `TestBed.configureTestingModule` throws:\n\n```\nTypeError: Cannot read properties of undefined (reading 'ɵcmp')\n at getComponentDef (core/testing/src/test_bed_compiler.ts:1169)\n at isStandaloneComponent (core/testing/src/test_bed_compiler.ts:1162)\n at queueTypesFromModulesArrayRecur\n at TestBedCompiler.queueTypesFromModulesArray\n at TestBedCompiler.configureTestingModule\n```\n\nDropping a module whose export is statically referenced from a retained array literal looks incorrect regardless of `sideEffects`, which is a module-level assertion about *import-for-side-effect* semantics — not a licence to remove a module that is still statically reachable.\n\nIn our library (761 barrel exports across 4 entry points) this fails 36 specs in 3 files, all in one secondary entry point. On Angular 21.2.19 the identical suite is 389/389 green.\n\n## Not a duplicate of #33728\n\n[#33728](https://github.com/angular/angular-cli/issues/33728) describes the same *symptom* (an export from a shared chunk reading as `undefined` under the jsdom runner) and was fixed by [#33729](https://github.com/angular/angular-cli/pull/33729) \"disable code splitting for unit test builds\", released in v22.1.5.\n\nThat fix is **installed and active** here and the failure still reproduces. Verified in the installed package:\n\n- `src/builders/unit-test/runners/vitest/build-options.js:238` sets `disableCodeSplitting: true`\n- honoured at `src/tools/esbuild/application-code-bundle.js:55-58` (`buildOptions.splitting = false`)\n\nSo this is a distinct cause: not cross-chunk lazy initialisers, but whole-module elision driven by `sideEffects`.\n\n# Please provide a link to a minimal reproduction of the bug\n\nUnable to share the affected codebase. Reproduction recipe below — it is deterministic and the toggle is a single field.\n\n**Setup**\n\n1. `ng new repro --no-standalone=false` then `ng generate library lib`, `@angular/build` >= 22.1.5, TypeScript 6.0.3, Node 22.\n2. Add `\"sideEffects\": false` to `projects/lib/package.json`.\n3. In the **primary** entry, create a standalone `FooComponent` and export it through a **multi-level** barrel: `public-api.ts` → `export * from './lib/components/public-api-components'` → `export * from './foo/foo.component'`. At least two levels of `export *` matter; a single flat barrel did not reproduce for us.\n4. Add a **secondary** entry point (`projects/lib/sub/` with its own `ng-package.json` and `public-api.ts`), mapped in the workspace `tsconfig.json` `paths` to its **source** `public-api.ts`, alongside `\"@lib\": [\"./projects/lib/src/public-api.ts\"]`.\n5. In the secondary entry, create a standalone `BarComponent` with `imports: [FooComponent]`, importing it as `import {FooComponent} from '@lib'`.\n6. Give `BarComponent` a spec doing `TestBed.configureTestingModule({imports: [BarComponent]})`.\n7. Configure a test target with `@angular/build:unit-test`, `buildTarget` pointing at the library build, and `include` covering the secondary entry's specs. **Run the whole suite, not a single file** — see below.\n\n**Expected:** spec passes.\n**Actual:** `TypeError: Cannot read properties of undefined (reading 'ɵcmp')`.\n\n**Toggle:** change `projects/lib/package.json` to `\"sideEffects\": true` (or a glob matching the source tree, e.g. `[\"./src/**\", \"./*/src/**\"]`) → passes. Change it back → fails. Deterministic in both directions.\n\n## Diagnostics that pin the mechanism\n\nThese are the observations that distinguish elision from TDZ, resolution failure, or interop snapshotting:\n\n1. **The module body never runs.** Injecting an unconditional `(globalThis as any).__RAN = true` at the top of an affected module leaves `__RAN` `undefined` at runtime, while a control module in the same barrel has its marker set. A module containing an unconditional global assignment can only be removed if it is declared side-effect-free.\n2. **Not TDZ.** On `import * as barrel from '@lib'`, the affected names are present as **ACCESSOR** property descriptors and still return `undefined` after both a microtask and a macrotask tick.\n3. **Not duplicate module identity.** `directImport === barrel.Foo` is `true`.\n4. **Only classes are affected in practice.** Plain functions exported from the same barrel in the same import statement resolve correctly; the value read as `undefined` is the component class. In our failing component, `ɵcmp.dependencies` was `[\"UNDEFINED\", \"TranslatePipe\"]` — the third-party pipe resolved, the barrel-imported component did not.\n5. **Whole-suite dependent.** Running the affected spec alone via `--include` passes; only the full multi-file run drops the module. The set of `undefined` exports also changes with import order — consistent with per-entry retention analysis rather than a fixed graph defect.\n6. **Not specific to one entry point.** A barrel probe placed in a different, CommonJS-free secondary entry returns the *identical* set of `undefined` exports. Whether a given spec fails depends on which barrel modules that entry's retention set happens to keep.\n\n# Please provide the exception or error you saw\n\n```\nTypeError: Cannot read properties of undefined (reading 'ɵcmp')\n at getComponentDef (../packages/core/testing/src/test_bed_compiler.ts:1169:24)\n at isStandaloneComponent (../packages/core/testing/src/test_bed_compiler.ts:1162:14)\n at queueTypesFromModulesArrayRecur (../packages/core/testing/src/test_bed_compiler.ts:847:23)\n at TestBedCompiler.queueTypesFromModulesArray (../packages/core/testing/src/test_bed_compiler.ts:861:4)\n at TestBedCompiler.configureTestingModule (../packages/core/testing/src/test_bed_compiler.ts:226:11)\n at TestBedImpl.configureTestingModule (../packages/core/testing/src/test_bed.ts:594:18)\n```\n\n# Please provide the environment you discovered this bug in (run: ng version)\n\n```\nAngular CLI: 22.1.5\nNode: 22.23.1\nPackage Manager: npm 10.x\nOS: linux x64\n\nAngular: 22.1.3\n... animations, cdk, common, compiler, compiler-cli, core, forms,\n platform-browser, router\n\nPackage Version\n---------------------------------------------------------\n@angular/build 22.1.5\n@angular/cli 22.1.5\n@angular-devkit/build-angular 22.1.3\nng-packagr 22.1.1\ntypescript 6.0.3\nvitest (via @angular/build:unit-test)\n```\n\n# Anything else?\n\nWorkaround that keeps published tree-shaking intact: instead of `\"sideEffects\": false`, use globs that match only the **workspace source** tree and nothing in the build output, e.g.\n\n```json\n\"sideEffects\": [\"./src/**\", \"./*/src/**\"]\n```\n\nng-packagr emits `fesm2022/*.mjs` plus per-entry `package.json` stubs, so these globs match zero emitted files. The published package therefore keeps effective side-effect-free semantics for consumers, while the workspace source — which is what the unit-test build reads through tsconfig `paths` — is treated as impure.\n\nRelated but distinct: [#33467](https://github.com/angular/angular-cli/issues/33467) (unit-test builder resolves `include` relative to `sourceRoot`, also a multi-entry-point blind spot) and [#33893](https://github.com/angular/angular-cli/issues/33893) (`sideEffects: false` interacting with optimizer pure annotations inside published FESMs — statement-level rather than whole-module).\n","author":{"url":"https://github.com/PatVandyke","@type":"Person","name":"PatVandyke"},"datePublished":"2026-08-24T15:33:49Z","url":"https://github.com/angular/angular-cli/issues/33910"}
| 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:b36aaa94-5d29-5fab-69eb-993305525425 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 953E:5D487:115555E:16FE84C:6B01E84D |
| html-safe-nonce | 1e669d8d2b46b8e6d6774bba296dd2e5e9b8e5acb2b160f5fac5369399c391f1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NTNFOjVENDg3OjExNTU1NUU6MTZGRTg0Qzo2QjAxRTg0RCIsInZpc2l0b3JfaWQiOiI3Mjc5MjA2MTIwODk4NTUzOTMzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | b708c6ecd7508798b71596ddcb65a7952a796196bf757e67f345d954b7a339d6 |
| hovercard-subject-tag | issue:5236588936 |
| 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-cli/33910/issue_layout |
| twitter:image | https://opengraph.githubassets.com/14969eba1f7d56d6f85a7d61607de1911f11ce87bc94ca7929889143cf06494f/angular/angular-cli/issues/33910 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/14969eba1f7d56d6f85a7d61607de1911f11ce87bc94ca7929889143cf06494f/angular/angular-cli/issues/33910 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? build Is this a regression? Yes, this behaviour used to work in version 21.2.19 Description In a multi-entry-point library built with ng-packa... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | PatVandyke |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular-cli git https://github.com/angular/angular-cli.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 36891867 |
| octolytics-dimension-repository_nwo | angular/angular-cli |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 36891867 |
| octolytics-dimension-repository_network_root_nwo | angular/angular-cli |
| 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 | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width