Bugfixes
This commit is contained in:
@@ -8,6 +8,7 @@ export class OpenCodeView extends ItemView {
|
||||
plugin: OpenCodePlugin;
|
||||
private iframeEl: HTMLIFrameElement | null = null;
|
||||
private currentState: ProcessState = "stopped";
|
||||
private unsubscribeStateChange: (() => void) | null = null;
|
||||
|
||||
constructor(leaf: WorkspaceLeaf, plugin: OpenCodePlugin) {
|
||||
super(leaf);
|
||||
@@ -31,7 +32,7 @@ export class OpenCodeView extends ItemView {
|
||||
this.contentEl.addClass("opencode-container");
|
||||
|
||||
// Subscribe to state changes
|
||||
this.plugin.onProcessStateChange((state) => {
|
||||
this.unsubscribeStateChange = this.plugin.onProcessStateChange((state) => {
|
||||
this.currentState = state;
|
||||
this.updateView();
|
||||
});
|
||||
@@ -47,6 +48,12 @@ export class OpenCodeView extends ItemView {
|
||||
}
|
||||
|
||||
async onClose(): Promise<void> {
|
||||
// Unsubscribe from state changes to prevent memory leak
|
||||
if (this.unsubscribeStateChange) {
|
||||
this.unsubscribeStateChange();
|
||||
this.unsubscribeStateChange = null;
|
||||
}
|
||||
|
||||
// Clean up iframe
|
||||
if (this.iframeEl) {
|
||||
this.iframeEl.src = "about:blank";
|
||||
@@ -184,12 +191,26 @@ export class OpenCodeView extends ItemView {
|
||||
setIcon(iconEl, "alert-circle");
|
||||
|
||||
statusContainer.createEl("h3", { text: "Failed to start OpenCode" });
|
||||
statusContainer.createEl("p", {
|
||||
text: "There was an error starting the OpenCode server. Please check that OpenCode is installed and try again.",
|
||||
cls: "opencode-status-message",
|
||||
|
||||
// Display specific error message if available
|
||||
const errorMessage = this.plugin.getLastError();
|
||||
if (errorMessage) {
|
||||
statusContainer.createEl("p", {
|
||||
text: errorMessage,
|
||||
cls: "opencode-status-message opencode-error-message",
|
||||
});
|
||||
} else {
|
||||
statusContainer.createEl("p", {
|
||||
text: "There was an error starting the OpenCode server.",
|
||||
cls: "opencode-status-message",
|
||||
});
|
||||
}
|
||||
|
||||
const buttonContainer = statusContainer.createDiv({
|
||||
cls: "opencode-button-group",
|
||||
});
|
||||
|
||||
const retryButton = statusContainer.createEl("button", {
|
||||
const retryButton = buttonContainer.createEl("button", {
|
||||
text: "Retry",
|
||||
cls: "mod-cta",
|
||||
});
|
||||
@@ -197,7 +218,7 @@ export class OpenCodeView extends ItemView {
|
||||
this.plugin.startServer();
|
||||
});
|
||||
|
||||
const settingsButton = statusContainer.createEl("button", {
|
||||
const settingsButton = buttonContainer.createEl("button", {
|
||||
text: "Open Settings",
|
||||
});
|
||||
settingsButton.addEventListener("click", () => {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { spawn, ChildProcess } from "child_process";
|
||||
import { Notice } from "obsidian";
|
||||
import { OpenCodeSettings } from "./types";
|
||||
|
||||
export type ProcessState = "stopped" | "starting" | "running" | "error";
|
||||
@@ -7,6 +6,8 @@ export type ProcessState = "stopped" | "starting" | "running" | "error";
|
||||
export class ProcessManager {
|
||||
private process: ChildProcess | null = null;
|
||||
private state: ProcessState = "stopped";
|
||||
private lastError: string | null = null;
|
||||
private earlyExitCode: number | null = null;
|
||||
private settings: OpenCodeSettings;
|
||||
private workingDirectory: string;
|
||||
private projectDirectory: string;
|
||||
@@ -37,6 +38,10 @@ export class ProcessManager {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
getLastError(): string | null {
|
||||
return this.lastError;
|
||||
}
|
||||
|
||||
getUrl(): string {
|
||||
const baseUrl = `http://${this.settings.hostname}:${this.settings.port}`;
|
||||
// Encode the project directory path as base64 for the URL
|
||||
@@ -50,13 +55,14 @@ export class ProcessManager {
|
||||
}
|
||||
|
||||
this.setState("starting");
|
||||
this.lastError = null;
|
||||
this.earlyExitCode = null;
|
||||
|
||||
try {
|
||||
// Validate vault/project directory is set
|
||||
if (!this.projectDirectory) {
|
||||
const error = "Project directory (vault) not configured";
|
||||
console.error("[OpenCode Error]", error);
|
||||
new Notice(`Failed to start OpenCode: ${error}`);
|
||||
this.lastError = "Project directory (vault) not configured";
|
||||
console.error("[OpenCode Error]", this.lastError);
|
||||
this.setState("error");
|
||||
return false;
|
||||
}
|
||||
@@ -113,15 +119,28 @@ export class ProcessManager {
|
||||
this.process.on("exit", (code, signal) => {
|
||||
console.log(`OpenCode process exited with code ${code}, signal ${signal}`);
|
||||
this.process = null;
|
||||
|
||||
// Track early exit during startup for better error messages
|
||||
if (this.state === "starting" && code !== null && code !== 0) {
|
||||
this.earlyExitCode = code;
|
||||
}
|
||||
|
||||
// Only set stopped if we're in running state (not during startup)
|
||||
if (this.state === "running") {
|
||||
this.setState("stopped");
|
||||
}
|
||||
});
|
||||
|
||||
this.process.on("error", (err) => {
|
||||
this.process.on("error", (err: NodeJS.ErrnoException) => {
|
||||
console.error("Failed to start OpenCode process:", err);
|
||||
new Notice(`Failed to start OpenCode: ${err.message}`);
|
||||
|
||||
// Provide user-friendly error messages for common errors
|
||||
if (err.code === "ENOENT") {
|
||||
this.lastError = `OpenCode executable not found at '${this.settings.opencodePath}'`;
|
||||
} else {
|
||||
this.lastError = `Failed to start OpenCode: ${err.message}`;
|
||||
}
|
||||
|
||||
this.process = null;
|
||||
this.setState("error");
|
||||
});
|
||||
@@ -132,13 +151,27 @@ export class ProcessManager {
|
||||
this.setState("running");
|
||||
return true;
|
||||
} else {
|
||||
// If already in error state (e.g., from spawn error event), don't overwrite
|
||||
if (this.state === "error") {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Determine appropriate error message
|
||||
if (this.earlyExitCode !== null) {
|
||||
this.lastError = `OpenCode process exited unexpectedly (exit code ${this.earlyExitCode})`;
|
||||
} else if (!this.process) {
|
||||
this.lastError = "OpenCode process exited before server became ready";
|
||||
} else {
|
||||
this.lastError = "OpenCode server failed to start within timeout";
|
||||
}
|
||||
|
||||
this.stop();
|
||||
this.setState("error");
|
||||
new Notice("OpenCode server failed to start within timeout");
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error starting OpenCode:", error);
|
||||
this.lastError = error instanceof Error ? error.message : String(error);
|
||||
this.setState("error");
|
||||
return false;
|
||||
}
|
||||
@@ -151,20 +184,42 @@ export class ProcessManager {
|
||||
}
|
||||
|
||||
if (this.process) {
|
||||
const proc = this.process;
|
||||
const pid = proc.pid;
|
||||
|
||||
console.log("[OpenCode] Stopping process with PID:", pid);
|
||||
|
||||
// Set state to stopped first to prevent exit handler from interfering
|
||||
this.setState("stopped");
|
||||
|
||||
// Now clear the process reference before killing
|
||||
// This ensures the exit handler knows we initiated the stop
|
||||
this.process = null;
|
||||
|
||||
try {
|
||||
// Try graceful shutdown first
|
||||
this.process.kill("SIGTERM");
|
||||
proc.kill("SIGTERM");
|
||||
console.log("[OpenCode] Sent SIGTERM to process");
|
||||
|
||||
// Force kill after 2 seconds if still running
|
||||
setTimeout(() => {
|
||||
if (this.process && !this.process.killed) {
|
||||
this.process.kill("SIGKILL");
|
||||
// Check if process has exited (exitCode or signalCode will be set)
|
||||
if (proc.exitCode === null && proc.signalCode === null) {
|
||||
console.log("[OpenCode] Process still running after SIGTERM, sending SIGKILL");
|
||||
try {
|
||||
proc.kill("SIGKILL");
|
||||
} catch (error) {
|
||||
console.error("[OpenCode] Error sending SIGKILL:", error);
|
||||
}
|
||||
} else {
|
||||
console.log("[OpenCode] Process exited with:", proc.exitCode, proc.signalCode);
|
||||
}
|
||||
}, 2000);
|
||||
} catch (error) {
|
||||
console.error("Error stopping OpenCode process:", error);
|
||||
console.error("[OpenCode] Error stopping process:", error);
|
||||
}
|
||||
this.process = null;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.setState("stopped");
|
||||
|
||||
15
src/main.ts
15
src/main.ts
@@ -197,14 +197,25 @@ export default class OpenCodePlugin extends Plugin {
|
||||
return this.processManager?.getState() ?? "stopped";
|
||||
}
|
||||
|
||||
// Get the last error message from the process manager
|
||||
getLastError(): string | null {
|
||||
return this.processManager?.getLastError() ?? null;
|
||||
}
|
||||
|
||||
// Get the server URL
|
||||
getServerUrl(): string {
|
||||
return this.processManager?.getUrl() ?? `http://127.0.0.1:${this.settings.port}`;
|
||||
}
|
||||
|
||||
// Subscribe to process state changes
|
||||
onProcessStateChange(callback: (state: ProcessState) => void): void {
|
||||
// Subscribe to process state changes, returns unsubscribe function
|
||||
onProcessStateChange(callback: (state: ProcessState) => void): () => void {
|
||||
this.stateChangeCallbacks.push(callback);
|
||||
return () => {
|
||||
const index = this.stateChangeCallbacks.indexOf(callback);
|
||||
if (index > -1) {
|
||||
this.stateChangeCallbacks.splice(index, 1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Notify all subscribers of state change
|
||||
|
||||
Reference in New Issue
Block a user