knip icon indicating copy to clipboard operation
knip copied to clipboard

🐛 Unused `export * from "./b"` line not reported

Open guillaumebrunerie opened this issue 1 year ago • 1 comments

Prerequisites

Reproduction url

https://codesandbox.io/p/devbox/wp5lv3

Reproduction access

  • [X] I've made sure the reproduction is publicly accessible

Description of the issue

I found a situation in which an export * from "./b"; line is unused (removing it makes no difference), but Knip doesn’t report anything:

// a.ts
export const a = "a";

// b.ts
export const b = "b";

// aAndB.ts
export * from "./a";
export * from "./b"; // Unused line?

// index.ts
import { a } from "./aAndB";
import { b } from "./b";
console.log(a, b);

Note that both a and b are used, but b is imported directly rather than being imported from the barrel file. This is a sign of a potential issue, that maybe b was meant to be imported from aAndB instead.

It’s not a big issue, as there isn’t any actual code that is unused (except from the export * line which will most likely disappear after bundling anyway), and supporting this use case would maybe require a whole new category of things that can be potentially unused, but I felt like I should report it anyway if you feel like it should be fixed.

guillaumebrunerie avatar Nov 21 '24 09:11 guillaumebrunerie

Oh, interesting. It's simply not supported yet, but I guess it could be. Won't be trivial at first thought though, because that * in the barrel file is not an exporteded identifier (yet).

Edit: and the * is also often not unique either (like in the example).

webpro avatar Nov 21 '24 09:11 webpro

I have a similar issue:

// src/not-entrypoint/a.ts

export {X} from './b'
export {Y} from './c'

// src/not-entrypoint/b.ts

export const X = "X"

the file b.ts is actually unused and should be flagged by knip.

Is this the same issue or should I create a separate ticket?

fabb avatar Apr 16 '25 11:04 fabb

Could be interesting to whip up a reproduction with the latest version of Knip. Recently I've added something that reports unused re-exports, maybe this is (part of) the solution?

webpro avatar Apr 16 '25 11:04 webpro

Thank you! Indeed, I was running knip only with knip --files which did not show the unused exports. Here's the repro: https://stackblitz.com/edit/knip-case-repro-bkevakau?file=README.md

fabb avatar Apr 17 '25 10:04 fabb

What knip --files does: report only unused files (not other issues/unused things, etc).

webpro avatar Apr 17 '25 11:04 webpro

I upgraded now to run all knip checks and add some ignore rules instead 😊

fabb avatar Apr 17 '25 13:04 fabb

This is one of the reasons why we should not use barrel files:

  • https://tkdodo.eu/blog/please-stop-using-barrel-files
  • https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/
  • https://github.com/thepassle/eslint-plugin-barrel-files

But I agree it would be nice to have support for export from in general.

fregante avatar May 02 '25 08:05 fregante

Closing this issue as part of a general cleanup to keep this project sustainable and optimize my time working on it. If you think this is inappropriate or if there is no workaround and you feel stuck, feel free to open a new issue. Thanks for your understanding.

webpro avatar Aug 10 '25 07:08 webpro