Comments (10)
A few remarks:
- The
project
file patterns are meant only to find unused files. The set ofproject
files minus theentry
files + resolved files result in unused files (https://knip.dev/guides/handling-issues#unused-files). In other words, you can't "remove files" from the resolved set of files based on the entry files by adding negated patterns to theproject
files. - You want to use
--production
to exclude entry files added by plugins. - Use
--debug
to see what files are added and why (e.g. by Knip core or by one of the plugins).
If this doesn't help enough you can create a minimal reproduction and I'm happy to look into it.
I don't understand why this question hasn't arisen in the community before. After all, this is a very common case when test files interfere with knip searching for unused files.
The issue you are seeing is particular to your project and your understanding of how Knip works. This is definitely something I need to improve in the documentation so your feedback is very useful!
from knip.
Also based on the feedback here I just added this page: https://knip.dev/guides/configuring-project-files
from knip.
Please see the following sections in the docs:
from knip.
Thank @webpro to your answer!
But I not found answer to my question. I think knip doesn't allow exclude tests files in analyze.
And this is very strange
Why does construction "!src/**/*.test.{ts,tsx}"
not exclude files from analysis, but simply make them ignored in the report?
{
"$schema": "https://unpkg.com/knip@5/schema.json",
"entry": ["src/index.{ts,tsx}"],
"project": [
"src/**/*.{ts,tsx}",
"!src/**/*.test.{ts,tsx}" // <--- use `!` the same thing as adding to section `ignore`
],
"ignore": [
"src/api/src/**",
"src/service-worker.ts"
]
}
Even if I use production mode, it won't help me either. After all, I can’t separate the test files from all the files that knip analyzes.
I don't understand why this question hasn't arisen in the community before. After all, this is a very common case when test files interfere with knip searching for unused files.
from knip.
Hi @webpro ! Thanks to your answer!
In other words, you can't "remove files" from the resolved set of files based on the entry files by adding negated patterns to the project files.
You are absolutely right! This is my problem.
Now in my project there are test files (*.test.tsx
) and storybook files (*.stories.tsx
), because of which knip cannot understand that the code is no longer used.
It would be great if we could separate the test and storybook files into a group - except
:
{
"entry": ["src/main.tsx"],
"project": ["src/**/*.{ts,tsx}"],
"except": ["src/**/*.test.tsx", "src/**/*.stories.tsx"] // <-- DON'T USE FOR CHECK "DEAD CODE"
}
If the file you are looking for is used only in test files (*.test.tsx
) or storybook files (*.stories.tsx
), then knip should consider it “dead code”.
I have prepared a thread for you in my project where you can experiment:
[https://github.com/pkolt/bitmap_editor/tree/knip-issue-591]
Component src/components/Cat/index.tsx
is only used in the test and is dead code, but knip cannot determine this.
Maybe you can do something about this? In fact, after your last answer, I think that only changes knip can solve my problem.
from knip.
Production mode is designed for this use case. What goes wrong if you use it?
from knip.
Hi @webpro !
Production mode is designed for this use case. What goes wrong if you use it?
Can you give an example of a setup for production mode? In the example below, I should exclude tests (*.test.tsx
) and storybooks (*.stories.tsx
).
Default setup:
{
"entry": ["src/main.tsx"],
"project": ["src/**/*.{ts,tsx}"]
}
from knip.
This seems like a good example: https://github.com/invoke-ai/InvokeAI/tree/main/invokeai/frontend/web
Install dependencies with pnpm
and run knip with pnpm run lint:knip --debug
and --debug --production
and see that in production mode, the stories aren't part of the analysis.
from knip.
Hi @webpro !
My ending knip config:
{
"entry": ["src/main.tsx!"]
}
I use command knip --production
.
It wasn't obvious, but now it works!
Thank you for help! 👍
from knip.
Glad you found it!
Just adding this for other reading this: src/main.tsx!
is part of the default settings. But when it needs to be extended and overridden, the exclamation mark needs to be there as well, and this may not be obvious indeed.
from knip.
Related Issues (20)
- commitlint plugin update breaks when `parserPreset` isn't a `string`
- SvelteKit Demo App $app/* unresolved HOT 2
- 5.14.0 breaks knip in git submodules
- Documentation Site Version Selector leads to 404 HOT 2
- Support local custom plugin HOT 1
- Support import.meta.resolve (in Pino v7+ transports) HOT 2
- Unused functions are not being detected HOT 5
- Default import name being used to determine if export is used HOT 7
- Re-exported enum members incorrectly reported as unused HOT 7
- False positive when using a dynamic default importing HOT 2
- Module '"knip"' has no exported member 'KnipConfig'. Did you mean to use 'import KnipConfig from "knip"' instead? HOT 2
- "ignore" file working even though it shouldn't based on the docs HOT 4
- [Svelte@5] Knip fails to resolve `*.svelte.(j|t)s` files HOT 4
- Run the script only against the staged files. HOT 2
- `ignore` is not applied for Auto-fix HOT 1
- Not working with @emotion/styled during string interpolation HOT 1
- 'TypeError: importItems.importedAs.entries is not a function' error when using --cache, but not without the cache HOT 2
- Dependency reported as "unused" even if it's used with require.resolve(...) HOT 5
- Fails to run due to TypeError in `graphql-codegen` HOT 3
- `No "exports" main defined` error with `estree-walker` when using `mdx-js` plugin HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from knip.