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): use projectGraph in hasher for calculating dependencies #9118

Merged
merged 2 commits into from Mar 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 24 additions & 14 deletions packages/linter/src/executors/eslint/hasher.ts
@@ -1,7 +1,8 @@
import { Task, TaskGraph } from '@nrwl/devkit';
import { ProjectGraph, Task, TaskGraph } from '@nrwl/devkit';
import { Hash, Hasher } from '@nrwl/workspace/src/core/hasher/hasher';
import { appRootPath } from '@nrwl/tao/src/utils/app-root';
import { Workspaces } from '@nrwl/tao/src/shared/workspace';
import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph';

export default async function run(
task: Task,
Expand All @@ -11,14 +12,22 @@ export default async function run(
if (task.overrides['hasTypeAwareRules'] === true) {
return hasher.hashTaskWithDepsAndContext(task);
}
if (!(global as any).projectGraph) {
try {
(global as any).projectGraph = readCachedProjectGraph();
} catch {
// do nothing, if project graph is unavailable we fallback to using all projects
}
}
const projectGraph = (global as any).projectGraph;
const command = hasher.hashCommand(task);
const sources = await hasher.hashSource(task);
// TODO: This will be used once we pass hasher's projectGraph
// const deps = allDeps(task.id, taskGraph);
const workspace = new Workspaces(appRootPath).readWorkspaceConfiguration();
const deps = projectGraph
? allDeps(task.id, taskGraph, projectGraph)
: Object.keys(workspace.projects);
const tags = hasher.hashArray(
// deps.map((d) => (workspace.projects[d].tags || []).join('|'))
Object.values(workspace.projects).map((proj) => (proj.tags || []).join('|'))
deps.map((d) => (workspace.projects[d].tags || []).join('|'))
);
const context = await hasher.hashContext();
return {
Expand All @@ -38,13 +47,14 @@ export default async function run(
};
}

function allDeps(taskId: string, taskGraph: TaskGraph): string[] {
return [
...taskGraph.dependencies[taskId].map(
(task) => taskGraph.tasks[task].target.project
),
...taskGraph.dependencies[taskId].flatMap((task) =>
allDeps(task, taskGraph)
),
];
function allDeps(
taskId: string,
taskGraph: TaskGraph,
projectGraph: ProjectGraph
): string[] {
const project = taskGraph.tasks[taskId].target.project;
const dependencies = projectGraph.dependencies[project]
.filter((d) => !!projectGraph.nodes[d.target])
.map((d) => d.target);
return dependencies;
}