Skip to content
Permalink
Browse files

fix: don't mutate arrays while iterating

This cropped up in a much more complicated codebase at work. Haven't nailed down an actual repro specimen yet but verified this change works there.

Issue was that the outgoing array was being iterated with a .forEach but inside of the loop .removeDependency was being called and that mutates the array. This mean that not all entries in the array would be iterated, leading to incorrect chunking results.

Fix is simple, since dependency-graph already does the right thing and cleans up dependencies when removing a node I can get rid of all my manual cleanup code and just call .removeNode when done iterating.
  • Loading branch information...
tivac committed Mar 29, 2019
1 parent d0fdb56 commit 0f63ea2422ac8e842b73c943a0224d98f991761a
@@ -5,22 +5,25 @@ const merge = (graph, original, target) => {
const incoming = graph.incomingEdges[original];
const outgoing = graph.outgoingEdges[original];

// Add original file reference to target array
graph.getNodeData(target).unshift(original);

// Copy all incoming dependencies from original to target
incoming.forEach((src) => {
graph.removeDependency(src, original);

if(src !== target) {
graph.addDependency(src, target);
if(src === target) {
return;
}

graph.addDependency(src, target);
});

// Copy all outgoing dependencies from original to target
outgoing.forEach((dest) => {
graph.removeDependency(original, dest);

if(dest !== target) {
graph.addDependency(target, dest);
if(dest === target) {
return;
}

graph.addDependency(target, dest);
});

// Bye bye
@@ -327,15 +327,33 @@ Object {
.a {
color: aqua;
}
.a2 {
color: azure;
}
/* packages/rollup/test/specimens/common-splitting/shared3.css */
.shared3 {
color: saddlebrown;
}
/* packages/rollup/test/specimens/common-splitting/b.css */
.b {
color: blue;
}
.b2 {
color: blanchedalmond;
}
",
"c.css": "/* packages/rollup/test/specimens/common-splitting/c.css */
.c {
color: cyan;
}

.c2 {
color: crimson;
}",
"shared2.css": "/* packages/rollup/test/specimens/common-splitting/shared2.css */
.shared2 {
color: sandybrown;
}
",
}
`;
@@ -3,3 +3,9 @@

color: aqua;
}

.a2 {
composes: shared2 from "./shared2.css";

color: azure;
}
@@ -3,3 +3,9 @@

color: blue;
}

.b2 {
composes: shared3 from "./shared3.css";

color: blanchedalmond;
}
@@ -1,3 +1,9 @@
.c {
color: cyan;
}

.c2 {
composes: shared2 from "./shared2.css";

color: crimson;
}
@@ -0,0 +1,3 @@
.shared2 {
color: sandybrown;
}
@@ -0,0 +1,3 @@
.shared3 {
color: saddlebrown;
}

0 comments on commit 0f63ea2

Please sign in to comment.
You can’t perform that action at this time.