Skip to content

Commit

Permalink
Fix: Append the log message to build output if it's from compilation …
Browse files Browse the repository at this point in the history
…report. (microsoft#1490)

- Store the id of the compilation task in an LRU cache.
- Append the log message to the build output if it's from the compilation task.

Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo authored and Jiaaming committed Jul 15, 2024
1 parent 5230c73 commit 8771f08
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -32,6 +33,7 @@
import ch.epfl.scala.bsp4j.MessageType;
import ch.epfl.scala.bsp4j.PublishDiagnosticsParams;
import ch.epfl.scala.bsp4j.ShowMessageParams;
import ch.epfl.scala.bsp4j.StatusCode;
import ch.epfl.scala.bsp4j.TaskDataKind;
import ch.epfl.scala.bsp4j.TaskFinishParams;
import ch.epfl.scala.bsp4j.TaskProgressParams;
Expand Down Expand Up @@ -60,6 +62,8 @@ public class GradleBuildClient implements BuildClient {

private final JavaLanguageClient lsClient;

private final LruCache<String> failedTaskCache = new LruCache<>(16);

public GradleBuildClient() {
this.lsClient = JavaLanguageServerPlugin.getProjectsManager().getConnection();
}
Expand All @@ -70,8 +74,12 @@ public void onBuildLogMessage(LogMessageParams params) {
if (type == MessageType.LOG) {
Utils.sendTelemetry(this.lsClient, params.getMessage());
} else {
this.lsClient.sendNotification(new ExecuteCommandParams(CLIENT_BUILD_LOG_CMD,
Arrays.asList(params.getMessage())));
String command = CLIENT_BUILD_LOG_CMD;
if (type == MessageType.ERROR && failedTaskCache.contains(params.getTask().getId())) {
// append the compilation failure message to the build output channel.
command = CLIENT_APPEND_BUILD_LOG_CMD;
}
this.lsClient.sendNotification(new ExecuteCommandParams(command, Arrays.asList(params.getMessage())));
}
}

Expand Down Expand Up @@ -154,6 +162,9 @@ public void onBuildTaskFinish(TaskFinishParams params) {
if (Objects.equals(params.getDataKind(), TaskDataKind.COMPILE_REPORT)) {
String msg = params.getMessage() + "\n------\n";
lsClient.sendNotification(new ExecuteCommandParams(CLIENT_APPEND_BUILD_LOG_CMD, Arrays.asList(msg)));
if (params.getStatus() == StatusCode.ERROR) {
failedTaskCache.addAll((params.getTaskId().getParents()));
}
} else {
Either<String, Integer> id = Either.forLeft(params.getTaskId().getId());
WorkDoneProgressEnd workDoneProgressEnd = new WorkDoneProgressEnd();
Expand All @@ -162,4 +173,22 @@ public void onBuildTaskFinish(TaskFinishParams params) {
lsClient.notifyProgress(new ProgressParams(id, Either.forLeft(workDoneProgressEnd)));
}
}

private class LruCache<T> extends LinkedHashSet<T> {
private final int maxSize;

public LruCache(int maxSize) {
super(maxSize);
this.maxSize = maxSize;
}

@Override
public boolean add(T element) {
if (size() >= maxSize) {
T oldestElement = iterator().next();
remove(oldestElement);
}
return super.add(element);
}
}
}

0 comments on commit 8771f08

Please sign in to comment.