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

Don't reuse BufStep args across invocations #1976

Merged
merged 8 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* New static method to `DiffMessageFormatter` which allows to retrieve diffs with their line numbers ([#1960](https://github.com/diffplug/spotless/issues/1960))
### Fixed
* Fix a regression in BufStep where the same arguments were being provided to every `buf` invocation. ([#1976](https://github.com/diffplug/spotless/issues/1976))
### Changes
* Use palantir-java-format 2.39.0 on Java 21. ([#1948](https://github.com/diffplug/spotless/pull/1948))

Expand Down
22 changes: 10 additions & 12 deletions lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -64,34 +63,33 @@ public FormatterStep create() {

private State createState() {
String instructions = "https://docs.buf.build/installation";
ForeignExe exeAbsPath = ForeignExe.nameAndVersion("buf", version)
ForeignExe exe = ForeignExe.nameAndVersion("buf", version)
.pathToExe(pathToExe)
.versionRegex(Pattern.compile("(\\S*)"))
.fixCantFind("Try following the instructions at " + instructions + ", or else tell Spotless where it is with {@code buf().pathToExe('path/to/executable')}");
return new State(this, exeAbsPath);
return new State(this, exe);
}

@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
static class State implements Serializable {
private static final long serialVersionUID = -1825662356883926318L;
// used for up-to-date checks and caching
final String version;
final transient ForeignExe exe;

// used for executing
private transient @Nullable List<String> args;
private final transient ForeignExe exe;
private transient String exeAbsPath;

State(BufStep step, ForeignExe exeAbsPath) {
State(BufStep step, ForeignExe exe) {
this.version = step.version;
this.exe = Objects.requireNonNull(exeAbsPath);
this.exe = Objects.requireNonNull(exe);
}

String format(ProcessRunner runner, String input, File file) throws IOException, InterruptedException {
if (args == null) {
args = Arrays.asList(
exe.confirmVersionAndGetAbsolutePath(),
"format",
file.getAbsolutePath());
if (exeAbsPath == null) {
exeAbsPath = exe.confirmVersionAndGetAbsolutePath();
}
List<String> args = List.of(exeAbsPath, "format", file.getAbsolutePath());
Copy link
Member

Choose a reason for hiding this comment

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

What about just args.set(2, file.getAbsolutePath()? I think exe.confirmVersionAndGetAbsolutePath(); is fairly expensive, and I don't think we need to call it for every invocation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I refactored to only call confirmVersionAndGetAbsolutePath once - did you see that?

Copy link
Member

Choose a reason for hiding this comment

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

Good call, looks nice. Sorry for slow turnaround.

return runner.exec(input.getBytes(StandardCharsets.UTF_8), args).assertExitZero(StandardCharsets.UTF_8);
}

Expand Down
Loading