Skip to content

Commit

Permalink
fix(linter): fix dep-checks projPackageJsonDeps caching for IDE
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Sep 5, 2023
1 parent 99eebfb commit 0d12270
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
5 changes: 1 addition & 4 deletions packages/eslint-plugin/src/rules/dependency-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,8 @@ export default createESLintRule<Options, MessageIds>({
'package.json'
);

globalThis.projPackageJsonDeps ??= getProductionDependencies(
getPackageJson(projPackageJsonPath)
);
const projPackageJsonDeps: Record<string, string> =
globalThis.projPackageJsonDeps;
getProductionDependencies(projPackageJsonPath);
const rootPackageJsonDeps = getAllDependencies(rootPackageJson);

function validateMissingDependencies(node: AST.JSONProperty) {
Expand Down
20 changes: 13 additions & 7 deletions packages/eslint-plugin/src/utils/package-json-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ProjectFileMap, readJsonFile } from '@nx/devkit';
import { readJsonFile } from '@nx/devkit';
import { existsSync } from 'fs';
import { PackageJson } from 'nx/src/utils/package-json';
import { isTerminalRun } from './runtime-lint-utils';

export function getAllDependencies(
packageJson: PackageJson
Expand All @@ -14,13 +15,18 @@ export function getAllDependencies(
}

export function getProductionDependencies(
packageJson: PackageJson
packageJsonPath: string
): Record<string, string> {
return {
...packageJson.dependencies,
...packageJson.peerDependencies,
...packageJson.optionalDependencies,
};
if (!globalThis.projPackageJsonDeps || !isTerminalRun()) {
const packageJson = getPackageJson(packageJsonPath);
globalThis.projPackageJsonDeps = {
...packageJson.dependencies,
...packageJson.peerDependencies,
...packageJson.optionalDependencies,
};
}

return globalThis.projPackageJsonDeps;
}

export function getPackageJson(path: string): PackageJson {
Expand Down
24 changes: 12 additions & 12 deletions packages/eslint-plugin/src/utils/project-graph-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ export function ensureGlobalProjectGraph(ruleName: string) {
* Enforce every IDE change to get a fresh nxdeps.json
*/
if (
!(global as any).projectGraph ||
!(global as any).projectRootMappings ||
!(global as any).projectFileMap ||
!globalThis.projectGraph ||
!globalThis.projectRootMappings ||
!globalThis.projectFileMap ||
!isTerminalRun()
) {
const nxJson = readNxJson();
(global as any).workspaceLayout = nxJson.workspaceLayout;
globalThis.workspaceLayout = nxJson.workspaceLayout;

/**
* Because there are a number of ways in which the rule can be invoked (executor vs ESLint CLI vs IDE Plugin),
* the ProjectGraph may or may not exist by the time the lint rule is invoked for the first time.
*/
try {
const projectGraph = readCachedProjectGraph();
(global as any).projectGraph = projectGraph;
(global as any).projectRootMappings = createProjectRootMappings(
globalThis.projectGraph = projectGraph;
globalThis.projectRootMappings = createProjectRootMappings(
projectGraph.nodes
);
(global as any).projectFileMap = readProjectFileMapCache().projectFileMap;
(global as any).targetProjectLocator = new TargetProjectLocator(
globalThis.projectFileMap = readProjectFileMapCache().projectFileMap;
globalThis.targetProjectLocator = new TargetProjectLocator(
projectGraph.nodes,
projectGraph.externalNodes
);
Expand All @@ -61,9 +61,9 @@ export function readProjectGraph(ruleName: string): {
} {
ensureGlobalProjectGraph(ruleName);
return {
projectGraph: (global as any).projectGraph,
projectFileMap: (global as any).projectFileMap,
projectRootMappings: (global as any).projectRootMappings,
targetProjectLocator: (global as any).targetProjectLocator,
projectGraph: globalThis.projectGraph,
projectFileMap: globalThis.projectFileMap,
projectRootMappings: globalThis.projectRootMappings,
targetProjectLocator: globalThis.targetProjectLocator,
};
}

0 comments on commit 0d12270

Please sign in to comment.