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

Adding guard when annotating linkage errors #2196

Merged
merged 4 commits into from
Aug 20, 2021
Merged

Conversation

suztomo
Copy link
Contributor

@suztomo suztomo commented Aug 20, 2021

A user reported that pom-packaging artifact is not working correctly.

@google-cla google-cla bot added the cla: yes label Aug 20, 2021
@suztomo suztomo requested a review from a team August 20, 2021 19:37
linkageProblem.setCause(UnknownCause.getInstance());
} else {
Artifact artifactInSubtree = entryInSubtree.getArtifact();
DependencyPath pathToSourceEntry = rootResult.getDependencyPaths(sourceEntry).get(0);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A user reported that this threw ArrayIndexOutOfBoundsException.

// Different version of that artifact is selected in rootResult
ImmutableList<DependencyPath> pathToSelectedArtifact =
rootResult.getDependencyPaths(selectedEntry);
if (pathToSelectedArtifact.isEmpty()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another guard to avoid ArrayIndexOutOfBoundsException

Artifact artifactInSubtree = entryInSubtree.getArtifact();
ImmutableList<DependencyPath> dependencyPathsToSource =
rootResult.getDependencyPaths(sourceEntry);
if (dependencyPathsToSource.isEmpty()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main purpose of this PR.

}

// For artifacts with classifiers, there can be multiple resolved artifacts for one node
for (ResolvedArtifact artifact : moduleArtifacts) {
// parentPath is null for the first item
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the first item's parentPath is actually set line 352. It's never null.

@suztomo suztomo requested review from dzou and removed request for a team August 20, 2021 19:52
ClassFile sourceClass = linkageProblem.getSourceClass();
ClassPathEntry sourceEntry = sourceClass.getClassPathEntry();
// Annotating linkage errors is a nice-to-have feature for Linkage Checker plugins. Let's not
// fail the entire process when there are problems, such as classPathBuilder unable to resolve
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// fail the entire process when there are problems, such as classPathBuilder unable to resolve
// fail the entire process if there are problems, such as classPathBuilder unable to resolve

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Symbol symbol = linkageProblem.getSymbol();
ClassPathEntry entryInSubtree = subtreeResult.findEntryBySymbol(symbol);
if (entryInSubtree == null) {
linkageProblem.setCause(UnknownCause.getInstance());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be more clear to early return and exit the method if entryInSubtree == null and then unindent everything in the else-block. (Fewer levels of indentation).

if (entryInSubtree == null) {
  linkageProblem.setCause(UnknownCause.getInstance());
  return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea. Updated.

artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId());
if (selectedEntry != null) {
Artifact selectedArtifact = selectedEntry.getArtifact();
if (!selectedArtifact.getVersion().equals(artifactInSubtree.getVersion())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably combine the guard clause you created here with the other error condition:

ClassPathEntry selectedEntry =
    rootResult.findEntryById(
        artifactInSubtree.getGroupId(), artifactInSubtree.getArtifactId());

if (selectedEntry != null) {
  Artifact selectedArtifact = selectedEntry.getArtifact();
  ImmutableList<DependencyPath> pathToSelectedArtifact =
      rootResult.getDependencyPaths(selectedEntry);

  if (pathToSelectedArtifact.isEmpty() || !selectedArtifact.getVersion().equals(artifactInSubtree.getVersion()) {
    linkageProblem.setCause(UnknownCause.getInstance());
    return;
  }
  
  linkageProblem.setCause(
      new DependencyConflict(
          linkageProblem, pathToSelectedArtifact.get(0), pathToUnselectedEntry));
} else {
...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be logically the same but the current if-statement delivers better meaning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@suztomo suztomo merged commit 7b8376c into master Aug 20, 2021
@suztomo suztomo deleted the dependency_path_missing branch August 20, 2021 21:03
@suztomo suztomo restored the dependency_path_missing branch August 20, 2021 22:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants