Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,25 @@ describe('diagnosticsChanged push notification', () => {
expect(notifiedDiag.range.end.character).toBe(20);
});

it('should serialize a null diagnostic code without crashing', async () => {
registerDiagnosticsChangedNotification(logger, httpServer as unknown as InProcHttpServer);

const uri = createMockUri('/test/file.ts');
const diag = createMockDiagnostic('Null code message', 0, 0, 0, 0, 10, 'test-source');
(diag as { code?: unknown }).code = null;

mockGetDiagnostics.mockReturnValue([diag]);

registeredCallback!({ uris: [uri] });
await vi.advanceTimersByTimeAsync(250);

const params = httpServer.broadcastNotification.mock.calls[0][1] as unknown as DiagnosticNotificationParams;
const notifiedDiag = params.uris[0].diagnostics[0];

expect(notifiedDiag.message).toBe('Null code message');
expect(notifiedDiag.code).toBeNull();
});

it('should handle multiple URIs in a single change event', async () => {
registerDiagnosticsChangedNotification(logger, httpServer as unknown as InProcHttpServer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,23 @@ describe('getDiagnostics tool', () => {

expect(result[0].diagnostics[0].code).toBe(2304);
});

it('should handle a null code in diagnostics without crashing', async () => {
const mockDiag = {
message: 'Null code error',
severity: 0,
range: {
start: { line: 0, character: 0 },
end: { line: 0, character: 5 },
},
source: 'ts',
code: null,
};
mockGetDiagnostics.mockReturnValue([mockDiag]);

const handler = server.getToolHandler('get_diagnostics')!;
const result = parseToolResult<DiagnosticsFileResult[]>(await handler({ uri: 'file:///test.ts' }));

expect(result[0].diagnostics[0].code).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as vscode from 'vscode';
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { makeTextResult } from './utils';
import { makeTextResult, normalizeDiagnosticCode } from './utils';
import { ILogger } from '../../../../../platform/log/common/logService';

export function registerGetDiagnosticsTool(server: McpServer, logger: ILogger): void {
Expand Down Expand Up @@ -44,7 +44,7 @@ export function registerGetDiagnosticsTool(server: McpServer, logger: ILogger):
end: { line: d.range.end.line, character: d.range.end.character },
},
source: d.source,
code: typeof d.code === 'object' ? d.code.value : d.code,
code: normalizeDiagnosticCode(d.code),
})),
})).filter(item => item.diagnostics.length > 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as vscode from 'vscode';
import { ILogger } from '../../../../../../platform/log/common/logService';
import { Delayer } from '../../../../../../util/vs/base/common/async';
import { InProcHttpServer } from '../../inProcHttpServer';
import { normalizeDiagnosticCode } from '../utils';

interface DiagnosticInfo {
uri: string;
Expand Down Expand Up @@ -49,7 +50,7 @@ function getDiagnosticsForUri(uri: vscode.Uri): DiagnosticInfo {
message: d.message,
severity: severityToString(d.severity),
source: d.source,
code: typeof d.code === 'object' ? d.code.value : d.code,
code: normalizeDiagnosticCode(d.code),
})),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import type * as vscode from 'vscode';

/**
* Normalizes a `vscode.Diagnostic.code` into a plain string or number for serialization.
*
* `Diagnostic.code` may be a string, a number, `undefined`, `null` (language servers and
* extensions are free to set it), or a `{ value, target }` object. Because
* `typeof null === 'object'`, callers must guard against `null` before reading `.value`;
* this helper centralizes that guard so every consumer of `vscode.languages.getDiagnostics`
* handles the value the same way.
*/
export function normalizeDiagnosticCode(code: vscode.Diagnostic['code']): string | number | undefined {
if (typeof code === 'object' && code !== null) {
return code.value;
}
return code;
}

export function makeTextResult(data: unknown): { content: [{ type: 'text'; text: string }] } {
return {
content: [
Expand Down