webpro-nl/knip

Provide a mechanism to skip reporting on specific unused files

Closed this issue · 4 comments

Currently, unexported symbols can bypass reporting via use of the JSDoc tags as described here.

However, I don't believe there is currently a way to bypass reporting of unused files.

This feature request is to provide the ability to do so. The use case is a scenario where a new file has been added in preparation for it to be used in a subsequent commit. While that subsequent commit is pending, the file is currently unused and shows on knip's report.

Alternatives considered

Update the ignore configuration
This would work, but it wouldn't be something that I think most developers in a large repository should be modifying. It would be a risky thing to encourage, and would also make the ignores even harder to find than with a tag or directive.

Exclude unused files from report
This would also work, but would eliminate one of the benefits of knip in finding unused files. In this case, the file is only unused temporarily but hiding all unused files from the report would cause missed items.

Have you considered adding them to entry? This is what we do for files that are not used yet.

Have you considered adding them to entry? This is what we do for files that are not used yet.

I didn't consider entry, but I don't think it works for us for a few reasons:

  1. Semantically, it's likely not actually an entry point. Ignore would probably be preferable as a fallback.
  2. When using --include-entry-exports it would trigger an error anyway.
  3. It suffers from the same concern I have with ignore in that it would require developers to be making changes to the knip configuration and then also having to remember to undo those changes. I feel like tooling configurations shouldn't necessarily be modified through the course of normal development pull request workflows.

For now, I've just set unused files to a "warn" level, but it would definitely be preferable if there was a way for individual files to be opted out of, like exports can be opted out of through tags.

Adding my 2 cents:

You might want to split your Knip commands, Knip provides a way to automatically exclude non-prod code, which often leads to reporting unused-files as many of those files might be only used in unit testing

I do run two knip validations, one for the main code where I validate the export and another one for all the code (main code + test files), which validate the unused files

If a file is unused in a project, it means that it is not imported by main code or unit testing; if any developer aims to add an "unused" file, I would be wondering how they test it. There must be at least one import of the file, the unit testing 😃

Knip does not even read unused files, so it's currently unable to ignore it based on its contents, so that's why I'm going to close this issue for now.

A custom preprocessor could be introduced that iterates over the unused files and filters out those matching your criteria.

What I'd like to improve though, is for the ignore* config options to better indicate what items are no longer doing anything and can be removed from the config.