Skip to content

Commit

Permalink
fix: Report errors on activation (#3963)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Dec 22, 2024
1 parent c1995e1 commit a1419a1
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/_integrationTests/src/ExtensionApi.mts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export type { OnSpellCheckDocumentStep } from '../../_server/dist/api.cjs';
export type { CSpellClient } from '../../client/dist/client/index.mjs';
export type { ExtensionApi } from '../../client/dist/extensionApi.mjs';
export type { CSpellClient } from '../../client/out/client/index.mjs';
export type { ExtensionApi } from '../../client/out/extensionApi.mjs';
3 changes: 2 additions & 1 deletion packages/client/src/configWatcher.mts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { opMap, pipe } from '@cspell/cspell-pipe/sync';
import { defaultConfigFilenames } from 'cspell-lib';
import { createDisposableList } from 'utils-disposables';
import type { Disposable, Event, Uri } from 'vscode';
import * as vscode from 'vscode';

import { defaultConfigFilenames } from './defaultConfigFilenames.mjs';

export interface ConfigWatcher extends Disposable {
on: Event<Uri>;
onDidChangeConfig: Event<Uri>;
Expand Down
52 changes: 52 additions & 0 deletions packages/client/src/defaultConfigFilenames.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export const defaultConfigFilenames = [
'package.json',
'.cspell.json',
'cspell.json',
'.cSpell.json',
'cSpell.json',
'.cspell.jsonc',
'cspell.jsonc',
'.vscode/cspell.json',
'.vscode/cSpell.json',
'.vscode/.cspell.json',
'.cspell.config.json',
'.cspell.config.jsonc',
'.cspell.config.yaml',
'.cspell.config.yml',
'cspell.config.json',
'cspell.config.jsonc',
'cspell.config.yaml',
'cspell.config.yml',
'cspell.config.mjs',
'cspell.config.cjs',
'cspell.config.js',
'.cspell.config.mjs',
'.cspell.config.cjs',
'.cspell.config.js',
'.cspell.yaml',
'.cspell.yml',
'cspell.yaml',
'cspell.yml',
'.config/.cspell.json',
'.config/cspell.json',
'.config/.cSpell.json',
'.config/cSpell.json',
'.config/.cspell.jsonc',
'.config/cspell.jsonc',
'.config/cspell.config.json',
'.config/cspell.config.jsonc',
'.config/cspell.config.yaml',
'.config/cspell.config.yml',
'.config/cspell.config.mjs',
'.config/cspell.config.cjs',
'.config/cspell.config.js',
'.config/.cspell.config.json',
'.config/.cspell.config.jsonc',
'.config/.cspell.config.yaml',
'.config/.cspell.config.yml',
'.config/.cspell.config.mjs',
'.config/.cspell.config.cjs',
'.config/.cspell.config.js',
'.config/cspell.yaml',
'.config/cspell.yml',
] as const;
10 changes: 10 additions & 0 deletions packages/client/src/defaultConfigFilenames.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defaultConfigFilenames as cspellDefaultConfigFilenames } from 'cspell-lib';
import { describe, expect, test } from 'vitest';

import { defaultConfigFilenames } from './defaultConfigFilenames.mjs';

describe('defaultConfigFilenames', () => {
test('defaultConfigFilenames', () => {
expect(new Set(defaultConfigFilenames)).toEqual(new Set(cspellDefaultConfigFilenames));
});
});
46 changes: 35 additions & 11 deletions packages/client/src/extension.mts
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,42 @@ let currLogLevel: CSpellSettings['logLevel'] = undefined;
modules.init();

export function activate(context: ExtensionContext): Promise<ExtensionApi> {
performance.mark('cspell_activate_start');
di.set('extensionContext', context);
const eventLogger = createEventLogger(context.globalStorageUri);
di.set('eventLogger', eventLogger);
eventLogger.logActivate();
try {
performance.mark('cspell_activate_start');
di.set('extensionContext', context);
const eventLogger = createEventLogger(context.globalStorageUri);
di.set('eventLogger', eventLogger);
eventLogger.logActivate();

setOutputChannelLogLevel();
setOutputChannelLogLevel();

const eIssueTracker = new vscode.EventEmitter<IssueTracker>();
const pIssueTracker = new Promise<IssueTracker>((resolve) => eIssueTracker.event(resolve));
const eIssueTracker = new vscode.EventEmitter<IssueTracker>();
const pIssueTracker = new Promise<IssueTracker>((resolve) => eIssueTracker.event(resolve));

activateIssueViewer(context, pIssueTracker);
activateFileIssuesViewer(context, pIssueTracker);
performance.mark('activateIssueViewer');
activateIssueViewer(context, pIssueTracker);
performance.mark('activateFileIssuesViewer');
activateFileIssuesViewer(context, pIssueTracker);

return _activate(context, eIssueTracker);
performance.mark('start_async_activate');
return _activate(context, eIssueTracker).catch((e) => {
throw activationError(e);
});
} catch (e) {
throw activationError(e);
}
}

function activationError(e: unknown) {
return new Error(`Failed to activate: (${performance.getLastEventName()}) ${e}`, { cause: e });
}

async function _activate(context: ExtensionContext, eIssueTracker: vscode.EventEmitter<IssueTracker>): Promise<ExtensionApi> {
const logOutput = vscode.window.createOutputChannel('Code Spell Checker', { log: true });
const dLogger = bindLoggerToOutput(logOutput);

// Get the cSpell Client
performance.mark('create client');
const client = await CSpellClient.create(context);
context.subscriptions.push(client, logOutput, dLogger);

Expand All @@ -84,10 +98,13 @@ async function _activate(context: ExtensionContext, eIssueTracker: vscode.EventE
});
}

