Skip to content

Commit 062bc9a

Browse files
authored
Set working directory for the native image process (palantir#1273)
Set working directory for the native image process
1 parent 2feda5a commit 062bc9a

5 files changed

Lines changed: 32 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type: fix
2+
fix:
3+
description: Set working directory for the native image process
4+
links:
5+
- https://github.com/palantir/palantir-java-format/pull/1273

‎idea-plugin/src/main/java/com/palantir/javaformat/intellij/PalantirJavaFormatFormattingService.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ public void run() {
114114
String formattedText = applyReplacements(request.getDocumentText(), replacements);
115115
request.onTextReady(formattedText);
116116
} catch (FormatterException e) {
117+
logger.error(
118+
String.format(
119+
"Failed to format file %s",
120+
request.getContext().getContainingFile().getName()),
121+
e);
117122
request.onError(
118123
Notifications.PARSING_ERROR_TITLE,
119124
Notifications.parsingErrorMessage(

‎palantir-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/BootstrappingFormatterService.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ private ImmutableList<Replacement> getFormatReplacementsInternal(String input, C
8787
.characterRanges(ranges.stream().map(RangeUtils::toStringRange).collect(Collectors.toList()))
8888
.build();
8989

90-
Optional<String> output = FormatterCommandRunner.runWithStdin(command.toArgs(), input);
90+
Optional<String> output =
91+
FormatterCommandRunner.runWithStdin(command.toArgs(), input, Optional.of(jdkPath.getParent()));
9192
if (output.isEmpty() || output.get().isEmpty()) {
9293
return ImmutableList.of();
9394
}
@@ -101,7 +102,8 @@ private String runFormatterCommand(String input) throws IOException {
101102
.implementationClasspath(implementationClassPath)
102103
.outputReplacements(false)
103104
.build();
104-
return FormatterCommandRunner.runWithStdin(command.toArgs(), input).orElse(input);
105+
return FormatterCommandRunner.runWithStdin(command.toArgs(), input, Optional.ofNullable(jdkPath.getParent()))
106+
.orElse(input);
105107
}
106108

107109
@Value.Immutable

‎palantir-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/FormatterCommandRunner.java‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,21 @@
2020
import java.io.InputStream;
2121
import java.io.OutputStream;
2222
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Path;
2324
import java.util.List;
2425
import java.util.Optional;
2526
import java.util.regex.Pattern;
2627

2728
final class FormatterCommandRunner {
2829
private static final Pattern SYNTAX_ERROR_PATTERN = Pattern.compile(":\\d+:\\d+:\\serror:\\s");
2930

30-
static Optional<String> runWithStdin(List<String> command, String input) throws IOException {
31-
Process process = new ProcessBuilder().command(command).start();
31+
static Optional<String> runWithStdin(List<String> command, String input, Optional<Path> workingDirectory)
32+
throws IOException {
33+
ProcessBuilder processBuilder = new ProcessBuilder().command(command);
34+
Process process = workingDirectory
35+
.map(dir -> processBuilder.directory(dir.toFile()))
36+
.orElse(processBuilder)
37+
.start();
3238

3339
try (OutputStream outputStream = process.getOutputStream()) {
3440
outputStream.write(input.getBytes(StandardCharsets.UTF_8));
@@ -50,7 +56,7 @@ static Optional<String> runWithStdin(List<String> command, String input) throws
5056
// In this case, we just want to silently do nothing and not surface an error to e.g. Intellij.
5157
return Optional.empty();
5258
}
53-
throw new IOException(getErrorMessage(command, stdout, stderr));
59+
throw new IOException(getErrorMessage(command, workingDirectory, stdout, stderr));
5460
}
5561

5662
return Optional.of(stdout);
@@ -70,11 +76,14 @@ private static String readToString(InputStream input) throws IOException {
7076
}
7177
}
7278

73-
private static String getErrorMessage(List<String> command, String stdout, String stderr) {
79+
private static String getErrorMessage(
80+
List<String> command, Optional<Path> workingDirectory, String stdout, String stderr) {
7481
return String.join(
7582
"\n",
7683
"Command terminated with exit value 1",
7784
"Command: " + String.join(" ", command),
85+
"Working Directory: "
86+
+ workingDirectory.map(Path::toString).orElse("<using default working directory>"),
7887
"Stdout:",
7988
stdout,
8089
"Stderr:",

‎palantir-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/NativeImageFormatterService.java‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public ImmutableList<Replacement> getFormatReplacements(String input, Collection
5252
ranges.stream().map(RangeUtils::toStringRange).collect(Collectors.toList()))
5353
.build();
5454

55-
Optional<String> output = FormatterCommandRunner.runWithStdin(command.toArgs(), input);
55+
Optional<String> output = FormatterCommandRunner.runWithStdin(
56+
command.toArgs(), input, Optional.ofNullable(nativeImagePath.getParent()));
5657
if (output.isEmpty() || output.get().isEmpty()) {
5758
return ImmutableList.of();
5859
}
@@ -85,7 +86,9 @@ private String runFormatterCommand(String input) throws IOException {
8586
.nativeImagePath(nativeImagePath)
8687
.outputReplacements(false)
8788
.build();
88-
return FormatterCommandRunner.runWithStdin(command.toArgs(), input).orElse(input);
89+
return FormatterCommandRunner.runWithStdin(
90+
command.toArgs(), input, Optional.ofNullable(nativeImagePath.getParent()))
91+
.orElse(input);
8992
}
9093

9194
@Value.Immutable

0 commit comments

Comments
 (0)