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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions build-tools/packages/build-cli/knip.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/

// Enable TypeScript type-checking for this file.
// See https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html#ts-check
// @ts-check

// 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.

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,

ignoreDependencies: [
...sharedConfig.ignoreDependencies,

// Oclif plugins are used dynamically at runtime.
"@oclif/plugin-*",
// Danger is used at runtime in the dangerfile.
"danger",
// Types from this package are used in markdown.ts.
"mdast",
// This is needed by the fluid-build task integration in policy-check.
"tslib",
],
};
48 changes: 4 additions & 44 deletions build-tools/packages/build-cli/package.json
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.

Original file line number Diff line number Diff line change
Expand Up @@ -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.

"ci:build:docs": "api-extractor run",
"clean": "rimraf --glob dist lib oclif.manifest.json \"**/*.tsbuildinfo\" \"**/*.build.log\" _api-extractor-temp nyc",
"clean:manifest": "rimraf --glob oclif.manifest.json",
Expand Down Expand Up @@ -79,105 +81,63 @@
"temp-directory": "nyc/.nyc_output"
},
"dependencies": {
"@andrewbranch/untar.js": "^1.0.3",
"@fluid-tools/build-infrastructure": "workspace:~",
"@fluid-tools/version-tools": "workspace:~",
Comment on lines -82 to -84
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?

"@fluidframework/build-tools": "workspace:~",
"@fluidframework/bundle-size-tools": "workspace:~",
"@inquirer/prompts": "^7.0.1",
Comment on lines -86 to -87
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.

"@microsoft/api-extractor": "^7.47.11",
"@oclif/core": "^4.0.30",
"@oclif/plugin-autocomplete": "^3.2.7",
"@oclif/plugin-commands": "^4.1.5",
"@oclif/plugin-help": "^6.2.16",
"@oclif/plugin-not-found": "^3.2.24",
"@octokit/core": "^5.2.0",
"@octokit/rest": "^21.0.2",
"@rushstack/node-core-library": "^5.9.0",
"async": "^3.2.6",
"azure-devops-node-api": "^11.2.0",
"change-case": "^3.1.0",
"cosmiconfig": "^8.3.6",
"danger": "^12.3.3",
"date-fns": "^2.30.0",
"debug": "^4.3.7",
"execa": "^5.1.1",
"fflate": "^0.8.2",
"fs-extra": "^11.2.0",
"github-slugger": "^2.0.0",
"globby": "^11.1.0",
"gray-matter": "^4.0.3",
"human-id": "^4.1.1",
"issue-parser": "^7.0.1",
"json5": "^2.2.3",
"jssm": "^5.104.1",
"jszip": "^3.10.1",
"latest-version": "^9.0.0",
"mdast": "^3.0.0",
"mdast-util-heading-range": "^4.0.0",
"mdast-util-to-string": "^4.0.0",
"minimatch": "^7.4.6",
"npm-check-updates": "^16.14.20",
"oclif": "^4.15.16",
"picocolors": "^1.1.1",
"prettier": "~3.2.5",
"prompts": "^2.4.2",
"read-pkg-up": "^7.0.1",
"remark": "^15.0.1",
"remark-gfm": "^4.0.0",
"remark-github": "^12.0.0",
"remark-github-beta-blockquote-admonitions": "^3.1.1",
"remark-toc": "^9.0.0",
"replace-in-file": "^7.2.0",
"resolve.exports": "^2.0.2",
"semver": "^7.6.3",
"semver-utils": "^1.1.4",
"simple-git": "^3.27.0",
"sort-json": "^2.0.1",
"sort-package-json": "1.57.0",
"strip-ansi": "^6.0.1",
"table": "^6.8.2",
"ts-morph": "^22.0.0",
"type-fest": "^2.19.0",
"unist-util-visit": "^5.0.0",
"xml2js": "^0.5.0"
"type-fest": "^2.19.0"
},
"devDependencies": {
"@biomejs/biome": "~1.9.3",
"@fluidframework/build-common": "^2.0.3",
"@fluidframework/eslint-config-fluid": "^5.4.0",
"@oclif/test": "^4.1.0",
"@types/async": "^3.2.24",
"@types/chai": "^4.3.20",
"@types/chai-arrays": "^2.0.3",
"@types/debug": "^4.1.12",
"@types/fs-extra": "^11.0.4",
"@types/issue-parser": "^3.0.5",
"@types/mdast": "^4.0.4",
"@types/mocha": "^10.0.9",
"@types/node": "^18.19.59",
"@types/prettier": "^2.7.3",
"@types/prompts": "^2.4.9",
"@types/semver": "^7.5.8",
"@types/semver-utils": "^1.1.3",
"@types/sort-json": "^2.0.3",
"@types/unist": "^3.0.3",
"@types/xml2js": "^0.4.14",
"c8": "^7.14.0",
"chai": "^4.5.0",
"chai-arrays": "^2.2.0",
"concurrently": "^8.2.2",
"copyfiles": "^2.4.1",
"eslint": "~8.57.0",
"eslint-config-oclif": "^5.2.1",
"eslint-config-oclif-typescript": "^3.1.12",
"eslint-config-prettier": "~9.1.0",
"jssm-viz-cli": "^5.101.0",
"knip": "^5.45.0",
"mocha": "^10.7.3",
"mocha-multi-reporters": "^1.5.1",
"mocked-env": "^1.3.5",
"rimraf": "^4.4.1",
"ts-node": "^10.9.2",
"tslib": "^2.8.0",
"typescript": "~5.4.5"
},
Expand Down
9 changes: 8 additions & 1 deletion fluidBuild.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = {
script: false,
},
"checks": {
dependsOn: ["check:format"],
dependsOn: ["check:format", "check:knip"],
script: false,
},
"checks:fix": {
Expand Down Expand Up @@ -129,6 +129,7 @@ module.exports = {
},
"check:biome": [],
"check:prettier": [],
"check:knip": [],
// ADO #7297: Review why the direct dependency on 'build:esm:test' is necessary.
// Should 'compile' be enough? compile -> build:test -> build:test:esm
"eslint": ["compile", "build:test:esm"],
Expand Down Expand Up @@ -160,6 +161,11 @@ module.exports = {

multiCommandExecutables: ["oclif", "syncpack"],
declarativeTasks: {
"knip": {
inputGlobs: ["*.*", "src/**"],
outputGlobs: ["*.*", "src/**"],
gitignore: ["input", "output"],
},
// fluid-build lowercases the executable name, so we need to use buildversion instead of buildVersion.
"flub check buildversion": {
inputGlobs: [
Expand Down Expand Up @@ -533,6 +539,7 @@ module.exports = {
["flub", "@fluid-tools/build-cli"],
["fluid-build", "@fluidframework/build-tools"],
["gf", "good-fences"],
["knip", "knip"],
["mocha", "mocha"],
["nyc", "nyc"],
["oclif", "oclif"],
Expand Down
33 changes: 33 additions & 0 deletions knip.base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/

// Enable TypeScript type-checking for this file.
// See https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html#ts-check
// @ts-check

export default {

// Knip has the capability to report many issue types. For now we are only using "dependencies" to check for unused dependency declaration in package.json
// In future, we can expand our requirements to report other issue types as well, for example: unused files, or unused exports etc.
// See: https://knip.dev/reference/cli#--include
include: ["dependencies"],
ignoreDependencies: [
// The following deps are actually in use, but depcheck reports them unused.

// These packages are used in the CI pipelines.
"mocha-multi-reporters",
"moment",

// The following deps are reported as missing, but they are available.

// We use a 'hack' to make plugins from the shared eslint config available to our packages, those these deps are not
// directly needed in the package.
"@typescript-eslint/eslint-plugin",
"eslint-config-prettier",
"eslint-import-resolver-typescript",
"eslint-plugin-tsdoc",
"eslint-plugin-unicorn",
],
};
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
"danger": "^11.3.0",
"eslint": "~8.55.0",
"jest": "^29.6.2",
"knip": "^5.45.0",
"mocha": "^10.8.2",
"prettier": "~3.0.3",
"pretty-quick": "^4.0.0",
Expand Down Expand Up @@ -259,6 +260,12 @@
],
"script": false
},
"check:knip": {
"dependsOn": [
"^check:knip"
],
"script": false
},
"checks": {
"dependsOn": [
"check:format",
Expand All @@ -267,7 +274,8 @@
"layer-check",
"syncpack:deps",
"syncpack:versions",
"check:versions"
"check:versions",
"check:knip"
],
"script": false
},
Expand Down
Loading
Loading