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

🐛 knip does not follow tsconfig paths #977

Closed
6 tasks done
jameshfisher opened this issue Mar 9, 2025 · 7 comments
Closed
6 tasks done

🐛 knip does not follow tsconfig paths #977

jameshfisher opened this issue Mar 9, 2025 · 7 comments
Labels
bug Something isn't working

Comments

@jameshfisher
Copy link

Prerequisites

Reproduction url

https://github.com/jameshfisher/knip-bug

Reproduction access

  • I've made sure the reproduction is publicly accessible

Description of the issue

Steps to reproduce:

  1. git clone https://github.com/jameshfisher/knip-bug, a minimal repro
  2. pnpm i
  3. pnpm run knip

Expected behavior: no unused files

Actual behavior:

pnpm run knip

> q@1.2.3 knip /Users/jim/dev/granola-inc/knip-bug
> knip

Unused files (1)
common/bar.ts

The file common/bar.ts is claimed to be unused. However it's used by src/main.ts, which is an entry point.

The import ... from "@qux/common/bar" should resolve to common/bar.ts because of the "@qux/common/*": ["common/*"] in tsconfig.json. This can be seen with pnpm run start, which resolves the import.

@jameshfisher jameshfisher added the bug Something isn't working label Mar 9, 2025
@jameshfisher
Copy link
Author

jameshfisher commented Mar 9, 2025

I also ran with --debug, but couldn't make much sense of it. What I wanted to see was: where does knip believe "@qux/common/bar" resolves to, which is different to common/bar.ts? And that different filepath presumably points to a non-existent file, so why doesn't knip give an error about that?

@jameshfisher
Copy link
Author

The debug tool I'm looking for is a dependency graph between files, e.g. dotfile output. I can see many knip outputs, but none that show me resolved file dependencies

@jameshfisher
Copy link
Author

I've also tried knip-bun, but it gives the same unexpected result

@webpro
Copy link
Member

webpro commented Mar 9, 2025

The file is resolved fine. However, the issue is that while using tsonfig.json#compilerOptions.paths is obviously valid from a TS perspective, it's something that Knip doesn't handle well. That's because it's handling workspaces separately, and when wiring things back together this done by the dependencies cq package names (not path aliases).

The recommendation is to use dependencies from package.json#dependencies over tsonfig.json#compilerOptions.paths to target internal workspaces.

Briefly explained here: https://knip.dev/reference/faq#why-cant-i-use-path-aliases-to-reference-other-workspaces. Definitely could be documented better, and obviously, ideally shouldn't be an issue at all.

Basically dup of #961.

@jameshfisher
Copy link
Author

@webpro aha, thanks! We'll give that a try. I think we have other reasons that we're using paths instead of dependencies, but maybe we can fix these reasons

webpro added a commit that referenced this issue Mar 10, 2025
@webpro webpro closed this as completed in c5f7f2c 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
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants