Skip to content

🐛 Knip doesn't resolve package.json exports when rootDir:"." #1738

Description

@maxpatiiuk

Prerequisites

Reproduction url

https://stackblitz.com/edit/github-z7snogip?file=README.md

Reproduction access

  • I've made sure the reproduction is publicly accessible

Description of the issue

If tsconfig.json rootDir is set to ".", and outDir to "dist", Knip fails to reverse-map package.json exports.

Reproduction steps:

  1. npm install
  2. npm run knip - see false unused files reports
  3. If I move those same files into src/ and update the tsconfig.json rootDir to be src/, the false error goes away

The issue might come from here from the !isUnderSrcDir(absSpecifier, srcDir) && check:

const rewritePattern = (sourceMaps: SourceMap[], absSpecifier: string, extensions: string) => {
for (const { srcDir, outDir } of sourceMaps) {
if (!isUnderSrcDir(absSpecifier, srcDir) && isUnderOutDir(absSpecifier, outDir)) {
return srcDir + absSpecifier.slice(outDir.length).replace(matchExt, extensions);
}
}
};

Use case:

  • I wish root-level .ts files, like vite.config.ts and support scripts to be type checked as part of build, so rootDir is set to . rather than src/. This also helps typescript-eslint as it can use the same typescript program instance for the entire package.

A workaround:

  • Do not specify a "rootDir" in tsconfig.json - typescript still automatically infers it as ".", where as knip infers it as "src". Fragile 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions