Skip to content

Commit f577067

Browse files
authored
check whether compiler lsp support compile project (microsoft#7374)
When leverage lsp to emit code, the lsp server need have the capacity of internalCompile. Typespec compiler support this only after version 1.0.0. So check the lsp capacity before emit code, and provide clear information to info user to upgrade their compiler.
1 parent 05ce00f commit f577067

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
3+
changeKind: fix
4+
packages:
5+
- typespec-vscode
6+
---
7+
8+
Check whether the compiler language server supports project compilation.

packages/typespec-vscode/src/vscode-cmd/emit-code/emit-code.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { getDirectoryPath } from "../../path-utils.js";
1212
import telemetryClient from "../../telemetry/telemetry-client.js";
1313
import { OperationTelemetryEvent } from "../../telemetry/telemetry-event.js";
1414
import { resolveTypeSpecCli } from "../../tsp-executable-resolver.js";
15+
import { TspLanguageClient } from "../../tsp-language-client.js";
1516
import { ResultCode } from "../../types.js";
1617
import {
1718
formatDiagnostic,
@@ -460,6 +461,7 @@ async function doEmit(
460461
});
461462
return ResultCode.Fail;
462463
}
464+
463465
const compileResult = await tspLanguageClient.compileProject(
464466
{
465467
uri: getVscodeUriFromPath(mainTspFile),
@@ -540,6 +542,25 @@ export async function emitCode(
540542
uri: vscode.Uri,
541543
tel: OperationTelemetryEvent,
542544
): Promise<ResultCode> {
545+
if (!tspLanguageClient) {
546+
logger.error(
547+
`LSP client is not started. Make sure typespec compiler has been installed. Emitting Cancelled.`,
548+
[],
549+
{
550+
showOutput: true,
551+
showPopup: true,
552+
},
553+
);
554+
return ResultCode.Cancelled;
555+
}
556+
const isSupport = await isCompilerSupport(tspLanguageClient);
557+
if (!isSupport) {
558+
logger.info(
559+
"Compiling project via the language server is not supported in the current version of TypeSpec Compiler. Emitting Cancelled.",
560+
);
561+
tel.lastStep = "Check compiler capability";
562+
return ResultCode.Cancelled;
563+
}
543564
let tspProjectFile: string = "";
544565
if (!uri) {
545566
const targetPathes = await TraverseMainTspFileInWorkspace();
@@ -797,3 +818,21 @@ export async function emitCode(
797818
}
798819
}
799820
}
821+
822+
async function isCompilerSupport(client: TspLanguageClient): Promise<boolean> {
823+
if (
824+
client.initializeResult?.serverInfo?.version === undefined ||
825+
client.initializeResult?.customCapacities?.internalCompile !== true
826+
) {
827+
logger.error(
828+
`Compiling project via the language server is not supported in the current version of TypeSpec Compiler (ver ${client.initializeResult?.serverInfo?.version ?? "<= 1.0.0"}). Please upgrade to a version later than 1.0.0 (by npm install @typespec/compiler) and try again.`,
829+
[],
830+
{
831+
showOutput: true,
832+
showPopup: true,
833+
},
834+
);
835+
return false;
836+
}
837+
return true;
838+
}

0 commit comments

Comments
 (0)