Skip to content

Commit 449fa0f

Browse files
authored
Merge pull request #35 from n-WN/copilot/fix-34
Fix 'command runsagemathfile.run not found' error by improving extension activation robustness
2 parents 57f7e5c + 1f8ff4c commit 449fa0f

File tree

2 files changed

+62
-44
lines changed

2 files changed

+62
-44
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/extension.ts

Lines changed: 60 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import {
1212
let client: LanguageClient;
1313

1414
export function activate(context: vscode.ExtensionContext) {
15-
// Start the Language Server
16-
startLanguageServer(context);
17-
18-
// Register the run command
15+
// Register the run command first to ensure it's available even if language server fails
1916
let runDisposable = vscode.commands.registerCommand('runsagemathfile.run', () => {
2017
const editor = vscode.window.activeTextEditor;
2118
if (!editor) {
@@ -72,52 +69,73 @@ export function activate(context: vscode.ExtensionContext) {
7269

7370
// Register the restart server command
7471
let restartDisposable = vscode.commands.registerCommand('sagemathEnhanced.restartServer', async () => {
75-
if (client) {
76-
await client.stop();
77-
startLanguageServer(context);
72+
try {
73+
if (client) {
74+
await client.stop();
75+
}
76+
await startLanguageServer(context);
7877
vscode.window.showInformationMessage('SageMath Language Server restarted');
78+
} catch (error) {
79+
console.error('Failed to restart SageMath Language Server:', error);
80+
vscode.window.showErrorMessage('Failed to restart SageMath Language Server. Check the output console for details.');
7981
}
8082
});
8183

8284
context.subscriptions.push(runDisposable, restartDisposable);
85+
86+
// Start the Language Server asynchronously to avoid blocking command registration
87+
startLanguageServer(context).catch(error => {
88+
console.error('Failed to start SageMath Language Server:', error);
89+
vscode.window.showWarningMessage('SageMath Language Server failed to start. Some features may not be available.');
90+
});
8391
}
8492

85-
function startLanguageServer(context: vscode.ExtensionContext) {
86-
// The server is implemented in the server directory
87-
const serverModule = context.asAbsolutePath(
88-
path.join('out', 'server', 'src', 'server.js')
89-
);
90-
91-
// If the extension is launched in debug mode then the debug server options are used
92-
// Otherwise the run options are used
93-
const serverOptions: ServerOptions = {
94-
run: { module: serverModule, transport: TransportKind.ipc },
95-
debug: {
96-
module: serverModule,
97-
transport: TransportKind.ipc,
98-
}
99-
};
100-
101-
// Options to control the language client
102-
const clientOptions: LanguageClientOptions = {
103-
// Register the server for SageMath documents
104-
documentSelector: [{ scheme: 'file', language: 'sage' }],
105-
synchronize: {
106-
// Notify the server about file changes to '.sage' files contained in the workspace
107-
fileEvents: vscode.workspace.createFileSystemWatcher('**/*.sage')
93+
async function startLanguageServer(context: vscode.ExtensionContext): Promise<void> {
94+
try {
95+
// The server is implemented in the server directory
96+
const serverModule = context.asAbsolutePath(
97+
path.join('out', 'server', 'src', 'server.js')
98+
);
99+
100+
// Check if server module exists
101+
if (!fs.existsSync(serverModule)) {
102+
throw new Error(`Language server module not found at: ${serverModule}`);
108103
}
109-
};
110-
111-
// Create the language client and start the client.
112-
client = new LanguageClient(
113-
'sagemathEnhanced',
114-
'SageMath Enhanced',
115-
serverOptions,
116-
clientOptions
117-
);
118-
119-
// Start the client. This will also launch the server
120-
client.start();
104+
105+
// If the extension is launched in debug mode then the debug server options are used
106+
// Otherwise the run options are used
107+
const serverOptions: ServerOptions = {
108+
run: { module: serverModule, transport: TransportKind.ipc },
109+
debug: {
110+
module: serverModule,
111+
transport: TransportKind.ipc,
112+
}
113+
};
114+
115+
// Options to control the language client
116+
const clientOptions: LanguageClientOptions = {
117+
// Register the server for SageMath documents
118+
documentSelector: [{ scheme: 'file', language: 'sage' }],
119+
synchronize: {
120+
// Notify the server about file changes to '.sage' files contained in the workspace
121+
fileEvents: vscode.workspace.createFileSystemWatcher('**/*.sage')
122+
}
123+
};
124+
125+
// Create the language client and start the client.
126+
client = new LanguageClient(
127+
'sagemathEnhanced',
128+
'SageMath Enhanced',
129+
serverOptions,
130+
clientOptions
131+
);
132+
133+
// Start the client. This will also launch the server
134+
await client.start();
135+
} catch (error) {
136+
console.error('Error in startLanguageServer:', error);
137+
throw error; // Re-throw to be caught by the calling function
138+
}
121139
}
122140

123141
export function deactivate(): Thenable<void> | undefined {

0 commit comments

Comments
 (0)