Skip to content

Commit da53172

Browse files
EdgarTwiggEdgarTwigg
authored andcommitted
Make sure that lint suppression path-filtering is always done using unix paths.
1 parent aedb7c0 commit da53172

File tree

5 files changed

+41
-49
lines changed

5 files changed

+41
-49
lines changed

lib/src/main/java/com/diffplug/spotless/LintSuppression.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 DiffPlug
2+
* Copyright 2024-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,8 +15,11 @@
1515
*/
1616
package com.diffplug.spotless;
1717

18+
import java.io.File;
1819
import java.util.Objects;
1920

21+
import javax.annotation.Nullable;
22+
2023
public class LintSuppression implements java.io.Serializable {
2124
private static final long serialVersionUID = 1L;
2225

@@ -30,6 +33,9 @@ public String getPath() {
3033
}
3134

3235
public void setPath(String path) {
36+
if (path.indexOf('\\') != -1) {
37+
throw new IllegalArgumentException("Path must use only unix style path separator `/`, this was " + path);
38+
}
3339
this.path = Objects.requireNonNull(path);
3440
}
3541

@@ -90,4 +96,20 @@ public String toString() {
9096
", code='" + shortCode + '\'' +
9197
'}';
9298
}
99+
100+
/**
101+
* Returns the relative path between root and dest, or null if dest is not a
102+
* child of root. Guaranteed to only have unix-separators.
103+
*/
104+
public static @Nullable String relativizeAsUnix(File root, File dest) {
105+
String rootPath = root.getAbsolutePath();
106+
String destPath = dest.getAbsolutePath();
107+
if (!destPath.startsWith(rootPath)) {
108+
return null;
109+
} else {
110+
String relativized = destPath.substring(rootPath.length());
111+
String unixified = relativized.replace('\\', '/');
112+
return unixified.startsWith("/") ? unixified.substring(1) : unixified;
113+
}
114+
}
93115
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -347,27 +347,12 @@ private final FileCollection parseTargetIsExclude(Object target, boolean isExclu
347347
}
348348

349349
private static void relativizeIfSubdir(List<String> relativePaths, File root, File dest) {
350-
String relativized = relativize(root, dest);
350+
String relativized = LintSuppression.relativizeAsUnix(root, dest);
351351
if (relativized != null) {
352352
relativePaths.add(relativized);
353353
}
354354
}
355355

356-
/**
357-
* Returns the relative path between root and dest, or null if dest is not a
358-
* child of root.
359-
*/
360-
static @Nullable String relativize(File root, File dest) {
361-
String rootPath = root.getAbsolutePath();
362-
String destPath = dest.getAbsolutePath();
363-
if (!destPath.startsWith(rootPath)) {
364-
return null;
365-
} else {
366-
String relativized = destPath.substring(rootPath.length());
367-
return relativized.startsWith("/") || relativized.startsWith("\\") ? relativized.substring(1) : relativized;
368-
}
369-
}
370-
371356
/** The steps that need to be added. */
372357
protected final List<FormatterStep> steps = new ArrayList<>();
373358

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.diffplug.spotless.Formatter;
5555
import com.diffplug.spotless.Jvm;
5656
import com.diffplug.spotless.LineEnding;
57+
import com.diffplug.spotless.LintState;
5758
import com.diffplug.spotless.LintSuppression;
5859
import com.diffplug.spotless.Provisioner;
5960
import com.diffplug.spotless.generic.LicenseHeaderStep;
@@ -228,7 +229,16 @@ protected List<LintSuppression> getLintSuppressions() {
228229
return lintSuppressions;
229230
}
230231

231-
protected abstract void process(String name, Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker, List<LintSuppression> lintSuppressions) throws MojoExecutionException;
232+
protected abstract void process(String name, Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException;
233+
234+
protected LintState calculateLintState(Formatter formatter, File file) throws IOException {
235+
String relativePath = LintSuppression.relativizeAsUnix(baseDir, file);
236+
if (relativePath == null) {
237+
// File is not within baseDir, use absolute path as fallback
238+
relativePath = file.getAbsolutePath();
239+
}
240+
return LintState.of(formatter, file).withRemovedSuppressions(formatter, relativePath, lintSuppressions);
241+
}
232242

233243
private static final int MINIMUM_JRE = 11;
234244

@@ -261,7 +271,7 @@ public final void execute() throws MojoExecutionException {
261271
for (FormatterFactory factory : formattersHolder.openFormatters.keySet()) {
262272
Formatter formatter = formattersHolder.openFormatters.get(factory);
263273
Iterable<File> files = formattersHolder.factoryToFiles.get(factory).get();
264-
process(formattersHolder.nameFor(factory), files, formatter, upToDateChecker, getLintSuppressions());
274+
process(formattersHolder.nameFor(factory), files, formatter, upToDateChecker);
265275
}
266276
} catch (PluginException e) {
267277
throw e.asMojoExecutionException();
@@ -424,19 +434,4 @@ private UpToDateChecker createUpToDateChecker(Iterable<Formatter> formatters) {
424434
}
425435
return UpToDateChecker.wrapWithBuildContext(checker, buildContext);
426436
}
427-
428-
/**
429-
* Returns the relative path between root and dest, or null if dest is not a
430-
* child of root.
431-
*/
432-
static String relativize(File root, File dest) {
433-
String rootPath = root.getAbsolutePath();
434-
String destPath = dest.getAbsolutePath();
435-
if (!destPath.startsWith(rootPath)) {
436-
return null;
437-
} else {
438-
String relativized = destPath.substring(rootPath.length());
439-
return relativized.startsWith("/") || relativized.startsWith("\\") ? relativized.substring(1) : relativized;
440-
}
441-
}
442437
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class SpotlessApplyMojo extends AbstractSpotlessMojo {
4545
private boolean spotlessIdeHookUseStdOut;
4646

4747
@Override
48-
protected void process(String name, Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker, List<LintSuppression> lintSuppressions) throws MojoExecutionException {
48+
protected void process(String name, Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
4949
if (isIdeHook()) {
5050
IdeHook.performHook(files, formatter, spotlessIdeHook, spotlessIdeHookUseStdIn, spotlessIdeHookUseStdOut);
5151
return;
@@ -63,12 +63,7 @@ protected void process(String name, Iterable<File> files, Formatter formatter, U
6363
}
6464

6565
try {
66-
String relativePath = relativize(baseDir, file);
67-
if (relativePath == null) {
68-
// File is not within baseDir, use absolute path as fallback
69-
relativePath = file.getAbsolutePath();
70-
}
71-
LintState lintState = LintState.of(formatter, file).withRemovedSuppressions(formatter, relativePath, lintSuppressions);
66+
LintState lintState = super.calculateLintState(formatter, file);
7267
boolean hasDirtyState = !lintState.getDirtyState().isClean() && !lintState.getDirtyState().didNotConverge();
7368
boolean hasUnsuppressedLints = lintState.isHasLints();
7469

@@ -93,6 +88,7 @@ protected void process(String name, Iterable<File> files, Formatter formatter, U
9388
for (Map.Entry<String, List<com.diffplug.spotless.Lint>> stepEntry : lintState.getLintsByStep(formatter).entrySet()) {
9489
String stepName = stepEntry.getKey();
9590
for (com.diffplug.spotless.Lint lint : stepEntry.getValue()) {
91+
String relativePath = LintSuppression.relativizeAsUnix(baseDir, file);
9692
message.append("\n ").append(relativePath).append(":");
9793
lint.addWarningMessageTo(message, stepName, true);
9894
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessCheckMojo.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import com.diffplug.spotless.Formatter;
3131
import com.diffplug.spotless.LintState;
32-
import com.diffplug.spotless.LintSuppression;
3332
import com.diffplug.spotless.extra.integration.DiffMessageFormatter;
3433
import com.diffplug.spotless.maven.incremental.UpToDateChecker;
3534

@@ -65,7 +64,7 @@ public int getSeverity() {
6564
private MessageSeverity m2eIncrementalBuildMessageSeverity;
6665

6766
@Override
68-
protected void process(String name, Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker, List<LintSuppression> lintSuppressions) throws MojoExecutionException {
67+
protected void process(String name, Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
6968
ImpactedFilesTracker counter = new ImpactedFilesTracker();
7069

7170
List<File> problemFiles = new ArrayList<>();
@@ -80,12 +79,7 @@ protected void process(String name, Iterable<File> files, Formatter formatter, U
8079
}
8180
buildContext.removeMessages(file);
8281
try {
83-
String relativePath = relativize(baseDir, file);
84-
if (relativePath == null) {
85-
// File is not within baseDir, use absolute path as fallback
86-
relativePath = file.getAbsolutePath();
87-
}
88-
LintState lintState = LintState.of(formatter, file).withRemovedSuppressions(formatter, relativePath, lintSuppressions);
82+
LintState lintState = super.calculateLintState(formatter, file);
8983
boolean hasDirtyState = !lintState.getDirtyState().isClean() && !lintState.getDirtyState().didNotConverge();
9084
boolean hasUnsuppressedLints = lintState.isHasLints();
9185

0 commit comments

Comments
 (0)