Skip to content

Commit

Permalink
Invalidate LS cache after fast run
Browse files Browse the repository at this point in the history
  • Loading branch information
manuranga committed Dec 11, 2023
1 parent 003242d commit 20dcc6d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,11 @@ public Optional<Process> run(Path filePath) throws IOException {
return Optional.empty();
}
JBallerinaBackend jBallerinaBackend = JBallerinaBackend.from(packageCompilation.get(), JvmTarget.JAVA_17);
for (Module module : pkg.modules()) {
for (DocumentId id : module.documentIds()) {
module.document(id).modify().apply();
}
}
Collection<Diagnostic> diagnostics = jBallerinaBackend.diagnosticResult().diagnostics(false);
if (diagnostics.stream().anyMatch(BallerinaWorkspaceManager::isError)) {
String msg = "Run command execution aborted due to compilation errors: " + diagnostics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
*/
package org.ballerinalang.langserver.workspace;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import io.ballerina.projects.Document;
import io.ballerina.projects.Module;
Expand All @@ -25,6 +28,7 @@
import io.ballerina.projects.ProjectKind;
import io.ballerina.projects.util.ProjectConstants;
import org.apache.commons.io.FileUtils;
import org.ballerinalang.diagramutil.DiagramUtil;
import org.ballerinalang.langserver.command.executors.RunExecutor;
import org.ballerinalang.langserver.command.executors.StopExecutor;
import org.ballerinalang.langserver.common.utils.CommonUtil;
Expand All @@ -35,6 +39,7 @@
import org.ballerinalang.langserver.commons.eventsync.exceptions.EventSyncException;
import org.ballerinalang.langserver.commons.workspace.WorkspaceDocumentException;
import org.ballerinalang.langserver.contexts.LanguageServerContextImpl;
import org.ballerinalang.langserver.extensions.ballerina.document.ExecutorPositionsUtil;
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
import org.eclipse.lsp4j.FileChangeType;
Expand Down Expand Up @@ -550,7 +555,36 @@ public void testWSLoadProject() throws WorkspaceDocumentException, ProjectExcept
@Test
public void testWSRunStopProject()
throws WorkspaceDocumentException, EventSyncException, IOException, LSCommandExecutorException {
Path filePath = RESOURCE_DIRECTORY.resolve("long_running").resolve("main.bal").toAbsolutePath();
Path projectPath = RESOURCE_DIRECTORY.resolve("long_running");
Path filePath = projectPath.resolve("main.bal");
ExecuteCommandContext execContext = runViaLs(filePath);
stopViaLs(execContext, projectPath);
}

@Test
public void testSemanticApiAfterWSRun()
throws WorkspaceDocumentException, EventSyncException, IOException, LSCommandExecutorException {
Path projectPath = RESOURCE_DIRECTORY.resolve("hello_service");
Path filePath = projectPath.resolve("main.bal");
ExecuteCommandContext execContext = runViaLs(filePath);

// Test syntax tree api
JsonElement syntaxTreeJSON = DiagramUtil.getSyntaxTreeJSON(workspaceManager.document(filePath).orElseThrow(),
workspaceManager.semanticModel(filePath).orElseThrow());
// 0 = class definition, 1 = listener declaration, 2 = service declaration
JsonObject service = syntaxTreeJSON.getAsJsonObject().get("members").getAsJsonArray().get(2).getAsJsonObject();
Assert.assertEquals(service.get("kind").getAsString(), "ServiceDeclaration");

// test executor positions api
JsonArray execPositions = ExecutorPositionsUtil.getExecutorPositions(workspaceManager, filePath);
Assert.assertEquals(execPositions.getAsJsonArray().get(0).getAsJsonObject().get("name").getAsString(),
"hello");

stopViaLs(execContext, projectPath);
}

private ExecuteCommandContext runViaLs(Path filePath)
throws WorkspaceDocumentException, EventSyncException, LSCommandExecutorException {
System.setProperty("java.command", guessJavaPath());
System.setProperty(BALLERINA_HOME, "./build");
workspaceManager.loadProject(filePath);
Expand All @@ -567,12 +601,16 @@ public void testWSRunStopProject()
Boolean didRan = runExecutor.execute(execContext);
Assert.assertTrue(didRan);
Assert.assertEquals(reduceToOutString(logCaptor), "Hello, World!" + System.lineSeparator());
return execContext;
}

private static void stopViaLs(ExecuteCommandContext execContext, Path projectPath)
throws LSCommandExecutorException, IOException {
StopExecutor stopExecutor = new StopExecutor();
Boolean didStop = stopExecutor.execute(execContext);
Assert.assertTrue(didStop);

Path target = RESOURCE_DIRECTORY.resolve("long_running").resolve("target");
Path target = projectPath.resolve("target");
FileUtils.deleteDirectory(target.toFile());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "baltest"
name = "long_running"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import ballerina/io;

public class Listener {
public isolated function 'start() returns error? {
}
public isolated function gracefulStop() returns error? {
}
public isolated function immediateStop() returns error? {
}
public isolated function detach(service object {} s) returns error? {
}
public isolated function attach(service object {} s, string[]|string? name = ()) returns error? {
return self.register(s, name);
}
isolated function register(service object {} s, string[]|string? name) returns error? {
return ();
}
}

listener Listener lsn = new;

service /hello on lsn {

function init() {
io:println("Hello, World!");
}

resource function get foo() returns int {
return 1;
}
}

0 comments on commit 20dcc6d

Please sign in to comment.