Vitest coverage issue `vite:import-analysis` error `Pre-transform error: Failed to resolve import "#/util" from "src/app/app.ts". Does the file exist?`

未關閉
#32,891 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
48/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
冷清
技術堆疊
playwright, typescript
領域
testing-qa

研究方向

先使用列出的 Angular 專案、tsconfig.json 路徑別名、src/app/app.ts 中的 import 以及 ng test 命令重現失敗,並比較 Vitest 4.1.0 與 4.0.8。在共用 app chunk 參照該別名時,追蹤 coverage 產生與 source-map 處理。別名能夠解析,且瀏覽器 coverage 在沒有 vite:import-analysis 錯誤的情況下完成,即表示完成。

由索引模型根據 Issue 內容生成。

描述

angular/build:unit-test area: @angular/build gemini-triaged
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

vitest@4.0.8

Description

I'm not sure if this issue is an issue with Vitest, or the Angular CLI integration with Vitest. I put the issue here, because I have no idea how to reproduce this issue without Angular at the moment.

The issue is that when using tsconfig.json property paths and running browser tests with coverage, these paths will not used when Vitest tries to read coverage from source maps. The requirement seems to be that a shared chunk exists (import './app' in the repro steps).

This results in errors during coverage generation, but tests do not crash. The errors come from Vite's dev server pre transforming original .ts files referenced in source-maps.

Requirements

  • Using paths in tsconfig.json
  • Angular CLI + eslint creates a shared chunk that has the paths in it's sourcemap
  • Vitest with browsers + coverage + coverageInclude
  • Vitest v4.1.0+
Minimal Reproduction
  1. Init Angular project & install vitest coverage & browser packages
npx @angular/cli@21.1.5 new ng21.1.5 -s -t --style css --no-ssr --ai-config none
npm i -E -D vitest@4.1.0 @vitest/browser-playwright@4.1.0 @vitest/coverage-v8@4.1.0 playwright
  1. Add src/app/util.ts file
export const greet = 'Hello';
  1. Add a path alias to util file
{
  "compilerOptions": {
    "paths": {
      "#/util": ["./src/app/util.ts"]
    }
  }
}
  1. Make App (app.ts) use value from util.ts
import { greet } from '#/util';
// ...
export class App {
  protected readonly title = signal(greet);
}
  1. Add import to app.ts from app.config.ts
import './app';
  1. Run tests with browser, coverage & coverage include
npx ng test --no-watch --browsers chromium --coverage --coverage-include src/app/**/*.ts
Exception or Error
There's logged errors for every file that uses the path alias, which in our project is most of them (e.g. `import { shared } from '#/core';`).


00:02:12 [vite] (client) Pre-transform error: Failed to resolve import "#/util" from "src/app/app.ts". Does the file exist?
  Plugin: vite:import-analysis
  File: C:/Users/foo/repro/src/app/app.ts:3:22
  11 |  import { Component, signal } from "@angular/core";
  12 |  import { RouterOutlet } from "@angular/router";
  13 |  import { greet } from "#/util";
     |                         ^
  14 |  export let App = class {
  15 |    title = signal(greet);
Your Environment
I've tried with `@angular/build` + `@angular/cli` at several versions, including `21.1.x` and `21.2.x`. Downgrading `vitest` to `4.0.8` fixes the issue, but `vitest@4.1.0` and `4.1.2` both failed.

----------------------------------------

Angular CLI       : 21.2.5
Angular           : 21.2.6
Node.js           : 25.8.2 (Unsupported)
Package Manager   : npm 11.11.1
Operating System  : win32 x64

┌───────────────────────────┬───────────────────┬───────────────────┐
│ Package                   │ Installed Version │ Requested Version │
├───────────────────────────┼───────────────────┼───────────────────┤
│ @angular/build            │ 21.2.5            │ ^21.1.5           │
│ @angular/cli              │ 21.2.5            │ ^21.1.5           │
│ @angular/common           │ 21.2.6            │ ^21.1.0           │
│ @angular/compiler         │ 21.2.6            │ ^21.1.0           │
│ @angular/compiler-cli     │ 21.2.6            │ ^21.1.0           │
│ @angular/core             │ 21.2.6            │ ^21.1.0           │
│ @angular/forms            │ 21.2.6            │ ^21.1.0           │
│ @angular/platform-browser │ 21.2.6            │ ^21.1.0           │
│ @angular/router           │ 21.2.6            │ ^21.1.0           │
│ rxjs                      │ 7.8.2             │ ~7.8.0            │
│ typescript                │ 5.9.3             │ ~5.9.2            │
│ vitest                    │ 4.1.0             │ 4.1.0             │
└───────────────────────────┴───────────────────┴───────────────────┘
Anything else relevant?

Does not seem to be OS specific. The issue is on GitHub Actions on Ubuntu, and locally on Windows.

主要語言
TypeScript
星號
27k
分支
11.8k
平均合併
16 小時 35 分鐘
30 天內合併 PR
176

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

angular/angular-cli 的其他 Issue

查看 angular/angular-cli 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。