Skip to content

Commit

Permalink
Don't throw exceptions if dependencies can't be pre-calculated (#475)
Browse files Browse the repository at this point in the history
We observed this issue:

```
INFO - 4abaf6cf-c5e6-4f48-b198-edc124aedf71 - Problem scanning file /tmp/codemodder-project6927415874030968841/app/src/main/java/org/apache/roller/weblogger/util/PasswordUtility.java
INFO - 4abaf6cf-c5e6-4f48-b198-edc124aedf71 - io.codemodder.plugins.maven.MavenProvider$DependencyUpdateException: Failure when retrieving dependencies
INFO - 4abaf6cf-c5e6-4f48-b198-edc124aedf71 - at io.codemodder.plugins.maven.MavenProvider.getAllDependencies(MavenProvider.java:146)
INFO - 4abaf6cf-c5e6-4f48-b198-edc124aedf71 - at io.codemodder.DefaultCodemodExecutor.lambda$execute$0(DefaultCodemodExecutor.java:187)
INFO - 4abaf6cf-c5e6-4f48-b198-edc124aedf71 - at java.base/java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:273)
INFO - 4abaf6cf-c5e6-4f48-b198-edc124aedf71 - at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
INFO - 4abaf6cf-c5e6-4f48-b198-edc124aedf71 - at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
...
io.codemodder.DefaultCodemodExecutor.lambda$execute$1(DefaultCodemodExecutor.java:188)
edc124aedf71 - Caused by: org.dom4j.DocumentException: Error on line 781 of document  : The element type "sequential" must be terminated by the matching end-tag "</sequential>".
```

This error is preventing _analysis_ from occurring, not the updating of
Maven dependencies. We should still run our codemods, even if
dependencies can't be pre-calculated.
  • Loading branch information
nahsra authored Nov 23, 2024
1 parent 3b5d693 commit e261a7f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.VisibleForTesting;
import org.slf4j.Logger;
Expand Down Expand Up @@ -184,7 +185,14 @@ public CodeTFResult execute(final List<Path> filePaths) {
Collection<DependencyGAV> deps =
projectProviders.stream()
.flatMap(
provider -> provider.getAllDependencies(projectDir, filePath).stream())
provider -> {
try {
return provider.getAllDependencies(projectDir, filePath).stream();
} catch (Exception e) {
log.error("Problem getting dependencies for file {}", filePath, e);
return Stream.empty();
}
})
.toList();

CodemodInvocationContext context =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public DependencyUpdateResult updateDependencies(
}

@Override
public Collection<DependencyGAV> getAllDependencies(Path projectDir, Path file) {
public Collection<DependencyGAV> getAllDependencies(final Path projectDir, final Path file) {
return List.of();
}
}
Expand All @@ -158,7 +158,41 @@ void it_generates_single_codemod_codetf() {

// should have just 1 entry because we only scanned javaFile1
List<CodeTFChangesetEntry> changeset = result.getChangeset();
assertThat(changeset.size()).isEqualTo(1);
assertThat(changeset).hasSize(1);
assertThat(changeset.get(0)).satisfies(DefaultCodemodExecutorTest::isJavaFile1ChangedCorrectly);
}

@Test
void it_works_despite_fail_prefetching_all_deps() {
FakeDepsProvider depsProvider =
new FakeDepsProvider() {
@Override
public Collection<DependencyGAV> getAllDependencies(
final Path projectDir, final Path file) {
throw new RuntimeException("failed to prefetch deps");
}
};

executor =
new DefaultCodemodExecutor(
repoDir,
includesEverything,
beforeAfterCodemod,
List.of(depsProvider),
List.of(),
fileCache,
javaParserFacade,
encodingDetector,
-1,
-1,
-1);

CodeTFResult result = executor.execute(List.of(javaFile1));
assertThat(result).satisfies(DefaultCodemodExecutorTest::hasBeforeAfterCodemodMetadata);

// should have just 1 entry because we only scanned javaFile1
List<CodeTFChangesetEntry> changeset = result.getChangeset();
assertThat(changeset).hasSize(1);
assertThat(changeset.get(0)).satisfies(DefaultCodemodExecutorTest::isJavaFile1ChangedCorrectly);
}

Expand Down Expand Up @@ -241,7 +275,7 @@ public CodeTFChange onChangeCreated(

// confirm the change was updated by the provider
List<CodeTFChangesetEntry> changeset = result.getChangeset();
assertThat(changeset.size()).isEqualTo(1);
assertThat(changeset).hasSize(1);
CodeTFChangesetEntry entry = changeset.get(0);
assertThat(entry.getChanges().get(0).getDescription()).isEqualTo("hi " + javaFile1.toString());
assertThat(entry.getChanges().get(0).getProperties()).hasSize(1);
Expand All @@ -256,7 +290,7 @@ void it_generates_all_files_codemod_codetf() {

// should have 2 entries for both javaFile1 and javaFile3
List<CodeTFChangesetEntry> changeset = result.getChangeset();
assertThat(changeset.size()).isEqualTo(2);
assertThat(changeset).hasSize(2);
assertThat(changeset.get(0)).satisfies(DefaultCodemodExecutorTest::isJavaFile1ChangedCorrectly);
assertThat(changeset.get(1)).satisfies(DefaultCodemodExecutorTest::isJavaFile3ChangedCorrectly);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ public Collection<DependencyGAV> getAllDependencies(final Path projectDir, final

return pomOperator.getAllFoundDependencies();
} catch (Exception e) {
throw new DependencyUpdateException("Failure when retrieving dependencies", e);
LOG.warn("Not all Maven dependencies could be found", e);
}
return Collections.emptyList();
}

private static final Logger LOG = LoggerFactory.getLogger(MavenProvider.class);
Expand Down

0 comments on commit e261a7f

Please sign in to comment.