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(js): filter project dependencies when calculating topological ordering #26491

Merged
merged 3 commits into from
Jun 11, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('sortProjectsTopologically', () => {
expect(result).toEqual([projectGraph.nodes.project1]);
});

it('should return projects in the correct order', () => {
it('should return [2,1] if 1 depends on 2', () => {
const projectGraph = {
dependencies: {
project1: [
Expand Down Expand Up @@ -111,4 +111,146 @@ describe('sortProjectsTopologically', () => {
const result = sortProjectsTopologically(projectGraph, projectNodes);
expect(result).toEqual(projectNodes);
});

it('should return [3,2,1] if 1 depends on 2 and 2 depends on 3', () => {
const projectGraph = {
dependencies: {
project1: [
{
source: 'project1',
target: 'project2',
type: 'static',
},
],
project2: [
{
source: 'project2',
target: 'project3',
type: 'static',
},
],
},
nodes: {
project1: {
name: 'project1',
data: {
root: '',
},
type: 'app' as const,
},
project2: {
name: 'project2',
data: {
root: '',
},
type: 'app' as const,
},
project3: {
name: 'project3',
data: {
root: '',
},
type: 'app' as const,
},
},
};
const projectNodes = [
projectGraph.nodes.project1,
projectGraph.nodes.project2,
projectGraph.nodes.project3,
];
const result = sortProjectsTopologically(projectGraph, projectNodes);
expect(result).toEqual([
projectGraph.nodes.project3,
projectGraph.nodes.project2,
projectGraph.nodes.project1,
]);
});

it('should return [1,2,3,4] if 1 has zero dependencies, 2 has one, 3 has two, and 4 has three', () => {
const projectGraph = {
dependencies: {
project1: [],
project2: [
{
source: 'project2',
target: 'project1',
type: 'static',
},
],
project3: [
{
source: 'project3',
target: 'project1',
type: 'static',
},
{
source: 'project3',
target: 'project2',
type: 'static',
},
],
project4: [
{
source: 'project4',
target: 'project3',
type: 'static',
},
{
source: 'project4',
target: 'project2',
type: 'static',
},
{
source: 'project4',
target: 'project1',
type: 'static',
},
],
},
nodes: {
project1: {
name: 'project1',
data: {
root: '',
},
type: 'app' as const,
},
project2: {
name: 'project2',
data: {
root: '',
},
type: 'app' as const,
},
project3: {
name: 'project3',
data: {
root: '',
},
type: 'app' as const,
},
project4: {
name: 'project4',
data: {
root: '',
},
type: 'app' as const,
},
},
};
const projectNodes = [
projectGraph.nodes.project1,
projectGraph.nodes.project2,
projectGraph.nodes.project3,
projectGraph.nodes.project4,
];
const result = sortProjectsTopologically(projectGraph, projectNodes);
expect(result).toEqual([
projectGraph.nodes.project1,
projectGraph.nodes.project2,
projectGraph.nodes.project3,
projectGraph.nodes.project4,
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ export function sortProjectsTopologically(
sortedProjects.push(node);

// Process each project that depends on the current node
filteredDependencies.forEach((dep) => {
const dependentNode = projectGraph.nodes[dep.source];
const count = edges.get(dependentNode) - 1;
edges.set(dependentNode, count);
if (count === 0) {
processQueue.push(dependentNode);
}
});
filteredDependencies
.filter((dep) => dep.target === node.name)
.forEach((dep) => {
const dependentNode = projectGraph.nodes[dep.source];
const count = edges.get(dependentNode) - 1;
edges.set(dependentNode, count);
if (count === 0) {
processQueue.push(dependentNode);
}
});
}

/**
Expand Down