Skip to content

Commit dc3b568

Browse files
committed
fix: don't interact with the file directly
1 parent 1025821 commit dc3b568

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.diffplug.spotless.FormatterFunc;
3737
import com.diffplug.spotless.FormatterStep;
3838
import com.diffplug.spotless.ProcessRunner;
39+
import com.diffplug.spotless.ThrowingEx;
3940

4041
public final class IdeaStep {
4142

@@ -120,15 +121,21 @@ private String pathToExe() {
120121

121122
@Override
122123
public String applyWithFile(String unix, File file) throws Exception {
123-
List<String> params = getParams(file);
124-
125-
try (ProcessRunner runner = new ProcessRunner()) {
126-
var result = runner.exec(params);
127-
128-
LOGGER.debug("command finished with stdout: {}",
129-
result.assertExitZero(StandardCharsets.UTF_8));
130-
131-
return Files.readString(file.toPath());
124+
// since we cannot directly work with the file, we need to write the unix string to a temporary file
125+
File tempFile = File.createTempFile("spotless", file.getName());
126+
try {
127+
Files.write(tempFile.toPath(), unix.getBytes(StandardCharsets.UTF_8));
128+
List<String> params = getParams(tempFile);
129+
130+
try (ProcessRunner runner = new ProcessRunner()) {
131+
var result = runner.exec(params);
132+
LOGGER.debug("command finished with stdout: {}",
133+
result.assertExitZero(StandardCharsets.UTF_8));
134+
135+
return Files.readString(tempFile.toPath(), StandardCharsets.UTF_8);
136+
}
137+
} finally {
138+
Files.delete(tempFile.toPath());
132139
}
133140
}
134141

@@ -143,7 +150,7 @@ private List<String> getParams(File file) {
143150
builder.add("-s");
144151
builder.add(codeStyleSettingsPath);
145152
}
146-
builder.add(file.toString());
153+
builder.add(ThrowingEx.get(file::getCanonicalPath));
147154
return builder.build().collect(Collectors.toList());
148155
}
149156
}

0 commit comments

Comments
 (0)