Skip to content

Commit

Permalink
Integrates with gradle-failure-reports. Adds ExceptionWithSuggestion. (
Browse files Browse the repository at this point in the history
…#2724)

Integrates with gradle-failure-reports. Adds ExceptionWithSuggestion.
  • Loading branch information
crogoz authored Feb 26, 2024
1 parent be53d92 commit d82bd00
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 42 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2724.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Integrates with gradle-failure-reports. Adds ExceptionWithSuggestion.
links:
- https://github.com/palantir/gradle-baseline/pull/2724
1 change: 1 addition & 0 deletions gradle-baseline-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation 'org.ow2.asm:asm'
implementation 'com.googlecode.java-diff-utils:diffutils'
implementation 'com.palantir.gradle.utils:lazily-configured-mapping'
implementation 'com.palantir.gradle.failure-reports:gradle-failure-reports-exceptions'

runtimeOnly 'com.palantir.javaformat:gradle-palantir-java-format'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedMap;
import com.palantir.baseline.services.JarClassHasher;
import com.palantir.gradle.failurereports.exceptions.ExceptionWithSuggestion;
import difflib.DiffUtils;
import difflib.Patch;
import java.io.File;
Expand Down Expand Up @@ -160,34 +161,36 @@ private void ensureLockfileContains(String expected) {
}

if (!lockFile.exists()) {
throw new GradleException("baseline-class-uniqueness detected multiple jars containing identically named "
+ "classes. Please resolve these problems, or run `./gradlew checkClassUniqueness "
+ "--write-locks` to accept them:\n\n"
+ expected);
throw new ExceptionWithSuggestion(
"baseline-class-uniqueness detected multiple jars containing identically named classes."
+ " Please resolve these problems, or run `./gradlew checkClassUniqueness --write-locks`"
+ "to accept them:\n\n " + expected,
"./gradlew checkClassUniqueness --write-locks");
}

String onDisk = GFileUtils.readFile(lockFile);
if (!onDisk.equals(expected)) {
List<String> onDiskLines = Splitter.on('\n').splitToList(onDisk);
Patch<String> diff = DiffUtils.diff(onDiskLines, Splitter.on('\n').splitToList(expected));

throw new GradleException(String.join(
"\n",
String.format(
"%s is out of date, please run `./gradlew checkClassUniqueness --write-locks` "
+ "to update this file. The diff is:",
lockFile),
"",
throw new ExceptionWithSuggestion(
String.join(
"\n",
DiffUtils.generateUnifiedDiff("on disk", "expected", onDiskLines, diff, Integer.MAX_VALUE)),
"",
"On disk was:",
"",
onDisk,
"",
"Expected was:",
expected));
String.format(
"%s is out of date, please run ` " + "to update this file. The diff is:", lockFile),
"",
String.join(
"\n",
DiffUtils.generateUnifiedDiff(
"on disk", "expected", onDiskLines, diff, Integer.MAX_VALUE)),
"",
"On disk was:",
"",
onDisk,
"",
"Expected was:",
expected),
"./gradlew checkClassUniqueness --write-locks");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.palantir.baseline.tasks;

import com.palantir.baseline.plugins.javaversions.BaselineJavaVersion;
import com.palantir.gradle.failurereports.exceptions.ExceptionWithSuggestion;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
Expand All @@ -25,7 +26,6 @@
import java.util.Collections;
import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.JavaVersion;
import org.gradle.api.Task;
import org.gradle.api.model.ObjectFactory;
Expand Down Expand Up @@ -130,20 +130,18 @@ public final void taskAction() throws IOException {
StandardOpenOption.CREATE);
return;
}

