webpro-nl/knip

@public modifier does not work if the whole file is unused

erheron opened this issue ยท 9 comments

Reproduction url

https://github.com/erheron/knip-v5-public-in-unused-files-repro

Description of the issue

Knip @public modifier, if placed above an exported function, promises to make Knip not count this export as unused (even if it is)
However, if the exported unused function is placed in a file, where all exports are unused, @public modifier seems to have no effect

This is somewhat contradictory and perhaps presents an inconsistency in knip modifiers

For reproduction, see this repo

We have three files there:

  • src/unused-files.js is a file where all exports are unused, and one of them is marked with /** @public */ JSDoc modifier
  • src/partially-unused-file exports a function which is later used in src/main.js

Moreover, in knip config knip.jsonc we explicitly include "files" in knip report

If you clone that repo and run yarn knip you get this report:

Unused files (1)
src/unused-file.js
Unused exports (1)
unused_getID  function  src/partially-used-file.js:5:17

So knip reports src/unused-file.js as an unused file (which it actually is), but also it means that @public modifier makes no effect there.

This also looks related to #706 -- if there was a special modifier to ignore an unused file, I guess that would be helpful for us as well.

However, I want to emphasize that this issue presents something else -- an inconsistency (or just something that is not explained well) about how do we report unused exports and unused files when @public modifier is applied. I suspect something similar might occur for other modifiers e.g. @internal and @beta

If you have a knip.json config file, you could add the unused files to the ignore list

ignore: ["src/commons/Routing/RNavStack/debugScreens/debugTab/index-noop.tsx",]

Thank you @eppisapiafsl . I should have mentioned that I'm aware of that ๐Ÿ™Œ๐Ÿป

However, that doesn't seem to be as good an option as having a dedicated "@file-ignore" comment OR "@public" working according to the documentation (which is not the case right now), mostly for the reasons actually mentioned in this comment in #706

@erheron Amms... I think you might want to review the code

The unused export method that is being reported doesn't have a @public decorator .... https://github.com/erheron/knip-v5-public-in-unused-files-repro/blob/main/src/partially-used-file.js#L5

Screenshot 2024-07-29 at 15 16 31

Once you add it, Knip reports only the unused file
Screenshot 2024-07-29 at 15 18 03

The code is perfectly all right, although I may have complicated the example a little bit ๐Ÿ˜†
The problem is not with partially-used-file at all. The problem is, that in the unused-file, one of the exports has a @public modifier, so it shouldn't count, and still the unused-file is reported as unused

Oh sorry, my bad. I thought you meant Knip was reporting unused export with @public decorator that were inside an unused file ๐Ÿ˜…

The @public and the tags decorator only work for exports; the file is still reported because it's not reachable from the entry point.

So you are making the same feature request as #706 (comment) ๐Ÿ‘

Knip works by creating a dependency tree based on entry files. Unused files are not part of the analysis. If the intention for the "unused file" is to be an entry file, then it should be marked as such.

Note that Knip also adds items in package.json#exports as entry files.

So you are making the same feature request as #706 (comment) ๐Ÿ‘

Yeah... it looks like it. As I mentioned, having a mechanism like that could help ๐Ÿ‘๐Ÿป

Unused files are not part of the analysis. If the intention for the "unused file" is to be an entry file, then it should be marked as such.

Do you think we can have an unused-file modifier so we can avoid adding those to entry files? We can also move this discussion to #706

In general currently not looking to add new features btw, still happy to have the conversation.

Let's close this one in favor of #706 indeed.