Skip to content

Commit

Permalink
fix(linter): ensure target project locator is using stale graph in IDE (
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Apr 25, 2023
1 parent 3d5b849 commit d47df3d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 2 additions & 10 deletions packages/eslint-plugin/src/rules/enforce-module-boundaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,23 +168,15 @@ export default createESLintRule<Options, MessageIds>({
);
const fileName = normalizePath(context.getFilename());

const { projectGraph, projectRootMappings } = readProjectGraph(RULE_NAME);
const { projectGraph, projectRootMappings, targetProjectLocator } =
readProjectGraph(RULE_NAME);

if (!projectGraph) {
return {};
}

const workspaceLayout = (global as any).workspaceLayout;

if (!(global as any).targetProjectLocator) {
(global as any).targetProjectLocator = new TargetProjectLocator(
projectGraph.nodes,
projectGraph.externalNodes
);
}
const targetProjectLocator = (global as any)
.targetProjectLocator as TargetProjectLocator;

function run(
node:
| TSESTree.ImportDeclaration
Expand Down
12 changes: 10 additions & 2 deletions packages/eslint-plugin/src/utils/project-graph-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ProjectRootMappings,
} from 'nx/src/project-graph/utils/find-project-for-path';
import { readNxJson } from 'nx/src/project-graph/file-utils';
import { TargetProjectLocator } from '@nx/js/src/internal';

export function ensureGlobalProjectGraph(ruleName: string) {
/**
Expand All @@ -25,9 +26,14 @@ export function ensureGlobalProjectGraph(ruleName: string) {
* the ProjectGraph may or may not exist by the time the lint rule is invoked for the first time.
*/
try {
(global as any).projectGraph = readCachedProjectGraph();
const projectGraph = readCachedProjectGraph();
(global as any).projectGraph = projectGraph;
(global as any).projectRootMappings = createProjectRootMappings(
(global as any).projectGraph.nodes
projectGraph.nodes
);
(global as any).targetProjectLocator = new TargetProjectLocator(
projectGraph.nodes,
projectGraph.externalNodes
);
} catch {
const WARNING_PREFIX = `${chalk.reset.keyword('orange')('warning')}`;
Expand All @@ -43,10 +49,12 @@ export function ensureGlobalProjectGraph(ruleName: string) {
export function readProjectGraph(ruleName: string): {
projectGraph: ProjectGraph;
projectRootMappings: ProjectRootMappings;
targetProjectLocator: TargetProjectLocator;
} {
ensureGlobalProjectGraph(ruleName);
return {
projectGraph: (global as any).projectGraph,
projectRootMappings: (global as any).projectRootMappings,
targetProjectLocator: (global as any).targetProjectLocator,
};
}

0 comments on commit d47df3d

Please sign in to comment.