Code Monkey home page Code Monkey logo

Comments (10)

webpro avatar webpro commented on June 17, 2024 1

A few remarks:

  • The project file patterns are meant only to find unused files. The set of project files minus the entry 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 the project 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.

webpro avatar webpro commented on June 17, 2024 1

Also based on the feedback here I just added this page: https://knip.dev/guides/configuring-project-files

from knip.

webpro avatar webpro commented on June 17, 2024

Please see the following sections in the docs:

from knip.

pkolt avatar pkolt commented on June 17, 2024

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.

pkolt avatar pkolt commented on June 17, 2024

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.

webpro avatar webpro commented on June 17, 2024

Production mode is designed for this use case. What goes wrong if you use it?

from knip.

pkolt avatar pkolt commented on June 17, 2024

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.

webpro avatar webpro commented on June 17, 2024

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.

pkolt avatar pkolt commented on June 17, 2024

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.

webpro avatar webpro commented on June 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.