performance.mark('ExtensionRegEx.activate');
ExtensionRegEx.activate(context, client);

// Start the client.
performance.mark('start client');
await client.start();
performance.mark('start IssueTracker');
const issueTracker = new IssueTracker(client);
di.set('issueTracker', issueTracker);
eIssueTracker.fire(issueTracker);
Expand All @@ -112,6 +129,7 @@ async function _activate(context: ExtensionContext, eIssueTracker: vscode.EventE
triggerGetSettings();
}

performance.mark('createConfigWatcher');
const configWatcher = createConfigWatcher();
// console.log('config files: %o', await configWatcher.scanWorkspaceForConfigFiles());
const decorator = new SpellingIssueDecorator(context, issueTracker);
Expand All @@ -125,6 +143,8 @@ async function _activate(context: ExtensionContext, eIssueTracker: vscode.EventE
'cSpell.createCSpellTerminal': createTerminal,
};

performance.mark('register');

// Push the disposable to the context's subscriptions so that the
// client can be deactivated on extension deactivation
context.subscriptions.push(
Expand Down Expand Up @@ -161,6 +181,7 @@ async function _activate(context: ExtensionContext, eIssueTracker: vscode.EventE
client.onBlockFile(notifyUserOfBlockedFile),
);

performance.mark('registerCspellInlineCompletionProviders');
await registerCspellInlineCompletionProviders(context.subscriptions).catch(() => undefined);

function handleOnDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
Expand Down Expand Up @@ -240,6 +261,8 @@ async function _activate(context: ExtensionContext, eIssueTracker: vscode.EventE
const getConfigurationForDocument = <F extends ConfigurationFields>(doc: vscode.TextDocument, fields: ConfigFieldSelector<F>) =>
client.getConfigurationForDocument(doc, fields);

performance.mark('setup Extension API');

const server = {
registerConfig,
triggerGetSettings,
Expand All @@ -263,6 +286,7 @@ async function _activate(context: ExtensionContext, eIssueTracker: vscode.EventE
disableCurrentLanguage: commands.disableCurrentFileType,
} as const satisfies ExtensionApi & { getConfigurationForDocument: typeof getConfigurationForDocument };

performance.mark('activateWebview');
activateWebview(context);

performance.mark('cspell_activate_end');
Expand Down
8 changes: 8 additions & 0 deletions packages/client/src/util/perf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export class PerformanceTimeline {
this.addEvent(event);
}

getLastEvent(): TimeLineEvent {
return this.timeLine[this.timeLine.length - 1];
}

getLastEventName(): string {
return this.getLastEvent().name || '';
}

private addEvent(event: TimeLineEvent) {
this.timeLine.push(event);
this.timeLineEvents.set(event.name, event);
Expand Down
1 change: 1 addition & 0 deletions packages/client/tsconfig.api.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out",
"emitDeclarationOnly": true
},
"files": ["src/extensionApi.mts"]
Expand Down
1 change: 1 addition & 0 deletions packages/client/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "temp/dist"
Expand Down

0 comments on commit a1419a1

Please sign in to comment.