Skip to content

Unrelated components compilation in the test environment due to imports (NG8001) #66886

@BEGEMOT9I

Description

@BEGEMOT9I

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

compiler-cli, Don't known / other

Is this a regression?

Yes

Description

// alarm.component.ts
export const ALARM_TYPES = ['success', 'error']

@Component({
    selector: 'alarm',
    template: `
        <span>Alarm!</span>
        <test-button>Close</test-button>
    `,
    standalone: false,
})
export class AlarmComponent {}

// alarm.module.ts
@NgModule({
    imports: [ButtonComponent], // <- standalone component
    declarations: [AlarmComponent],
    exports: [AlarmComponent]
})
export class AlarmModule {}

// alarm.types.ts
import type {ALARM_TYPES} from './alarm.component'

export type AlarmType = (typeof ALARM_TYPES)[number]

// app.spec.ts
import type { AlarmType } from "../alarm/alarm.types"; // <- comment out to pass the case
// import {AlarmModule} from '../alarm/alarm.module'; // or u can add a direct import of the module

When building a test environment, if non-standalone components without modules are included in the compilation, this results in a missing element error. In the example shown, this occurs when importing a type from a component file.
In real projects, including components in the import chain is quite common, as constants and types can be stored along with components.
This example works in the regular build (when all *.ts-files are included), in the JIT compilation and in v20.

Please provide a link to a minimal reproduction of the bug

https://github.com/BEGEMOT9I/v21-testing-modules

Please provide the exception or error you saw

✘ [ERROR] NG8001: 'test-button' is not a known element:
1. If 'test-button' is an Angular component, then verify that it is part of this module.
2. If 'test-button' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. [plugin angular-compiler]

    src/alarm/alarm.component.ts:9:8:
      9 │         <test-button>Close</test-button>

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

Angular CLI       : 21.1.2
Angular           : 21.1.2
Node.js           : 24.8.0
Package Manager   : npm 11.6.0
Operating System  : darwin arm64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 21.1.2            │ ^21.1.2           │
│ @angular/cli              │ 21.1.2            │ ^21.1.2           │
│ @angular/common           │ 21.1.2            │ ^21.1.0           │
│ @angular/compiler         │ 21.1.2            │ ^21.1.0           │
│ @angular/compiler-cli     │ 21.1.2            │ ^21.1.0           │
│ @angular/core             │ 21.1.2            │ ^21.1.0           │
│ @angular/forms            │ 21.1.2            │ ^21.1.0           │
│ @angular/platform-browser │ 21.1.2            │ ^21.1.0           │
│ @angular/router           │ 21.1.2            │ ^21.1.0           │
│ rxjs                      │ 7.8.2             │ ~7.8.0            │
│ typescript                │ 5.9.3             │ ~5.9.2            │
│ vitest                    │ 4.0.18            │ ^4.0.8            │
└───────────────────────────┴───────────────────┴───────────────────┘

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: compilerIssues related to `ngc`, Angular's template compilerarea: testingIssues related to Angular testing features, such as TestBed

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions