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

Restrict migration to safe-logging to source sets that have the library #981

Merged
merged 6 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-981.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: Stop migrating source sets to safe-logging, unless they already have
the requisite libraries (`com.palantir.safe-logging:{preconditions,safe-logging}`).
links:
- https://github.com/palantir/gradle-baseline/pull/981
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.gradle.api.provider.ListProperty;

public class BaselineErrorProneExtension {

private static final ImmutableList<String> DEFAULT_PATCH_CHECKS = ImmutableList.of(
// Baseline checks
"ExecutorSubmitRunnableFutureIgnored",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import net.ltgt.gradle.errorprone.CheckSeverity;
Expand Down Expand Up @@ -242,6 +243,9 @@ private static Stream<String> getNotDisabledErrorproneChecks(
BaselineErrorProneExtension errorProneExtension,
JavaCompile javaCompile,
ErrorProneOptions errorProneOptions) {

Predicate<String> filterOutPreconditions = filterOutPreconditions(javaCompile.getClasspath());

return errorProneExtension.getPatchChecks().get().stream().filter(check -> {
if (checkExplicitlyDisabled(errorProneOptions, check)) {
log.info(
Expand All @@ -250,10 +254,37 @@ private static Stream<String> getNotDisabledErrorproneChecks(
check);
return false;
}
return true;
return filterOutPreconditions.test(check);
});
}

/** Filters out preconditions checks if the required libraries are not on the classpath. */
public static Predicate<String> filterOutPreconditions(FileCollection compileClasspath) {
boolean hasSafeLogging = !compileClasspath.filter(file -> file.getName().startsWith("safe-logging-")).isEmpty();
dansanduleac marked this conversation as resolved.
Show resolved Hide resolved
boolean hasPreconditions =
// The real 'preconditions' brings in 'safe-logging'. Because of inaccurate jar name checks, we
// use that fact to ensure we're not picking up another jar named 'preconditions' by mistake.
hasSafeLogging
&& !compileClasspath.filter(file -> file.getName().startsWith("preconditions-")).isEmpty();
return check -> {
if (!hasPreconditions && check.equals("PreferSafeLoggingPreconditions")) {
log.info(
"Disabling check PreferSafeLoggingPreconditions as 'com.palantir.safe-logging:safe-logging' "
+ "missing from source set for {}",
compileClasspath);
return false;
}
if (!hasSafeLogging && check.equals("PreferSafeLoggableExceptions")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

These live in the preconditions library, we only need the hasPreconditions check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah fair, but i'd still like to check for both since I don't trust trust just the check on a jar starting with preconditions-

dansanduleac marked this conversation as resolved.
Show resolved Hide resolved
log.info(
"Disabling check PreferSafeLoggableExceptions as 'com.palantir.safe-logging:preconditions' "
+ "missing from source set for {}",
compileClasspath);
return false;
}
return true;
};
}

private static boolean isRefactoring(Project project) {
return isRefasterRefactoring(project) || isErrorProneRefactoring(project);
}
Expand Down