Respect up-the-tree `.gitignore` files
I have a .gitignore in my ~ with the following contents:
.scratch/
This allows me to plop .scratch directories all over the place and put not-for-remote stuff there like personal notes, half-baked code snippets, etc. Knip, unfortunately, doesn't look at ~/.gitignore and reports code files in .scratch directories as unused.
I don't want to add .scratch to the repo .gitignore because it has nothing to do with the project. I don't want to store these files outside of the repo because I want to keep them close to the packages they're related to.
Does something like npm do the same eg when publishing?
Doesn't look like it, unfortunately:
https://stackoverflow.com/q/12534699/242684 https://github.com/semantic-release-action/typescript/issues/10#issuecomment-1536964025
Though I wouldn't treat npm's choices as gospel :sweat_smile:
To frame this a little bit differently, how else could I tell Knip to ignore **/.scratch/** without polluting the configuration?
I'm not against the feature per se, but I also don't want unexpected results. What might be a feature to you, could be a hard-to-track and tricky-to-overcome issue to someone else.
So if other tools use it as well it's common/expected behavior.
To frame this a little bit differently, how else could I tell Knip to ignore
**/.scratch/**without polluting the configuration?
You can use ignore at the root config with this pattern. If you don't fancy that you could perhaps use a knip.ts and make it more DRY.
I'm struggling to find examples. Prettier doesn't respect .gitignore, nor does ESLint. Biome, however, does (at least intends to). Outside of JavaScript, RipGrep does too, and fd. Can you maybe suggest some more projects to take a look at?
I'm not against the feature per se, but I also don't want unexpected results. What might be a feature to you, could be a hard-to-track and tricky-to-overcome issue to someone else.
On the one hand, I agree, changes like this require justification. On the other hand, Git is the common denominator here. If a file ultimately isn't committed, does it make sense to ever take it into consideration?
It should probably just follow whatever git check-ignore does?
Since I'm convinced this should be in Knip I've worked on this feature today. Would appreciate your feedback and verify if it works as expected. Performance should not be affected noticeably (you can use --performance before/after to verify).
You can install this preview using e.g.:
npm i -D https://pkg.pr.new/knip@359e624
It should respect .gitgnore files up-the-tree, also outside the current .git repo. You can use the --debug flag and look for "Parsed gitignore files" to see the files parsed and the resulting glob patterns to ignore.
cc @alecmev @TheKnarf @phiresky @drodil
:rocket: This issue has been resolved in v5.30.0. See Release 5.30.0 for release notes.
Using Knip in a commercial project? Please consider becoming a sponsor.
Works great, thank you! 👍