Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter): fix dep-checks projPackageJsonDeps caching for IDE #18935

Merged
merged 1 commit into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};
}