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

improved handling of lsp failure state #309

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
icon="icons/AmazonQ.png"
class="software.aws.toolkits.eclipse.amazonq.views.ChatAssetMissingView">
</view>
<view
id="software.aws.toolkits.eclipse.amazonq.views.LspStartUpFailedView"
name="Amazon Q"
icon="icons/AmazonQ.png"
class="software.aws.toolkits.eclipse.amazonq.views.LspStartUpFailedView">
</view>
<view
id="software.aws.toolkits.eclipse.amazonq.views.ToolkitLoginWebview"
name="Amazon Q"
Expand Down Expand Up @@ -142,6 +148,12 @@
relationship="stack"
visible="false">
</view>
<view
id="software.aws.toolkits.eclipse.amazonq.views.LspStartUpFailedView"
relative="software.aws.toolkits.eclipse.amazonq.views.ToolkitLoginWebview"
relationship="stack"
visible="false">
</view>
</perspectiveExtension>
</extension>
<extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import software.aws.toolkits.eclipse.amazonq.chat.models.GenericCommandParams;
import software.aws.toolkits.eclipse.amazonq.chat.models.SendToPromptParams;
import software.aws.toolkits.eclipse.amazonq.chat.models.TriggerType;
import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspStatusManager;
import software.aws.toolkits.eclipse.amazonq.plugin.Activator;
import software.aws.toolkits.eclipse.amazonq.telemetry.ToolkitTelemetryProvider;
import software.aws.toolkits.eclipse.amazonq.telemetry.metadata.ExceptionMetadata;
Expand All @@ -27,7 +28,7 @@ public abstract class AbstractQChatEditorActionsHandler extends AbstractHandler
@Override
public final boolean isEnabled() {
try {
return Activator.getLoginService().getAuthState().isLoggedIn();
return Activator.getLoginService().getAuthState().isLoggedIn() && !LspStatusManager.lspFailed();
} catch (Exception e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import software.aws.toolkits.eclipse.amazonq.plugin.Activator;

import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspStatusManager;

import software.aws.toolkits.eclipse.amazonq.views.ViewVisibilityManager;

public class QOpenLoginViewHandler extends AbstractHandler {
@Override
public final Object execute(final ExecutionEvent event) {
if (Activator.getLoginService().getAuthState().isLoggedIn()) {
ViewVisibilityManager.showChatView("statusBar");
if (LspStatusManager.lspFailed()) {
ViewVisibilityManager.showLspStartUpFailedView("statusBar");
} else {
ViewVisibilityManager.showLoginView("statusBar");
ViewVisibilityManager.showDefaultView("statusBar");
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import software.amazon.awssdk.utils.StringUtils;
import software.aws.toolkits.eclipse.amazonq.lsp.encryption.DefaultLspEncryptionManager;
import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspManager;
import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspStatusManager;
import software.aws.toolkits.eclipse.amazonq.lsp.manager.fetcher.RecordLspSetupArgs;
import software.aws.toolkits.eclipse.amazonq.providers.LspManagerProvider;
import software.aws.toolkits.eclipse.amazonq.telemetry.LanguageServerTelemetryProvider;
Expand All @@ -26,19 +27,25 @@ public class QLspConnectionProvider extends AbstractLspConnectionProvider {

public QLspConnectionProvider() throws IOException {
super();
LanguageServerTelemetryProvider.setAllStartPoint(Instant.now());
LspManager lspManager = LspManagerProvider.getInstance();
var lspInstallResult = lspManager.getLspInstallation();
try {
LanguageServerTelemetryProvider.setAllStartPoint(Instant.now());
LspManager lspManager = LspManagerProvider.getInstance();
var lspInstallResult = lspManager.getLspInstallation();

setWorkingDirectory(lspInstallResult.getServerDirectory());

setWorkingDirectory(lspInstallResult.getServerDirectory());
var serverCommand = Paths.get(lspInstallResult.getServerDirectory(), lspInstallResult.getServerCommand());
List<String> commands = new ArrayList<>();
commands.add(serverCommand.toString());
commands.add(lspInstallResult.getServerCommandArgs());
commands.add("--stdio");
commands.add("--set-credentials-encryption-key");
setCommands(commands);
} catch (Exception e) {
LspStatusManager.setToFailed();
throw(e);
}

var serverCommand = Paths.get(lspInstallResult.getServerDirectory(), lspInstallResult.getServerCommand());
List<String> commands = new ArrayList<>();
commands.add(serverCommand.toString());
commands.add(lspInstallResult.getServerCommandArgs());
commands.add("--stdio");
commands.add("--set-credentials-encryption-key");
setCommands(commands);
}

@Override
Expand Down Expand Up @@ -69,10 +76,12 @@ public final void start() throws IOException {

lspEncryption.initializeEncryptedCommunication(serverStdIn);
} catch (Exception e) {
LspStatusManager.setToFailed();
emitInitFailure(ExceptionMetadata.scrubException(e));
Activator.getLogger().error("Error occured while initializing communication with Amazon Q Lsp Server", e);
}
} catch (Exception e) {
LspStatusManager.setToFailed();
emitInitFailure(ExceptionMetadata.scrubException(e));
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.eclipse.amazonq.lsp.manager;

public enum LspState {
ACTIVE,
FAILED,
PENDING
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.eclipse.amazonq.lsp.manager;

import software.aws.toolkits.eclipse.amazonq.views.ViewVisibilityManager;

public final class LspStatusManager {

private LspStatusManager() {
//prevent instantiation
}

private static LspState lspState = LspState.PENDING;

public static boolean lspFailed() {
return (lspState == LspState.FAILED);
}
public static void setToActive() {
lspState = LspState.ACTIVE;
ViewVisibilityManager.showDefaultView("restart");
}
public static void setToFailed() {
if (lspState != LspState.FAILED) {
ViewVisibilityManager.showLspStartUpFailedView("update");
lspState = LspState.FAILED;
}
}
public static LspState getLspState() {
return lspState;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.eclipse.lsp4j.services.LanguageServer;

import software.aws.toolkits.eclipse.amazonq.lsp.AmazonQLspServer;
import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspStatusManager;
import software.aws.toolkits.eclipse.amazonq.lsp.manager.fetcher.RecordLspSetupArgs;
import software.aws.toolkits.eclipse.amazonq.telemetry.LanguageServerTelemetryProvider;
import software.aws.toolkits.telemetry.TelemetryDefinitions.Result;
Expand Down Expand Up @@ -52,6 +53,7 @@ public void setAmazonQServer(final LanguageServer server) {
future.complete(server);
}
emitInitializeMetric();
LspStatusManager.setToActive();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import software.aws.toolkits.eclipse.amazonq.chat.ChatTheme;
import software.aws.toolkits.eclipse.amazonq.lsp.AwsServerCapabiltiesProvider;
import software.aws.toolkits.eclipse.amazonq.lsp.auth.model.AuthState;
import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspStatusManager;
import software.aws.toolkits.eclipse.amazonq.lsp.model.ChatOptions;
import software.aws.toolkits.eclipse.amazonq.lsp.model.QuickActions;
import software.aws.toolkits.eclipse.amazonq.lsp.model.QuickActionsCommandGroup;
Expand Down Expand Up @@ -140,7 +141,7 @@ public final void onAuthStatusChanged(final AuthState authState) {
// chat view
if (browser != null && !browser.isDisposed() && !chatStateManager.hasPreservedState()) {
Optional<String> content = getContent();
if (!content.isPresent()) {
if (!content.isPresent() && !LspStatusManager.lspFailed()) {
canDisposeState = true;
ViewVisibilityManager.showChatAssetMissingView("update");
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.eclipse.amazonq.views;
import java.util.concurrent.CompletableFuture;

import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspStatusManager;

public final class LspStartUpFailedView extends BaseView {
public static final String ID = "software.aws.toolkits.eclipse.amazonq.views.LspStartUpFailedView";

private static final String ICON_PATH = "icons/AmazonQ64.png";
private static final String HEADER_LABEL = "Language Server failed to start.";
private static final String DETAIL_MESSAGE = "Restart Eclipse or review error logs for troubleshooting";

/* TODO: After refactor of LSP error handling is completed,
* add logic to base detail_message on error code returned from exception
* */
@Override
protected String getIconPath() {
return ICON_PATH;
}

@Override
protected String getHeaderLabel() {
return HEADER_LABEL;
}

@Override
protected String getDetailMessage() {
return DETAIL_MESSAGE;
}

@Override
protected void showAlternateView() {
ViewVisibilityManager.showDefaultView("restart");
}

@Override
protected CompletableFuture<Boolean> isViewDisplayable() {
return CompletableFuture.completedFuture(LspStatusManager.lspFailed());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,26 @@ private ViewVisibilityManager() {
private static final String RE_AUTHENTICATE_VIEW = ReauthenticateView.ID;
private static final String CHAT_ASSET_MISSING_VIEW = ChatAssetMissingView.ID;
private static final String CODE_REFERENCE_VIEW = AmazonQCodeReferenceView.ID;
private static final String LSP_STARTUP_FAILED_VIEW = LspStartUpFailedView.ID;
private static final String ERROR_LOG_VIEW = "org.eclipse.pde.runtime.LogView";

private static final Set<String> MUTUALLY_EXCLUSIVE_VIEWS = Set.of(
TOOLKIT_LOGIN_VIEW,
CHAT_VIEW,
DEPENDENCY_MISSING_VIEW,
RE_AUTHENTICATE_VIEW,
CHAT_ASSET_MISSING_VIEW
CHAT_ASSET_MISSING_VIEW,
LSP_STARTUP_FAILED_VIEW
);

public static void showDefaultView(final String source) {
if (Activator.getLoginService().getAuthState().isLoggedIn()) {
showChatView(source);
} else {
showLoginView(source);
}
}

public static void showLoginView(final String source) {
showMutuallyExclusiveView(TOOLKIT_LOGIN_VIEW, source);
}
Expand All @@ -56,6 +66,10 @@ public static void showChatAssetMissingView(final String source) {
showMutuallyExclusiveView(CHAT_ASSET_MISSING_VIEW, source);
}

public static void showLspStartUpFailedView(final String source) {
showMutuallyExclusiveView(LSP_STARTUP_FAILED_VIEW, source);
}

public static void showCodeReferenceView(final String source) {
showView(CODE_REFERENCE_VIEW, source);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@

package software.aws.toolkits.eclipse.amazonq.lsp.connection;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import software.aws.toolkits.eclipse.amazonq.extensions.implementation.ActivatorStaticMockExtension;
import software.aws.toolkits.eclipse.amazonq.extensions.implementation.DefaultLspEncryptionManagerStaticMockExtension;
import software.aws.toolkits.eclipse.amazonq.extensions.implementation.LspManagerProviderStaticMockExtension;
import software.aws.toolkits.eclipse.amazonq.extensions.implementation.ProxyUtilsStaticMockExtension;
import software.aws.toolkits.eclipse.amazonq.lsp.encryption.LspEncryptionManager;
import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspInstallResult;
import software.aws.toolkits.eclipse.amazonq.lsp.manager.LspStatusManager;

import org.eclipse.lsp4e.server.ProcessStreamConnectionProvider;
import software.aws.toolkits.eclipse.amazonq.util.LoggingService;
import software.aws.toolkits.eclipse.amazonq.util.ProxyUtil;
Expand All @@ -30,6 +35,7 @@
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;

public final class QLspConnectionProviderTest {
Expand All @@ -48,6 +54,18 @@ public final class QLspConnectionProviderTest {
@RegisterExtension
private static ProxyUtilsStaticMockExtension proxyUtilsStaticMockExtension = new ProxyUtilsStaticMockExtension();

private MockedStatic<LspStatusManager> mockLspStatusManager;

@BeforeEach
void setupBeforeEach() {
mockLspStatusManager = mockStatic(LspStatusManager.class);
}

@AfterEach
void tearDown() {
mockLspStatusManager.close();
}

private static final class TestProcessConnectionProvider extends ProcessStreamConnectionProvider {

TestProcessConnectionProvider(final List<String> commands, final String workingDirectory) {
Expand Down
Loading