Skip to content

Commit 8a0f281

Browse files
committed
Check server does exist and version is the one expected
1 parent 09970ad commit 8a0f281

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export async function activate(context: vscode.ExtensionContext)
4343
// Create language client
4444
const possiblyNullClient = await createLanguageClient(context);
4545
if (possiblyNullClient === null) {
46-
vscode.window.showErrorMessage("Failed to launch shader-validator language server.");
46+
console.error("Failed to launch shader-validator language server.");
4747
return;
4848
}
4949
let client = possiblyNullClient;

src/validator.ts

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,49 @@ export function isRunningOnWeb() : boolean {
3636
// Web environment is detected with no fallback on child process which is not supported there.
3737
return typeof cp.spawn !== 'function' || typeof process === 'undefined';
3838
}
39+
function getServerVersion(serverPath: string) : string | null{
40+
console.assert(!isRunningOnWeb());
41+
if (fs.existsSync(serverPath)) {
42+
const result = cp.execSync(serverPath + " --version");
43+
const version = result.toString("utf8").trim();
44+
return version;
45+
} else {
46+
return null;
47+
}
48+
}
49+
function isValidVersion(serverVersion: string) {
50+
const requestedServerVersion = vscode.extensions.getExtension('antaalt.shader-validator')!.packageJSON.server_version;
51+
const versionExpected = "shader-language-server v" + requestedServerVersion;
52+
return serverVersion === versionExpected;
53+
}
3954
function getUserServerPath() : string | null {
4055
if (isRunningOnWeb()) {
4156
return null;
4257
} else {
4358
// Check configuration.
4459
let serverPath = vscode.workspace.getConfiguration("shader-validator").get<string>("serverPath");
4560
if (serverPath && serverPath.length > 0) {
46-
if (fs.existsSync(serverPath)) {
47-
console.info(`User server path found: ${serverPath}`);
61+
let serverVersion = getServerVersion(serverPath);
62+
if (serverVersion) {
63+
console.info(`shader-validator.serverPath found: ${serverPath}`);
4864
return serverPath;
4965
} else {
50-
vscode.window.showErrorMessage(`User server path is invalid: ${serverPath}`);
66+
console.warn("shader-validator.serverPath not found.");
5167
}
5268
}
5369
// Check environment variables
5470
if (process.env.SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH !== undefined) {
5571
let envPath = process.env.SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH;
56-
if (fs.existsSync(envPath)) {
72+
let serverVersion = getServerVersion(envPath);
73+
if (serverVersion) {
5774
console.info(`SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH found: ${envPath}`);
5875
return envPath;
5976
} else {
60-
vscode.window.showErrorMessage(`SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH is invalid: ${serverPath}`);
77+
console.warn("SHADER_LANGUAGE_SERVER_EXECUTABLE_PATH server path not found.");
6178
}
6279
}
6380
// Use bundled executables.
81+
console.info("No server path user settings found. Using bundled executable.");
6482
return null;
6583
}
6684
}
@@ -183,6 +201,14 @@ async function createLanguageClientStandard(context: vscode.ExtensionContext, pl
183201
context.subscriptions.push(channel);
184202

185203
const executable = getPlatformBinaryUri(context.extensionUri, platform);
204+
const version = getServerVersion(executable.fsPath);
205+
if (!version) {
206+
vscode.window.showErrorMessage(`Server executable not found.`);
207+
return null;
208+
}
209+
if (!isValidVersion(version)) {
210+
vscode.window.showWarningMessage(`${version} is not compatible with this extension. Server may crash or behave weirdly.`);
211+
}
186212
// Current working directory need to be set to executable for finding DLL.
187213
// But it would be better to have it pointing to workspace.
188214
const cwd = getPlatformBinaryDirectoryPath(context.extensionUri, platform);
@@ -231,23 +257,31 @@ async function createLanguageClientStandard(context: vscode.ExtensionContext, pl
231257

232258
return client;
233259
}
234-
async function createLanguageClientWASI(context: vscode.ExtensionContext) : Promise<LanguageClient> {
260+
async function createLanguageClientWASI(context: vscode.ExtensionContext) : Promise<LanguageClient | null> {
235261
const channelName = 'Shader language Server WASI'; // For trace option, need same name
236262
const channel = vscode.window.createOutputChannel(channelName);
237263
context.subscriptions.push(channel);
238264

239265
// Load the WASM API
240266
const wasm: Wasm = await Wasm.load();
241267

268+
// Load the WASM module. It is stored alongside the extension's JS code.
269+
// So we can use VS Code's file system API to load it. Makes it
270+
// independent of whether the code runs in the desktop or the web.
271+
const executable = getPlatformBinaryUri(context.extensionUri, ServerPlatform.wasi);
272+
const version = getServerVersion(executable.fsPath);
273+
if (!version) {
274+
vscode.window.showErrorMessage(`Server executable not found.`);
275+
return null;
276+
}
277+
if (!isValidVersion(version)) {
278+
vscode.window.showWarningMessage(`${version} is not compatible with extension. Server may crash or behave weirdly.`);
279+
}
242280
const serverOptions: ServerOptions = async () => {
243281
// Create virtual file systems to access workspaces from wasi app
244282
const mountPoints: MountPointDescriptor[] = [
245283
{ kind: 'workspaceFolder'}, // Workspaces
246284
];
247-
// Load the WASM module. It is stored alongside the extension's JS code.
248-
// So we can use VS Code's file system API to load it. Makes it
249-
// independent of whether the code runs in the desktop or the web.
250-
const executable = getPlatformBinaryUri(context.extensionUri, ServerPlatform.wasi);
251285
console.info(`Executing wasi server ${executable}`);
252286
const bits = await vscode.workspace.fs.readFile(executable);
253287
const module = await WebAssembly.compile(bits);

0 commit comments

Comments
 (0)