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 fileName = normalizePath(context.getFilename());
const { projectGraph, projectRootMappings } = readProjectGraph(RULE_NAME); const { projectGraph, projectRootMappings, targetProjectLocator } =
readProjectGraph(RULE_NAME);
if (!projectGraph) { if (!projectGraph) {
return {}; return {};
@ -176,15 +177,6 @@ export default createESLintRule<Options, MessageIds>({
const workspaceLayout = (global as any).workspaceLayout; 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( function run(
node: node:
| TSESTree.ImportDeclaration | TSESTree.ImportDeclaration

View File

@ -6,6 +6,7 @@ import {
ProjectRootMappings, ProjectRootMappings,
} from 'nx/src/project-graph/utils/find-project-for-path'; } from 'nx/src/project-graph/utils/find-project-for-path';
import { readNxJson } from 'nx/src/project-graph/file-utils'; import { readNxJson } from 'nx/src/project-graph/file-utils';
import { TargetProjectLocator } from '@nx/js/src/internal';
export function ensureGlobalProjectGraph(ruleName: string) { 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. * the ProjectGraph may or may not exist by the time the lint rule is invoked for the first time.
*/ */
try { try {
(global as any).projectGraph = readCachedProjectGraph(); const projectGraph = readCachedProjectGraph();
(global as any).projectGraph = projectGraph;
(global as any).projectRootMappings = createProjectRootMappings( (global as any).projectRootMappings = createProjectRootMappings(
(global as any).projectGraph.nodes projectGraph.nodes
);
(global as any).targetProjectLocator = new TargetProjectLocator(
projectGraph.nodes,
projectGraph.externalNodes
); );
} catch { } catch {
const WARNING_PREFIX = `${chalk.reset.keyword('orange')('warning')}`; const WARNING_PREFIX = `${chalk.reset.keyword('orange')('warning')}`;
@ -43,10 +49,12 @@ export function ensureGlobalProjectGraph(ruleName: string) {
export function readProjectGraph(ruleName: string): { export function readProjectGraph(ruleName: string): {
projectGraph: ProjectGraph; projectGraph: ProjectGraph;
projectRootMappings: ProjectRootMappings; projectRootMappings: ProjectRootMappings;
targetProjectLocator: TargetProjectLocator;
} { } {
ensureGlobalProjectGraph(ruleName); ensureGlobalProjectGraph(ruleName);
return { return {
projectGraph: (global as any).projectGraph, projectGraph: (global as any).projectGraph,
projectRootMappings: (global as any).projectRootMappings, projectRootMappings: (global as any).projectRootMappings,
targetProjectLocator: (global as any).targetProjectLocator,
}; };
} }