ci: introduce knip for checking unused packages

fixes #21948

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2023-12-21 09:00:59 +02:00
parent 00175d889a
commit 23a29697aa
153 changed files with 2345 additions and 9 deletions
+11
View File
@@ -1,5 +1,6 @@
# Knip report
<<<<<<< HEAD
## Unused devDependencies (3)
| Name | Location |
@@ -7,4 +8,14 @@
| @backstage/cli | package.json |
| @backstage/test-utils | package.json |
| @testing-library/react | package.json |
=======
## Unused devDependencies (4)
| Name | Location |
|:--------------------------|:-------------|
| @backstage/cli | package.json |
| @backstage/test-utils | package.json |
| @testing-library/react | package.json |
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
+15
View File
@@ -1,6 +1,10 @@
# Knip report
<<<<<<< HEAD
## Unused dependencies (64)
=======
## Unused dependencies (63)
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| Name | Location |
|:-------------------------------------------------|:-------------|
@@ -30,7 +34,10 @@
| @backstage/plugin-puppetdb | package.json |
| @backstage/plugin-playlist | package.json |
| @backstage/plugin-newrelic | package.json |
<<<<<<< HEAD
| @backstage/plugin-linguist | package.json |
=======
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| @backstage/plugin-devtools | package.json |
| @backstage/plugin-api-docs | package.json |
| @backstage/plugin-airbrake | package.json |
@@ -69,7 +76,11 @@
| @roadiehq/backstage-plugin-github-pull-requests | package.json |
| @backstage/plugin-techdocs-module-addons-contrib | package.json |
<<<<<<< HEAD
## Unused devDependencies (7)
=======
## Unused devDependencies (8)
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| Name | Location |
|:----------------------------|:-------------|
@@ -79,5 +90,9 @@
| @types/zen-observable | package.json |
| @backstage/test-utils | package.json |
| @testing-library/react | package.json |
<<<<<<< HEAD
=======
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| @testing-library/user-event | package.json |
+14
View File
@@ -15,12 +15,21 @@
| @vitejs/plugin-react | package.json |
| vite-plugin-node-polyfills | package.json |
| @backstage/plugin-airbrake | package.json |
<<<<<<< HEAD
| @backstage/frontend-app-api | package.json |
| @backstage/plugin-azure-sites | package.json |
| @backstage/plugin-search-common | package.json |
| @backstage/plugin-linguist-common | package.json |
## Unused devDependencies (5)
=======
| @backstage/plugin-azure-sites | package.json |
| @backstage/plugin-search-common | package.json |
| @backstage/plugin-stack-overflow | package.json |
| @backstage/plugin-linguist-common | package.json |
## Unused devDependencies (7)
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| Name | Location |
|:----------------------------|:-------------|
@@ -28,6 +37,11 @@
| @types/jquery | package.json |
| @testing-library/dom | package.json |
| @types/zen-observable | package.json |
<<<<<<< HEAD
=======
| @testing-library/react | package.json |
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| @testing-library/user-event | package.json |
## Unlisted dependencies (1)
+10
View File
@@ -1,9 +1,19 @@
# Knip report
<<<<<<< HEAD
## Unused devDependencies (2)
| Name | Location |
|:----------------|:-------------|
| @backstage/cli | package.json |
| wait-for-expect | package.json |
=======
## Unused devDependencies (3)
| Name | Location |
|:------------------------------|:-------------|
| @backstage/cli | package.json |
| wait-for-expect | package.json |
| @backstage/backend-test-utils | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
+4
View File
@@ -14,7 +14,11 @@
| pg-connection-string | package.json |
| azure-devops-node-api | package.json |
| @backstage/plugin-tech-insights-node | package.json |
<<<<<<< HEAD
| @backstage/plugin-azure-sites-common | package.json |
=======
| @backstage/plugin-azure-sites-backend | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| @backstage/plugin-scaffolder-backend-module-rails | package.json |
## Unused devDependencies (3)
+31
View File
@@ -33,11 +33,18 @@
| @spotify/eslint-config-react | package.json |
| @typescript-eslint/eslint-plugin | package.json |
<<<<<<< HEAD
## Unused devDependencies (18)
| Name | Location |
|:----------------------------------------|:-------------|
| msw | package.json |
=======
## Unused devDependencies (13)
| Name | Location |
|:----------------------------------------|:-------------|
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| del | package.json |
| ts-node | package.json |
| nodemon | package.json |
@@ -46,6 +53,7 @@
| @backstage/theme | package.json |
| @types/http-proxy | package.json |
| @backstage/dev-utils | package.json |
<<<<<<< HEAD
| @backstage/test-utils | package.json |
| @backstage/core-app-api | package.json |
| @backstage/backend-common | package.json |
@@ -53,6 +61,11 @@
| @backstage/core-components | package.json |
| @types/terser-webpack-plugin | package.json |
| @backstage/backend-test-utils | package.json |
=======
| @backstage/core-app-api | package.json |
| @backstage/core-components | package.json |
| @types/terser-webpack-plugin | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| @backstage/backend-plugin-api | package.json |
| @types/rollup-plugin-peer-deps-external | package.json |
@@ -65,6 +78,7 @@
| @vitejs/plugin-react | package.json |
| vite-plugin-node-polyfills | package.json |
<<<<<<< HEAD
## Unlisted dependencies (3)
| Name | Location |
@@ -72,4 +86,21 @@
| prettier | src/commands/migrate/packageLintConfigs.ts |
| react-dom/client | src/lib/bundler/hasReactDomClient.ts |
| react/package.json | src/lib/bundler/server.ts |
=======
## Unlisted dependencies (11)
| Name | Location |
|:----------------------------------|:-------------------------------------------------------------------------------------------------|
| prettier | src/commands/migrate/packageLintConfigs.ts |
| react-dom/client | src/lib/bundler/hasReactDomClient.ts |
| react/package.json | src/lib/bundler/server.ts |
| supertest | templates/default-backend-plugin/src/service/router.test.ts |
| winston | templates/default-backend-plugin/src/service/router.ts |
| express-promise-router | templates/default-backend-plugin/src/service/router.ts |
| react | templates/default-react-plugin-package/src/components/ExampleComponent/ExampleComponent.test.tsx |
| @testing-library/react | templates/default-react-plugin-package/src/components/ExampleComponent/ExampleComponent.test.tsx |
| react | templates/default-react-plugin-package/src/components/ExampleComponent/ExampleComponent.tsx |
| @material-ui/core | templates/default-react-plugin-package/src/components/ExampleComponent/ExampleComponent.tsx |
| @backstage/plugin-scaffolder-node | templates/scaffolder-module/src/actions/example/example.ts |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
+8
View File
@@ -6,7 +6,11 @@
|:--------|:-------------|
| history | package.json |
<<<<<<< HEAD
## Unused devDependencies (11)
=======
## Unused devDependencies (12)
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| Name | Location |
|:-----------------------------|:-------------|
@@ -19,6 +23,10 @@
| @backstage/test-utils | package.json |
| @testing-library/react | package.json |
| react-router-dom-stable | package.json |
<<<<<<< HEAD
=======
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| @testing-library/user-event | package.json |
| @testing-library/react-hooks | package.json |
+6
View File
@@ -12,9 +12,15 @@
|:--------------------------------------|:-------------|
| @backstage/cli | package.json |
| @testing-library/react | package.json |
<<<<<<< HEAD
| @backstage/plugin-catalog | package.json |
| @backstage/plugin-puppetdb | package.json |
| @backstage/frontend-app-api | package.json |
=======
| @testing-library/jest-dom | package.json |
| @backstage/plugin-catalog | package.json |
| @backstage/plugin-puppetdb | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| @backstage/plugin-stackstorm | package.json |
| @backstage/frontend-test-utils | package.json |
| @oriflame/backstage-plugin-score-card | package.json |
+12
View File
@@ -9,7 +9,11 @@
| zen-observable | package.json |
| @backstage/config | package.json |
<<<<<<< HEAD
## Unused devDependencies (9)
=======
## Unused devDependencies (11)
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| Name | Location |
|:----------------------------|:-------------|
@@ -18,9 +22,17 @@
| @backstage/cli | package.json |
| @testing-library/dom | package.json |
| @types/zen-observable | package.json |
<<<<<<< HEAD
| @types/google-protobuf | package.json |
| @testing-library/react | package.json |
| @backstage/app-defaults | package.json |
=======
| @backstage/test-utils | package.json |
| @types/google-protobuf | package.json |
| @testing-library/react | package.json |
| @backstage/core-app-api | package.json |
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| @testing-library/user-event | package.json |
## Unlisted dependencies (1)
+8
View File
@@ -6,7 +6,11 @@
|:--------|:-------------|
| history | package.json |
<<<<<<< HEAD
## Unused devDependencies (6)
=======
## Unused devDependencies (7)
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| Name | Location |
|:----------------------------|:-------------|
@@ -15,5 +19,9 @@
| @backstage/test-utils | package.json |
| @testing-library/react | package.json |
| @backstage/core-app-api | package.json |
<<<<<<< HEAD
=======
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| @testing-library/user-event | package.json |
+7
View File
@@ -1,10 +1,17 @@
# Knip report
<<<<<<< HEAD
## Unused devDependencies (6)
| Name | Location |
|:------------------------------|:-------------|
| msw | package.json |
=======
## Unused devDependencies (5)
| Name | Location |
|:------------------------------|:-------------|
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| ts-node | package.json |
| nodemon | package.json |
| @backstage/cli | package.json |
+8
View File
@@ -6,7 +6,11 @@
|:-----------------|:-------------|
| @backstage/theme | package.json |
<<<<<<< HEAD
## Unused devDependencies (6)
=======
## Unused devDependencies (7)
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| Name | Location |
|:----------------------------|:-------------|
@@ -15,5 +19,9 @@
| @testing-library/dom | package.json |
| @backstage/test-utils | package.json |
| @testing-library/react | package.json |
<<<<<<< HEAD
=======
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| @testing-library/user-event | package.json |
+10
View File
@@ -1,9 +1,19 @@
# Knip report
<<<<<<< HEAD
## Unlisted dependencies (2)
| Name | Location |
|:-------|:-------------------------------|
| estree | lib/visitImports.js |
| estree | rules/no-undeclared-imports.js |
=======
## Unlisted dependencies (3)
| Name | Location |
|:--------------------------|:-------------------------------|
| @typescript-eslint/parser | .eslintrc.js |
| estree | lib/visitImports.js |
| estree | rules/no-undeclared-imports.js |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
+11
View File
@@ -1,5 +1,6 @@
# Knip report
<<<<<<< HEAD
## Unused devDependencies (3)
| Name | Location |
@@ -7,4 +8,14 @@
| @backstage/cli | package.json |
| @backstage/test-utils | package.json |
| @testing-library/react | package.json |
=======
## Unused devDependencies (4)
| Name | Location |
|:--------------------------|:-------------|
| @backstage/cli | package.json |
| @backstage/test-utils | package.json |
| @testing-library/react | package.json |
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
@@ -8,7 +8,11 @@
| @material-ui/core | package.json |
| @backstage/core-components | package.json |
<<<<<<< HEAD
## Unused devDependencies (6)
=======
## Unused devDependencies (7)
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| Name | Location |
|:-------------------------------|:-------------|
@@ -16,6 +20,10 @@
| @backstage/cli | package.json |
| @backstage/test-utils | package.json |
| @testing-library/react | package.json |
<<<<<<< HEAD
=======
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
| @backstage/frontend-app-api | package.json |
| @backstage/frontend-test-utils | package.json |
@@ -1,8 +1,17 @@
# Knip report
<<<<<<< HEAD
## Unused devDependencies (1)
| Name | Location |
|:---------------|:-------------|
| @backstage/cli | package.json |
=======
## Unused devDependencies (2)
| Name | Location |
|:--------------------------|:-------------|
| @backstage/cli | package.json |
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
+20
View File
@@ -1,5 +1,6 @@
# Knip report
<<<<<<< HEAD
## Unused devDependencies (4)
| Name | Location |
@@ -8,4 +9,23 @@
| @backstage/cli | package.json |
| @testing-library/dom | package.json |
| @backstage/test-utils | package.json |
=======
## Unused dependencies (1)
| Name | Location |
|:------------------|:-------------|
| @material-ui/core | package.json |
## Unused devDependencies (7)
| Name | Location |
|:---------------------------|:-------------|
| msw | package.json |
| @backstage/cli | package.json |
| @testing-library/dom | package.json |
| @backstage/dev-utils | package.json |
| @backstage/test-utils | package.json |
| @testing-library/jest-dom | package.json |
| @backstage/core-components | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
@@ -290,10 +290,10 @@ function logApiReportInstructions() {
'*************************************************************************************',
);
console.log(
'* You have uncommitted changes to the public API of a package. *',
'* You have uncommitted changes to the public API or reports of a package. *',
);
console.log(
'* To solve this, run `yarn build:api-reports` and commit all api-report.md changes. *',
'* To solve this, run `yarn build:api-reports` and commit all md file changes. *',
);
console.log(
'*************************************************************************************',
@@ -1418,3 +1418,66 @@ export async function runCliExtraction({
}
}
}
interface KnipExtractionOptions {
packageDirs: string[];
isLocalBuild: boolean;
}
export async function runKnipReports({
packageDirs,
isLocalBuild,
}: KnipExtractionOptions) {
const knipDir = cliPaths.resolveTargetRoot('./node_modules/knip/bin/');
for (const packageDir of packageDirs) {
console.log(`## Processing ${packageDir}`);
const fullDir = cliPaths.resolveTargetRoot(packageDir);
const reportPath = resolvePath(fullDir, 'knip-report.md');
const run = createBinRunner(fullDir, '');
const report = await run(
`${knipDir}/knip.js`,
`--directory ${fullDir}`, // Run in the package directory
'--no-exit-code', // Removing this will end the process in case there are findings by knip
'--no-progress', // Remove unnecessary debugging from output
// TODO: Add more checks when dependencies start to look ok, see https://knip.dev/reference/cli#--include
'--include dependencies,unlisted',
'--reporter markdown',
);
const existingReport = await fs
.readFile(reportPath, 'utf8')
.catch(error => {
if (error.code === 'ENOENT') {
return undefined;
}
throw error;
});
if (existingReport !== report) {
if (isLocalBuild) {
console.warn(`Knip report changed for ${packageDir}`);
await fs.writeFile(reportPath, report);
} else {
logApiReportInstructions();
if (existingReport) {
console.log('');
console.log(
`The conflicting file is ${relativePath(
cliPaths.targetRoot,
reportPath,
)}, expecting the following content:`,
);
console.log('');
console.log(report);
logApiReportInstructions();
}
throw new Error(`Knip report changed for ${packageDir}, `);
}
}
}
}
@@ -43,6 +43,7 @@ jest.mock('./api-extractor', () => ({
runApiExtraction: jest.fn(),
runCliExtraction: jest.fn(),
buildDocs: jest.fn(),
runKnipReports: jest.fn(),
}));
const projectPaths = pathsLib.paths;
@@ -21,6 +21,10 @@ import {
createTemporaryTsConfig,
runApiExtraction,
runCliExtraction,
<<<<<<< HEAD
=======
runKnipReports,
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
} from './api-extractor';
import { paths as cliPaths, resolvePackagePaths } from '../../lib/paths';
import { generateTypeDeclarations } from './generateTypeDeclarations';
@@ -103,6 +107,14 @@ export const buildApiReports = async (paths: string[] = [], opts: Options) => {
});
}
if (selectedPackageDirs.length > 0) {
console.log('# Generating package knip reports');
await runKnipReports({
packageDirs: selectedPackageDirs,
isLocalBuild: !isCiBuild,
});
}
if (isDocsBuild) {
console.log('# Generating package documentation');
await buildDocs({
+10
View File
@@ -6,10 +6,20 @@
|:------------------|:-------------|
| @material-ui/core | package.json |
<<<<<<< HEAD
## Unused devDependencies (2)
| Name | Location |
|:---------------|:-------------|
| msw | package.json |
| @backstage/cli | package.json |
=======
## Unused devDependencies (3)
| Name | Location |
|:--------------------------|:-------------|
| msw | package.json |
| @backstage/cli | package.json |
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
+11
View File
@@ -7,6 +7,7 @@
| @emotion/react | package.json |
| @emotion/styled | package.json |
<<<<<<< HEAD
## Unused devDependencies (3)
| Name | Location |
@@ -14,4 +15,14 @@
| @mui/styles | package.json |
| @backstage/cli | package.json |
| @testing-library/react | package.json |
=======
## Unused devDependencies (4)
| Name | Location |
|:--------------------------|:-------------|
| @mui/styles | package.json |
| @backstage/cli | package.json |
| @testing-library/react | package.json |
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)
+10
View File
@@ -1,9 +1,19 @@
# Knip report
<<<<<<< HEAD
## Unused devDependencies (2)
| Name | Location |
|:-----------------------|:-------------|
| @backstage/cli | package.json |
| @testing-library/react | package.json |
=======
## Unused devDependencies (3)
| Name | Location |
|:--------------------------|:-------------|
| @backstage/cli | package.json |
| @testing-library/react | package.json |
| @testing-library/jest-dom | package.json |
>>>>>>> a99592ad081 (ci: introduce knip for checking unused packages)