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

Flag to analyse all workspaces that depend on the one passed into -W, for better entrypoint analysis #249

Closed
me4502 opened this issue Sep 19, 2023 · 4 comments
Labels
feature request Feature request

Comments

@me4502
Copy link
Contributor

me4502 commented Sep 19, 2023

Not sure the best way to do this one, but basically the idea is to be able to detect what entrypoints of an internal workspace aren't used without running Knip across the entire monorepo.

Thought is the single workspace would be provided via flag, and only that workspace would have items reported, but any other workspace that depends on the passed workspace would also be included in analysis. A possible flag name could be --traverse-upwards, --resolve-dependents, --resolve-upwards, etc. Only directly listed dependencies (across all dev deps, prod deps, peer deps, etc) should be included, not transitive dependencies.

In the following setup,

  • A: depends on B and C
  • B
  • C

If I ran knip -W C --include-entry-exports --traverse-upwards, I would expect Knip to provide me a report of what is unused within workspace C, however I would expect its analysis to also take into account usages within A but not B. The current behaviour is that anything not internally used is reported as unused, even if it's used by other workspaces within the monorepo.

This would make it significantly more feasible to use Knip to find unused entry exports of a shared internal library, without running it across an entire monorepo.

The big caveat here is that it does depend on dependencies being setup correctly, as it would miss workspaces that forget to include the dependency. I feel this isn't too big a deal, as Knip already provides functionality to resolve these cases anyway, and it's significantly better than the current situation of no other workspaces being included.

@me4502 me4502 added the feature request Feature request label Sep 19, 2023
@webpro
Copy link
Collaborator

webpro commented Sep 19, 2023

Knip doesn't build that kind of dependency tree yet. Sounds like a good idea. Maybe we can steal some of the --filter implemention that Turborepo stole from pnpm.

@webpro
Copy link
Collaborator

webpro commented Sep 28, 2023

Released a fix under the @graph tag:

npm install knip@graph

I consider it basically a bugfix to the -W argument as you describe (no extra flags necessary).

What Knip did before, was analyzing the provided --workspace [dir] + all ancestor workspaces (could in theory be more than just the root workspace when using nested workspaces, which is valid).

With this fix, it includes the provided --workspace [dir], its ancestors and all their combined dependencies. In practice it should do what you expect.

Needs some more testing on my end too, would be great if you could try it out :)

@webpro
Copy link
Collaborator

webpro commented Oct 5, 2023

Just published another graph dist-tag (0.0.0-graph.2). I'm want to get it right, because this feature is essential for working with large monorepos, which are otherwise hard/slow to work with. So thanks again for bearing with me :)

After going back and forth in what I think we need, and how it could be implemented, this should version come a lot closer to what you requested initially.

Its definitely still WIP, but it now only considers direct dependents and filters out results from other workspaces. Also added a little convenience to directly see what workspaces are included when using --debug.

One issue that must still be fixed is that it filters out issues coming from the root workspace when using --workspace [dir].

To be continued. Feel free to share any feedback or insights you might have :)

@webpro
Copy link
Collaborator

webpro commented Oct 8, 2023

After a few more tests and tweaks this landed in v2.32.3. With that, I'm going to close this ticket, but feel free to discuss further/report bugs.

@webpro webpro closed this as completed Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Feature request
Projects
None yet
Development

No branches or pull requests

2 participants