angular / angular/angular-cli

`unit-test` builder resolves include paths relative to sourceRoot instead of project root, breaking secondary entry points tests

Đang mở
#33,467 0 bình luận 0 reaction 0 người được giao Xem trên GitHub
angular/build:unit-test area: @angular/build
Ngôn ngữ chính
TypeScript
Star
27k
Fork
11.8k
Merge trung bình
14 giờ 23 phút
Pull request đã merge (30 ngày)
162

Mô tả

### Command

test

### Is this a regression?

- [ ] Yes, this behavior used to work in the previous version

### The previous version in which this bug was not present was

_No response_

### Description

When using the `@angular/build:unit-test` builder in a library project that contains Secondary Entry Points (sub-packages), the builder incorrectly resolves the glob patterns defined in the `include` array (inside `angular.json`) relative to the project's `sourceRoot` (usually `.../src`) instead of the project's root.

Because the unit-test builder anchors include patterns to `sourceRoot`, it cannot discover test files located in secondary entry points unless developers use relative path traversal (for example, `../my-sub-package/**/*.spec.ts`).

### Minimal Reproduction

Create an Angular library project.
2. Generate a secondary entry point inside the library root (for example, `projects/my-lib/my-sub-package/`).
3. Add a unit test inside the secondary entry point:

```text
projects/my-lib/my-sub-package/my-sub.spec.ts
```

4. Configure the test target in `angular.json` using `@angular/build:unit-test`:

```json
{
"options": {
"include": [
"**/*.spec.ts",
"my-sub-package/**/*.spec.ts"
],
"tsConfig": "projects/my-lib/tsconfig.spec.json"
}
}
```

5. Run:

```bash
ng test
```

### Exception or Error

The builder should resolve the `include` patterns relative to the project's root, or otherwise recognize ng-packagr secondary entry points, allowing patterns such as:

```json
{
"include": [
"**/*.spec.ts",
"my-sub-package/**/*.spec.ts"
]
}
```

to correctly match test files located in:

```text
projects/my-lib/my-sub-package/
```

### Workaround

Currently, developers must escape the `sourceRoot` directory by using a parent path segment in `angular.json`:

```json
{
"options": {
"include": [
"**/*.spec.ts",
"../my-sub-package/**/*.spec.ts"
],
"tsConfig": "projects/my-lib/tsconfig.spec.json"
}
}
```

### Your Environment

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

Angular CLI : 22.0.4
Angular : 22.0.2
Node.js : 24.16.0
Package Manager : npm 11.13.0
Operating System : linux x64

┌───────────────────────────────────┬───────────────────┬───────────────────┐
│ Package │ Installed Version │ Requested Version │
├───────────────────────────────────┼───────────────────┼───────────────────┤
│ @angular/animations │ 22.0.2 │ ^22.0.2 │
│ @angular/build │ 22.0.4 │ ^22.0.4 │
│ @angular/cli │ 22.0.4 │ ^22.0.4 │
│ @angular/common │ 22.0.2 │ ^22.0.2 │
│ @angular/compiler │ 22.0.2 │ ^22.0.2 │
│ @angular/compiler-cli │ 22.0.2 │ ^22.0.2 │
│ @angular/core │ 22.0.2 │ ^22.0.2 │
│ @angular/forms │ 22.0.2 │ ^22.0.2 │
│ @angular/localize │ 22.0.2 │ ^22.0.2 │
│ @angular/platform-browser │ 22.0.2 │ ^22.0.2 │
│ @angular/platform-browser-dynamic │ 22.0.2 │ ^22.0.2 │
│ @angular/router │ 22.0.2 │ ^22.0.2 │
│ @angular/service-worker │ 22.0.2 │ ^22.0.2 │
│ ng-packagr │ 22.0.0 │ ^22.0.0 │
│ rxjs │ 7.8.2 │ ~7.8.2 │
│ typescript │ 6.0.3 │ ^6.0.3 │
│ vitest │ 4.1.9 │ ^4.1.9 │
│ zone.js │ 0.16.2 │ ^0.16.2 │
└───────────────────────────────────┴───────────────────┴───────────────────┘
```

### Anything else relevant?

_No response_

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Hướng nghiên cứu

Bắt đầu bằng cách chạy bản tái hiện tối thiểu với builder @angular/build:unit-test và kiểm tra cách các mẫu include của nó trong angular.json được phân giải. Xác minh hành vi với một thư viện chứa đường dẫn my-sub-package/**/*.spec.ts được nêu. Được xem là hoàn tất khi các mẫu được phân giải từ thư mục gốc của project và ng test phát hiện các test trong các điểm vào thứ cấp mà không duyệt đường dẫn bằng ../.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
angular, typescript
Lĩnh vực
build-system, testing
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
58/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.