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

Unused dependency checker and fixer #23989

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

pragya91
Copy link
Contributor

@pragya91 pragya91 commented Mar 5, 2025

Purpose: Report and fix unused dependencies.

  • This PR adds a new check scripts check:knip to check and knip:fix to fix, the unused dependencies found in the package.json of any the packages in the repo . The underlying tool used is knip npm package.
  • For now, we only introduce this script to one of the packages - build-cli. The idea is to incrementally make the same change to all the packages in our repo, to ensure quicker PR turnaround time and to open up possibility of multiplying the effort with more devs.

Future of this work

  • Add knip and fix scripts to all the packages progressively.
  • Possibly add a policy check handler to check if the script has been added to all the packages, just like we have for 'prettier'.
  • Once we have completed a basic for knip - dependency checker tool, and it is found to be a stable one to use with our repo, we can expand the implementation to make use of more features knip provides - like checking for unused files, unused exports etc.

Context
(This work is continuation of the previous hack project #22448 and also the work done by Tyler in the PR here: #23992 where we explore the various build/reporting options that work the best with our requirements.)

@github-actions github-actions bot added area: build Build related issues base: main PRs targeted against main branch labels Mar 5, 2025
@pragya91 pragya91 changed the title check deps Used dependency checker and fixer Mar 5, 2025
@pragya91 pragya91 changed the title Used dependency checker and fixer Unused dependency checker and fixer Mar 5, 2025
@pragya91 pragya91 requested review from a team, markfields, jatgarg, kian-thompson, WillieHabi, rajatch-ff and MarioJGMsoft and removed request for a team March 5, 2025 18:45
@pragya91 pragya91 marked this pull request as ready for review March 5, 2025 19:54
@Copilot Copilot bot review requested due to automatic review settings March 5, 2025 19:54
Copilot

This comment was marked as spam.

Copy link
Contributor

@jason-ha jason-ha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you generate a PR from cleanup in client? Will be very helpful to see what we are getting with this policy.

Copy link
Member

@tylerbutler tylerbutler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together! In #22448 (comment) I outlined three options for this type of check. This PR is option 2, and I got curious about option 1, which is doing a per-package integration at the task level - adding new depcheck tasks to individual packages and then running them as part of the build.

I looked into depcheck's config story, and there is no inheritance or looking up the tree for configs. It's pretty clearly a "one package at a time" tool. However, we can create our own base config and import it and modify it on a per-package basis since the config supports CJS. So the next question is, "can we share a config that the majority of packages would use?"

The answer to that is a little unclear, but based on my testing, it doesn't look good unfortunately. The problem is that there are a lot of false results. Depcheck flags some deps that are used at runtime or in CI, etc. I suspect each package in the repo will need some level of config. That means a config file in each package, even if it mostly inherits from the root config. I suspect many packages will have additional exclusions that apply only to it as well.

These false positives also make it unlikely that an autofixer will be safe. Each removal needs to be scrutinized.

All of this together points towards option 1 possibly being a better approach for this integration. I explored those changes in #23992. Note that many of those changes would be needed with this approach as well, since each package will need its own config.

@pragya91 pragya91 closed this Mar 5, 2025
@pragya91 pragya91 reopened this Mar 5, 2025
@github-actions github-actions bot added the dependencies Pull requests that update a dependency file label Mar 7, 2025
@pragya91
Copy link
Contributor Author

pragya91 commented Mar 7, 2025

@tylerbutler / @jason-ha - Thanks for reviewing the first pass of the implementation.
The PR has a new look now, where we are changing our direction from adding a new policy check handler to having check scripts instead. The underlying tool is also changed from depcheck to knip since it is more actively maintained and is more feature rich. ( Thanks @sonalivdeshpande as the original pitcher for this idea and @tylerbutler for reviewing).

Requesting a re-review. Please let me know if we can make further improvements to our approach. Thanks!

@@ -43,6 +43,8 @@
"build:test:esm": "tsc --project ./src/test/tsconfig.json",
"check:biome": "biome check .",
"check:format": "npm run check:biome",
"check:knip": "knip --no-config-hints",
"knip:fix": "knip --fix --fix-type dependencies --no-config-hints",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--no-config-hints -- supresses the hints provided by knip tool to remove the unused ignoreDependencies coming from knip.base.config.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's mildly annoying the fix-types aren't part of config. DOesn't seem to be supported.

// Import the shared config from the root of the repo.
import sharedConfig from "../../../knip.base.ts";

export default {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a doc comment with a type directive here so we can get type checking on the config object. Something like this:

/**
 * @type {import("knip").Config}
 */

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right it's typescript - just import type then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you can remove the ts-check-related comments.

import sharedConfig from "../../../knip.base.ts";

export default {
include: [...sharedConfig.include],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
include: [...sharedConfig.include],
...sharedConfig,

Comment on lines -82 to -84
"@andrewbranch/untar.js": "^1.0.3",
"@fluid-tools/build-infrastructure": "workspace:~",
"@fluid-tools/version-tools": "workspace:~",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is definitely a false positive. Does it understand the workspace:~ deps?

Comment on lines -86 to -87
"@fluidframework/bundle-size-tools": "workspace:~",
"@inquirer/prompts": "^7.0.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely a false positive.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think most of the removed deps are actually used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: build Build related issues base: main PRs targeted against main branch dependencies Pull requests that update a dependency file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants