Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💡 an issue with detecting unnecessary files in the monorepo project. #961

Closed
vincent890120 opened this issue Feb 26, 2025 · 6 comments
Closed
Labels
question Further information is requested

Comments

@vincent890120
Copy link

Suggest an idea for Knip

I ran into a problem while using knip detection in a monorepo project.
Let me give you an example:
The project comprises two sub-packages, app and infrastructure, where app relies on infrastructure's baseMethod.
When I import the baseMethod() from '@infrastructure/lib/base' in app and run 'knip --production', the file 'packages/infrastructure/lib/base.ts' is flagged as unnecessary.
But when I import the baseMethod from 'infrastructure/lib/base', knip correctly identifies 'packages/infrastructure/lib/base.ts' as a necessary file. I'm wondering, why does the presence of the '@' symbol cause this misidentification?
Here's the layout of the project structure.
Image

Image Can you assist me? Could there be an issue with how I'm using knip?
@vincent890120 vincent890120 added the feature request Feature request label Feb 26, 2025
@vincent890120
Copy link
Author

This is what I get when I run knip.
Image

@webpro webpro added question Further information is requested and removed feature request Feature request labels Feb 26, 2025
@webpro
Copy link
Member

webpro commented Feb 26, 2025

The answer is here: https://knip.dev/reference/faq#why-cant-i-use-path-aliases-to-reference-other-workspaces

@vincent890120
Copy link
Author

The answer is here: https://knip.dev/reference/faq#why-cant-i-use-path-aliases-to-reference-other-workspaces

I set up the paths attribute in knip.json, but it still fails to properly recognize the references from monorepo.
Image
I attempted to debug some of the kinp's source code and discovered that when there are multiple workspaces, each workspace corresponds to a principal. When the principal.addProjectPath(projectPath) is executed, it only adds the projectPath of the current workspace. However, when it comes to determining the usefulness of a file, it compares it with sourceFiles and projectPaths. I've logged some of the results.

Image As you can see, the sourceFile analysis indicates that "/Users/vincent/Project/web/Demo/anpoi-monorepo/packages/infrastructure/lib/base.ts" is a useful file. However, since this file is not included in the projectFiles, it's being identified as a useless file. Is there any solution to this issue?

@webpro
Copy link
Member

webpro commented Feb 27, 2025

The recommendation is to use dependencies from package.json#dependencies, not knip.json#paths

Using tsonfig.json#compilerOptions.paths is valid, it's just something that Knip doesn't handle well.

By the way, there's --isolate-workspaces to use one principal per workspace (but it's not a solution here).

@webpro webpro closed this as completed in 303a141 Mar 10, 2025
webpro added a commit that referenced this issue Mar 10, 2025
@webpro
Copy link
Member

webpro commented Mar 10, 2025

Looked at the implementation again and came to the same conclusion, which I've now tried to document a bit better: https://knip.dev/guides/handling-issues#typescript-path-aliases-in-monoreposhttps://knip.dev/reference/faq#why-cant-i-use-path-aliases-to-reference-other-workspaces

@webpro
Copy link
Member

webpro commented Mar 13, 2025

🚀 This issue has been resolved in v5.46.0. See Release 5.46.0 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants