Skip to content

Commit

Permalink
Fix file counting in merkletree.DirectoryTreeBuilder
Browse files Browse the repository at this point in the history
The DirectoryTreeBuilder did not check if files already existed in the
resulting map, so the file counter got wrong and an assertion failed.

Fixes bazelbuild#14286.
  • Loading branch information
moroten committed Nov 19, 2021
1 parent a729f63 commit 8c927cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ static class DirectoryNode extends Node {
super(pathSegment);
}

void addChild(Node child) {
children.add(Preconditions.checkNotNull(child, "child"));
boolean addChild(Node child) {
return children.add(Preconditions.checkNotNull(child, "child"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ private static int buildFromPaths(
throw new IOException(String.format("Input '%s' is not a file.", input));
}
Digest d = digestUtil.compute(input);
currDir.addChild(FileNode.createExecutable(path.getBaseName(), input, d));
return 1;
boolean childAdded = currDir.addChild(
FileNode.createExecutable(path.getBaseName(), input, d));
return childAdded ? 1 : 0;
});
}

Expand All @@ -120,16 +121,16 @@ private static int buildFromActionInputs(
DigestUtil digestUtil,
Map<PathFragment, DirectoryNode> tree)
throws IOException {
return build(
int ret = build(
inputs,
tree,
(input, path, currDir) -> {
if (input instanceof VirtualActionInput) {
VirtualActionInput virtualActionInput = (VirtualActionInput) input;
Digest d = digestUtil.compute(virtualActionInput);
currDir.addChild(
boolean childAdded = currDir.addChild(
FileNode.createExecutable(path.getBaseName(), virtualActionInput.getBytes(), d));
return 1;
return childAdded ? 1 : 0;
}

FileArtifactValue metadata =
Expand All @@ -141,8 +142,9 @@ private static int buildFromActionInputs(
case REGULAR_FILE:
Digest d = DigestUtil.buildDigest(metadata.getDigest(), metadata.getSize());
Path inputPath = ActionInputHelper.toInputPath(input, execRoot);
currDir.addChild(FileNode.createExecutable(path.getBaseName(), inputPath, d));
return 1;
boolean childAdded = currDir.addChild(
FileNode.createExecutable(path.getBaseName(), inputPath, d));
return childAdded ? 1 : 0;

case DIRECTORY:
SortedMap<PathFragment, ActionInput> directoryInputs =
Expand Down Expand Up @@ -170,6 +172,7 @@ private static int buildFromActionInputs(

return 0;
});
return ret;
}

private static <T> int build(
Expand Down

0 comments on commit 8c927cd

Please sign in to comment.