diff --git a/e2e/eslint/src/linter.test.ts b/e2e/eslint/src/linter.test.ts index ab96bf8338..0afb94d9a0 100644 --- a/e2e/eslint/src/linter.test.ts +++ b/e2e/eslint/src/linter.test.ts @@ -478,13 +478,21 @@ describe('Linter', () => { `import { names } from '@nx/devkit';\n\n` + content.replace(/=> .*;/, `=> names('${mylib}').className;`) ); + // intentionally set an obsolete dependency + updateJson(`libs/${mylib}/package.json`, (json) => { + json.dependencies['@nx/js'] = nxVersion; + return json; + }); - // output should now report missing dependency + // output should now report missing dependency and obsolete dependency out = runCLI(`lint ${mylib}`, { silenceError: true }); expect(out).toContain('they are missing'); expect(out).toContain('@nx/devkit'); + expect(out).toContain( + `The "@nx/js" package is not used by "${mylib}" project` + ); - // should fix the missing dependency issue + // should fix the missing and obsolete dependency issues out = runCLI(`lint ${mylib} --fix`, { silenceError: true }); expect(out).toContain( `Successfully ran target lint for project ${mylib}` diff --git a/packages/eslint-plugin/src/rules/dependency-checks.ts b/packages/eslint-plugin/src/rules/dependency-checks.ts index ed06be31be..5978e2a1f8 100644 --- a/packages/eslint-plugin/src/rules/dependency-checks.ts +++ b/packages/eslint-plugin/src/rules/dependency-checks.ts @@ -152,14 +152,9 @@ export default ESLintUtils.RuleCreator( ); const expectedDependencyNames = Object.keys(npmDependencies); - const projPackageJsonPath = join( - workspaceRoot, - sourceProject.data.root, - 'package.json' - ); + const packageJson = JSON.parse(context.sourceCode.getText()); + const projPackageJsonDeps = getProductionDependencies(packageJson); - const projPackageJsonDeps: Record = - getProductionDependencies(projPackageJsonPath); const rootPackageJsonDeps = getAllDependencies(rootPackageJson); function validateMissingDependencies(node: AST.JSONProperty) { diff --git a/packages/eslint-plugin/src/utils/package-json-utils.ts b/packages/eslint-plugin/src/utils/package-json-utils.ts index 2369dfe6c2..a882674143 100644 --- a/packages/eslint-plugin/src/utils/package-json-utils.ts +++ b/packages/eslint-plugin/src/utils/package-json-utils.ts @@ -1,7 +1,6 @@ import { readJsonFile } from '@nx/devkit'; import { existsSync } from 'fs'; -import { PackageJson } from 'nx/src/utils/package-json'; -import { isTerminalRun } from './runtime-lint-utils'; +import type { PackageJson } from 'nx/src/utils/package-json'; export function getAllDependencies( packageJson: PackageJson @@ -15,23 +14,13 @@ export function getAllDependencies( } export function getProductionDependencies( - packageJsonPath: string + packageJson: PackageJson ): Record { - if ( - !globalThis.projPackageJsonDeps || - !globalThis.projPackageJsonDeps[packageJsonPath] || - !isTerminalRun() - ) { - const packageJson = getPackageJson(packageJsonPath); - globalThis.projPackageJsonDeps = globalThis.projPackageJsonDeps || {}; - globalThis.projPackageJsonDeps[packageJsonPath] = { - ...packageJson.dependencies, - ...packageJson.peerDependencies, - ...packageJson.optionalDependencies, - }; - } - - return globalThis.projPackageJsonDeps[packageJsonPath]; + return { + ...packageJson.dependencies, + ...packageJson.peerDependencies, + ...packageJson.optionalDependencies, + }; } export function getPackageJson(path: string): PackageJson {