fix(linter): ensure target project locator is using stale graph in IDE (#16534)

This commit is contained in:
Miroslav Jonaš 2023-04-25 23:50:12 +02:00 committed by GitHub
parent 3d5b849826
commit d47df3d676
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View File

@ -168,7 +168,8 @@ 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 {};
@ -176,15 +177,6 @@ export default createESLintRule<Options, MessageIds>({
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

View File

@ -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) {
/**
@ -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')}`;
@ -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,
};
}