From b6992070705bd63a08ac7785afe70aaa0d5e9912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Fri, 16 Aug 2024 17:14:09 +0200 Subject: [PATCH] fix(linter): update the @nx/dependency-checks rule to read the package.json content from the rule context (#27476) ## Current Behavior The `@nx/dependency-checks` rule sometimes doesn't apply all identified fixes and wrongly succeeds. ESLint performs multiple passes to try to apply fixes, and in each pass the rule receives the updated source code with the previously applied fixes. This allows merging different fixes, but the `@nx/dependency-checks` rule always reads the `package.json` file from the filesystem, caches it globally, and mutates it. It never uses the updated source code provided in the rule context. ## Expected Behavior The `@nx/dependency-checks` rule should apply all identified fixes [as long as they don't conflict](https://eslint.org/docs/latest/extend/custom-rules#conflicting-fixes). ## Related Issue(s) Fixes #27412 --- e2e/eslint/src/linter.test.ts | 12 +++++++-- .../src/rules/dependency-checks.ts | 9 ++----- .../src/utils/package-json-utils.ts | 25 ++++++------------- 3 files changed, 19 insertions(+), 27 deletions(-) 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 {