vite-config: dts extension rewrite mangles directory-barrel imports ('../utils' → '../utils.js' instead of '../utils/index.js')
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 72/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 冷清
- 技术栈
- typescript, vite
- 领域
- build-system
调研方向
从 packages/vite-config/src/index.ts 中的 ensureImportFileExtension 及其对 beforeWriteFile 的处理开始。使用目录 barrel 重现该问题,并检查 ESM 和 CJS 两个分支生成的 .d.ts 文件。完成的标准是:目录导入变为 ../utils/index.js 或 ../utils/index.cjs,而普通文件导入保持不变,并且报告的类型解析失败得到解决。
由索引模型根据 Issue 内容生成。
描述
Describe the bug
tanstackViteConfig's declaration post-processing (ensureImportFileExtension in packages/vite-config/src/index.ts) appends .js / .cjs to every extensionless relative specifier in emitted .d.ts files, without checking whether the specifier targets a directory barrel.
Given a source file that imports from a directory with an index.ts:
// src/adapters/image.ts
import type { GeminiClientConfig } from '../utils' // src/utils/index.ts
the emitted declaration becomes:
// dist/esm/adapters/image.d.ts
import { GeminiClientConfig } from '../utils.js';
but the build only emits dist/esm/utils/index.d.ts — there is no dist/esm/utils.d.ts — so the specifier should have been '../utils/index.js'. Any consumer compiling with skipLibCheck: false fails with:
error TS2307: Cannot find module '../utils.js' or its corresponding type declarations.
The emitted JS is unaffected (rollup/rolldown resolves the barrel itself, pointing at the concrete module, e.g. '../utils/client.js'), so this is invisible at runtime and only breaks the type layer.
Both the ESM (.js) and CJS (.cjs) branches of ensureImportFileExtension have the same problem. Verified present in 0.4.1 and still present in the 0.5.2 source.
Why it goes unnoticed
- The rewrite happens in
beforeWriteFile, after type-checking —afterDiagnosticvalidates the source compile, and the rewritten output text is never re-checked. - Consumers overwhelmingly use
skipLibCheck: true, which suppresses TS2307 inside.d.tsfiles; the affected types silently degrade instead of erroring. publintdoesn't catch it (it checks the export map, not deep declaration resolution).@arethetypeswrong/clidoes flag it, asInternalResolutionError.
Real-world occurrence
Published packages built with this config ship the broken specifiers today, e.g. @tanstack/[email protected]:
dist/esm/adapters/image.d.ts→import { GeminiClientConfig } from '../utils.js'(onlydist/esm/utils/index.d.tsexists)
and @tanstack/ai (dist/esm/activities/generate*/index.d.ts → '../middleware.js', where middleware is a directory barrel).
Steps to reproduce
- Scaffold a lib using
tanstackViteConfigwithsrcDir: './src', entry./src/index.ts. - Add
src/utils/index.tsexporting anything, and import it from a sibling asfrom '../utils'. vite build, then inspect the emitted.d.ts— the specifier is'../utils.js'.- Compile a file importing the package with
skipLibCheck: false→ TS2307.
Suggested fix
Make the rewrite resolution-aware instead of purely textual. beforeWriteFile receives the output filePath, and the dist tree mirrors entryRoot/srcDir, so for each relative specifier the plugin can check whether the target resolves to a directory (or equivalently, whether <target>/index.d.ts rather than <target>.d.ts is being emitted) and append /index.js in that case:
'../utils' → '../utils/index.js' // when utils/ is a directory barrel
'../client' → '../client.js' // unchanged for plain files
Happy to send a PR if the approach sounds right.
- 主要语言
- TypeScript
- 星标
- 397
- 派生
- 41
- 平均合并
- 1 小时 9 分钟
- 30 天内合并 PR
- 3
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
TanStack/config 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 45/100
-
Dependency Dashboard可能重新可做 @lachlancollins 于 824 天前认领,目前没有进行中的 PR。 未关闭dependencies
相似的 Issue
-
bug go
难度 2/5 1-3 小时 新手友好度 82/100
genkit-ai/genkit#6761 · 1 条评论 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
NousResearch/hermes-agent#136483 ·
维护者通常 1 天内回复
-
documentation
难度 2/5 1-3 小时 新手友好度 84/100
facioquo/stock-indicators-dotnet#2316 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 86/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
vercel-labs/skills#2460 ·
维护者通常 1 天内回复