Skip to content

Commit

Permalink
Add depcheck for CI step to check for unused dependencies (#1677)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
- Fixes #1672 

## Description of the changes
- This PR adds a step in the unit-test workflow to check for unused
dependencies using the depcheck package.
- Certain dependencies which are installed but still being given in the
output are put into `.depcheckrc.json` file to be ignored.
- The `--skip-missing` flag is also added to ignore the missing deps
warning

## How was this change tested?
- The `ignores` array without any dep in `.depcheckrc.json` file was
pushed to the branch for testing, and it was observed that the CI
failed, which is the required behaviour: Workflow Run:
https://github.com/anshgoyalevil/jaeger-ui/actions/runs/5854765474/job/15871258991
- The `ignores` array with all unused deps was pushed for CI testing,
and the CI passed the Depcheck flow. Workflow Run:
https://github.com/anshgoyalevil/jaeger-ui/actions/runs/5854816286/job/15871414318#step:6:10
(Ignore the Lint fail error, as it is resolved in this PR, but workflow
run is not available because target push branch was changed to `main` in
that commit, and thus workflow run is not available to be shared)

## Notes:
Before merging this PR, all the unused deps which are neither added to
the `ignores` array, not have been removed need to be removed. Thus PR
#1675 needs to be merged before this one.
After that, this PR's branch needs to be updated with that code so that
the Depcheck doesn't fail in CI for this or for upcoming PRs.

## Checklist
- [x] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [x] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`
  - for `jaeger-ui`: `yarn lint` and `yarn test`

---------

Signed-off-by: Ansh Goyal <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: Pavol Loffay <[email protected]>
Signed-off-by: wck-iipi <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Pavol Loffay <[email protected]>
Co-authored-by: Yuri Shkuro <[email protected]>
Co-authored-by: Wck-iipi <[email protected]>
  • Loading branch information
5 people authored Aug 16, 2023
1 parent 662a148 commit 85a2080
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ jobs:
cache: yarn
node-version: '16'
- run: yarn install --frozen-lockfile
- name: Run depcheck
run: |
yarn global add depcheck
yarn run depcheck
- run: yarn lint
- run: yarn build
- run: yarn coverage
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"build": "lerna run build",
"check-license": "./scripts/check-license.sh",
"coverage": "lerna run coverage",
"depcheck": "./scripts/run-depcheck.sh",
"eslint": "eslint --cache 'scripts/*.{js,jsx,ts,tsx}' 'packages/*/src/**/*.{js,jsx,ts,tsx}' 'packages/*/*.{js,jsx,ts,tsx}'",
"fmt": "yarn prettier",
"lint": "npm-run-all -ln --parallel prettier-lint tsc-lint eslint check-license",
Expand Down
3 changes: 3 additions & 0 deletions packages/jaeger-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@
"react-ga": "^3.3.1",
"react-helmet": "^6.1.0",
"react-icons": "2.2.7",
"react-is": "^18.2.0",
"react-redux": "^5.0.6",
"react-router-dom": "4.3.1",
"react-router-redux": "5.0.0-alpha.8",
"react-select": "1.3.0",
"react-virtualized": "9.21.0",
"react-virtualized-select": "^3.1.0",
"react-vis": "^1.7.2",
"react-vis-force": "^0.3.1",
Expand Down
7 changes: 5 additions & 2 deletions packages/jaeger-ui/test/babel-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@

const babelJest = require('babel-jest').default;

module.exports = babelJest.createTransformer({
const babelConfiguration = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
['@babel/preset-react', { development: !process.env.CI }],
'@babel/preset-typescript',
],
plugins: ['babel-plugin-inline-react-svg'],
});
}

module.exports = babelJest.createTransformer(babelConfiguration);
module.exports.babelConfiguration = babelConfiguration;
47 changes: 47 additions & 0 deletions scripts/generateDepcheckrcJaegerUI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2023 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/* eslint-disable import/no-extraneous-dependencies */
const fs = require('fs');
const path = require('path');
const { babelConfiguration } = require('../packages/jaeger-ui/test/babel-transform');

const packageNames = [
...babelConfiguration.presets.flatMap(preset => {
if (Array.isArray(preset)) {
return [preset[0]];
}
return [preset];
}),
...babelConfiguration.plugins,
];

const otherPackages = ['jest-environment-jsdom'];

// Use the selected targetPackage for generating depcheckrcContent
const depcheckrcContent = {
ignores: [...packageNames, ...otherPackages],
'ignore-dirs': ['build'],
};

// Use the argument provided to the script as the output file path
const outputFile = process.argv[2];

if (!outputFile) {
process.exit(1);
}

const depcheckrcPath = path.resolve(__dirname, outputFile);

fs.writeFileSync(depcheckrcPath, JSON.stringify(depcheckrcContent, null, 2));
57 changes: 57 additions & 0 deletions scripts/generateDepcheckrcPlexus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2023 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/* eslint-disable import/no-extraneous-dependencies */
const fs = require('fs');
const path = require('path');
const getBabelConfig = require('../packages/plexus/babel.config');

const babelConfiguration = getBabelConfig({
env: () => {
'development';
},
});

const packageNames = [
...babelConfiguration.presets.flatMap(preset => {
if (Array.isArray(preset)) {
return [preset[0]];
}
return [preset];
}),
...babelConfiguration.plugins.flatMap(plugin => {
if (Array.isArray(plugin)) {
return [plugin[0]];
}
return [plugin];
}),
];

const otherPackages = ['rimraf', 'webpack-cli'];

// Use the selected targetPackage for generating depcheckrcContent
const depcheckrcContent = {
ignores: [...packageNames, ...otherPackages],
};

// Use the argument provided to the script as the output file path
const outputFile = process.argv[2];

if (!outputFile) {
process.exit(1);
}

const depcheckrcPath = path.resolve(__dirname, outputFile);

fs.writeFileSync(depcheckrcPath, JSON.stringify(depcheckrcContent, null, 2));
12 changes: 12 additions & 0 deletions scripts/run-depcheck.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
set -ex

# Create a temporary depcheckrc file for 'jaeger-ui'
tempfile_jaeger=$(mktemp /tmp/depcheckrc.XXXXXX.json)
node scripts/generateDepcheckrcJaegerUI.js "$tempfile_jaeger"
depcheck packages/jaeger-ui --config "$tempfile_jaeger"

# Create a temporary depcheckrc file for 'plexus'
tempfile_plexus=$(mktemp /tmp/depcheckrc.XXXXXX.json)
node scripts/generateDepcheckrcPlexus.js "$tempfile_plexus"
depcheck packages/plexus --config "$tempfile_plexus"
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10641,7 +10641,7 @@ react-router@^4.2.0, react-router@^4.3.1:
prop-types "^15.6.1"
warning "^4.0.1"

react-select@^1.0.0-rc.2:
react-select@1.3.0, react-select@^1.0.0-rc.2:
version "1.3.0"
resolved "https://registry.yarnpkg.com/react-select/-/react-select-1.3.0.tgz#1828ad5bf7f3e42a835c7e2d8cb13b5c20714876"
dependencies:
Expand Down Expand Up @@ -10701,7 +10701,7 @@ react-virtualized-select@^3.1.0:
react-select "^1.0.0-rc.2"
react-virtualized "^9.0.0"

react-virtualized@^9.0.0:
react-virtualized@9.21.0, react-virtualized@^9.0.0:
version "9.21.0"
resolved "https://registry.yarnpkg.com/react-virtualized/-/react-virtualized-9.21.0.tgz#8267c40ffb48db35b242a36dea85edcf280a6506"
dependencies:
Expand Down

0 comments on commit 85a2080

Please sign in to comment.