Skip to content

Commit 44a4bfc

Browse files
authored
Fix: Append the log message to build output if it's from compilation report. (#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 <sheche@microsoft.com>
1 parent a4265b3 commit 44a4bfc

File tree

1 file changed

+31
-2
lines changed
  • extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer

1 file changed

+31
-2
lines changed

extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildClient.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.Arrays;
66
import java.util.Date;
77
import java.util.HashSet;
8+
import java.util.LinkedHashSet;
89
import java.util.Objects;
910
import java.util.Set;
1011
import java.util.concurrent.CompletableFuture;
@@ -32,6 +33,7 @@
3233
import ch.epfl.scala.bsp4j.MessageType;
3334
import ch.epfl.scala.bsp4j.PublishDiagnosticsParams;
3435
import ch.epfl.scala.bsp4j.ShowMessageParams;
36+
import ch.epfl.scala.bsp4j.StatusCode;
3537
import ch.epfl.scala.bsp4j.TaskDataKind;
3638
import ch.epfl.scala.bsp4j.TaskFinishParams;
3739
import ch.epfl.scala.bsp4j.TaskProgressParams;
@@ -60,6 +62,8 @@ public class GradleBuildClient implements BuildClient {
6062

6163
private final JavaLanguageClient lsClient;
6264

65+
private final LruCache<String> failedTaskCache = new LruCache<>(16);
66+
6367
public GradleBuildClient() {
6468
this.lsClient = JavaLanguageServerPlugin.getProjectsManager().getConnection();
6569
}
@@ -70,8 +74,12 @@ public void onBuildLogMessage(LogMessageParams params) {
7074
if (type == MessageType.LOG) {
7175
Utils.sendTelemetry(this.lsClient, params.getMessage());
7276
} else {
73-
this.lsClient.sendNotification(new ExecuteCommandParams(CLIENT_BUILD_LOG_CMD,
74-
Arrays.asList(params.getMessage())));
77+
String command = CLIENT_BUILD_LOG_CMD;
78+
if (type == MessageType.ERROR && failedTaskCache.contains(params.getTask().getId())) {
79+
// append the compilation failure message to the build output channel.
80+
command = CLIENT_APPEND_BUILD_LOG_CMD;
81+
}
82+
this.lsClient.sendNotification(new ExecuteCommandParams(command, Arrays.asList(params.getMessage())));
7583
}
7684
}
7785

@@ -154,6 +162,9 @@ public void onBuildTaskFinish(TaskFinishParams params) {
154162
if (Objects.equals(params.getDataKind(), TaskDataKind.COMPILE_REPORT)) {
155163
String msg = params.getMessage() + "\n------\n";
156164
lsClient.sendNotification(new ExecuteCommandParams(CLIENT_APPEND_BUILD_LOG_CMD, Arrays.asList(msg)));
165+
if (params.getStatus() == StatusCode.ERROR) {
166+
failedTaskCache.addAll((params.getTaskId().getParents()));
167+
}
157168
} else {
158169
Either<String, Integer> id = Either.forLeft(params.getTaskId().getId());
159170
WorkDoneProgressEnd workDoneProgressEnd = new WorkDoneProgressEnd();
@@ -162,4 +173,22 @@ public void onBuildTaskFinish(TaskFinishParams params) {
162173
lsClient.notifyProgress(new ProgressParams(id, Either.forLeft(workDoneProgressEnd)));
163174
}
164175
}
176+
177+
private class LruCache<T> extends LinkedHashSet<T> {
178+
private final int maxSize;
179+
180+
public LruCache(int maxSize) {
181+
super(maxSize);
182+
this.maxSize = maxSize;
183+
}
184+
185+
@Override
186+
public boolean add(T element) {
187+
if (size() >= maxSize) {
188+
T oldestElement = iterator().next();
189+
remove(oldestElement);
190+
}
191+
return super.add(element);
192+
}
193+
}
165194
}

0 commit comments

Comments
 (0)