Skip to content

Commit

Permalink
Keep the result order deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Sep 27, 2024
1 parent be716a4 commit df76dfc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ Stream<DefaultModelBuilderResult> results(DefaultModelBuilderResult r) {
@SuppressWarnings("checkstyle:MethodLength")
private void loadFromRoot(Path root, Path top) {
try (PhasingExecutor executor = createExecutor()) {
loadFilePom(executor, top, root, Set.of(), null);
loadFilePom(executor, top, root, Set.of(), getResultForPom(top, root, null), Objects.equals(top, root));
}
if (result.getFileModel() == null && !Objects.equals(top, root)) {
logger.warn(
Expand All @@ -759,16 +759,12 @@ private void loadFromRoot(Path root, Path top) {
}

private void loadFilePom(
Executor executor, Path top, Path pom, Set<Path> parents, DefaultModelBuilderResult parent) {
DefaultModelBuilderResult r;
if (pom.equals(top)) {
r = result;
} else {
r = new DefaultModelBuilderResult(parent);
if (parent != null) {
parent.getChildren().add(r);
}
}
Executor executor,
Path top,
Path pom,
Set<Path> parents,
DefaultModelBuilderResult r,
boolean isInResultTree) {
try {
Path pomDirectory = Files.isDirectory(pom) ? pom : pom.getParent();
ModelSource src = ModelSource.fromPath(pom);
Expand Down Expand Up @@ -821,12 +817,17 @@ private void loadFilePom(
continue;
}

// Compute the result for the subproject now so that we can have a correct order of the results
DefaultModelBuilderResult subprojectResult = getResultForPom(top, subprojectFile, r);
boolean subprojectsInResultTree =
Objects.equals(top, subprojectFile) || isInResultTree && request.isRecursive();
executor.execute(() -> loadFilePom(
executor,
top,
subprojectFile,
concat(parents, pom),
(parent != null || Objects.equals(pom, top)) && request.isRecursive() ? r : null));
subprojectResult,
subprojectsInResultTree));
}
} catch (ModelBuilderException e) {
// gathered with problem collector
Expand All @@ -837,6 +838,19 @@ private void loadFilePom(
}
}

private DefaultModelBuilderResult getResultForPom(Path top, Path pom, DefaultModelBuilderResult parent) {
DefaultModelBuilderResult r;
if (pom.equals(top)) {
r = result;
} else {
r = new DefaultModelBuilderResult(parent);
if (parent != null) {
parent.getChildren().add(r);
}
}
return r;
}

static <T> Set<T> concat(Set<T> a, T b) {
Set<T> result = new HashSet<>(a);
result.add(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ public MavenProject(MavenProject project) {
}

public File getParentFile() {
return parentFile;
// The projects may be built out of order, in which case the parent may exist
// but not be fully populated at the time this project was being setup.
// So if the parent exists, just delegate to it.
return getParent() != null ? getParent().getFile() : parentFile;
}

public void setParentFile(File parentFile) {
Expand Down

0 comments on commit df76dfc

Please sign in to comment.