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

Emit contextual error message during incremental m2e builds #1962

Merged
merged 6 commits into from
Dec 15, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.AbstractMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;

import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.Edit;
import org.eclipse.jgit.diff.EditList;
import org.eclipse.jgit.diff.MyersDiff;
import org.eclipse.jgit.diff.RawText;
Expand Down Expand Up @@ -234,6 +237,19 @@ private void addIntendedLine(String indent, String line) {
* sequence (\n, \r, \r\n).
*/
private String diff(File file) throws IOException {
return diff(formatter, file).getKey();
}

/**
* Returns a map entry with key being a git-style diff between the contents of the given file and what those contents would
* look like if formatted using the given formatter. Does not end with any newline
* sequence (\n, \r, \r\n). The value of the map entry is the line where the first difference occurred.
*/
public static Map.Entry<String, Integer> diff(Formatter formatter, File file) throws IOException {
return diff(new CleanProviderFormatter(formatter), file);
}

private static Map.Entry<String, Integer> diff(CleanProvider formatter, File file) throws IOException {
String raw = new String(Files.readAllBytes(file.toPath()), formatter.getEncoding());
String rawUnix = LineEnding.toUnix(raw);
String formatted = formatter.getFormatted(file, rawUnix);
Expand All @@ -248,13 +264,13 @@ private String diff(File file) throws IOException {
}

/**
* Returns a git-style diff between the two unix strings.
* Returns a map entry with key being a git-style diff between the two unix strings and value being the line of the first difference (in the dirty string)
nedtwigg marked this conversation as resolved.
Show resolved Hide resolved
* <p>
* Output has no trailing newlines.
* <p>
* Boolean args determine whether whitespace or line endings will be visible.
*/
private static String diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException {
private static Map.Entry<String, Integer> diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException {
dirty = visibleWhitespaceLineEndings(dirty, whitespace, lineEndings);
clean = visibleWhitespaceLineEndings(clean, whitespace, lineEndings);

Expand All @@ -271,7 +287,11 @@ private static String diffWhitespaceLineEndings(String dirty, String clean, bool

// we don't need the diff to show this, since we display newlines ourselves
formatted = formatted.replace("\\ No newline at end of file\n", "");
return NEWLINE_MATCHER.trimTrailingFrom(formatted);
return new AbstractMap.SimpleEntry<>(NEWLINE_MATCHER.trimTrailingFrom(formatted), getLineOfFirstDifference(edits));
nedtwigg marked this conversation as resolved.
Show resolved Hide resolved
}

private static int getLineOfFirstDifference(EditList edits) {
return edits.stream().mapToInt(Edit::getBeginA).min().getAsInt();
}

private static final CharMatcher NEWLINE_MATCHER = CharMatcher.is('\n');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.sonatype.plexus.build.incremental.BuildContext;

import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.PaddedCell;
Expand Down Expand Up @@ -54,13 +56,17 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
PaddedCell.DirtyState dirtyState = PaddedCell.calculateDirtyState(formatter, file);
if (!dirtyState.isClean() && !dirtyState.didNotConverge()) {
problemFiles.add(file);
if (buildContext.isIncremental()) {
Map.Entry<String, Integer> diffEntry = DiffMessageFormatter.diff(formatter, file);
buildContext.addMessage(file, diffEntry.getValue() + 1, 0, diffEntry.getKey(), BuildContext.SEVERITY_ERROR, null);
}
counter.cleaned();
} else {
counter.checkedButAlreadyClean();
upToDateChecker.setUpToDate(file.toPath());
}
} catch (IOException | RuntimeException e) {
throw new MojoExecutionException("Unable to format file " + file, e);
throw new MojoExecutionException("Unable to check file " + file, e);
}
}

Expand Down
Loading