throw new GradleException(String.format(
"%s must set sourceCompatibility explicitly in '%s', "
+ "otherwise compilation will not be reproducible but instead depends on the Java version "
+ "that Gradle is currently running with (%s). To auto-fix, run%n"
+ "%n"
+ " ./gradlew %s --fix%n"
+ "%n"
+ "This will automatically add a suggested line "
+ "(you may need to adjust the number, e.g. to '1.8' for maximum compatibility).",
getProject(),
getProject().getRootProject().relativePath(getProject().getBuildFile()),
JavaVersion.current(),
getPath()));
String suggestion = String.format("./gradlew %s --fix", getPath());
throw new ExceptionWithSuggestion(
String.format(
"%s must set sourceCompatibility explicitly in '%s', otherwise compilation will not be"
+ " reproducible but instead depends on the Java version that Gradle is currently running"
+ " with (%s). To auto-fix, run%n%n %s%n%nThis will automatically add a suggested line"
+ " (you may need to adjust the number, e.g. to '1.8' for maximum compatibility).",
getProject(),
getProject().getRootProject().relativePath(getProject().getBuildFile()),
JavaVersion.current(),
suggestion),
suggestion);
}

private JavaVersion getRawSourceCompat() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.collect.Streams;
import com.palantir.baseline.plugins.BaselineExactDependencies;
import com.palantir.gradle.failurereports.exceptions.ExceptionWithSuggestion;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -26,7 +27,6 @@
import java.util.Set;
import java.util.stream.Collectors;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.artifacts.ResolvedDependency;
Expand Down Expand Up @@ -90,11 +90,12 @@ public final void checkImplicitDependencies() {
.map(artifact -> getSuggestionString(artifact))
.sorted()
.collect(Collectors.joining("\n", " dependencies {\n", "\n }"));

throw new GradleException(String.format(
"Found %d implicit dependencies - consider adding the following explicit "
+ "dependencies to '%s', or avoid using classes from these jars:\n%s",
usedButUndeclared.size(), buildFile(), suggestion));
throw new ExceptionWithSuggestion(
String.format(
"Found %d implicit dependencies - consider adding the following explicit "
+ "dependencies to '%s', or avoid using classes from these jars:\n%s",
usedButUndeclared.size(), buildFile(), suggestion),
buildFile().toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import com.palantir.baseline.plugins.BaselineExactDependencies;
import com.palantir.gradle.failurereports.exceptions.ExceptionWithSuggestion;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -28,7 +29,6 @@
import java.util.Set;
import java.util.stream.Collectors;
import org.gradle.api.DefaultTask;
import org.gradle.api.GradleException;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.artifacts.ResolvedDependency;
Expand Down Expand Up @@ -130,7 +130,7 @@ public final void checkUnusedDependencies() {
.append("\n"));
}
}
throw new GradleException(builder.toString());
throw new ExceptionWithSuggestion(builder.toString(), buildFile().toString());
}
}

Expand Down
1 change: 1 addition & 0 deletions versions.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ com.google.protobuf:protobuf-java:3.19.6 (1 constraints: 301169c9)
com.googlecode.concurrent-trees:concurrent-trees:2.6.1 (1 constraints: 761166da)
com.googlecode.java-diff-utils:diffutils:1.3.0 (1 constraints: 0605f935)
com.googlecode.javaewah:JavaEWAH:1.1.12 (1 constraints: 750eee5e)
com.palantir.gradle.failure-reports:gradle-failure-reports-exceptions:1.2.0 (1 constraints: 0505f635)
com.palantir.gradle.utils:lazily-configured-mapping:0.1.0 (1 constraints: 0305ee35)
com.palantir.javaformat:gradle-palantir-java-format:1.1.0 (1 constraints: 0405f335)
com.palantir.javaformat:palantir-java-format-spi:1.1.0 (1 constraints: 711560be)
Expand Down
1 change: 1 addition & 0 deletions versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ com.googlecode.java-diff-utils:diffutils = 1.3.0
com.puppycrawl.tools:checkstyle = 10.12.5
com.palantir.gradle.utils:* = 0.1.0
com.uber.nullaway:nullaway = 0.10.19
com.palantir.gradle.failure-reports:* = 1.2.0

# test deps
com.fasterxml.jackson.*:* = 2.15.3
Expand Down

0 comments on commit d82bd00

Please sign in to comment.