Skip to content

Commit c775d6f

Browse files
committed
fix(vscode): try to load stdlib firstly from the installed zenstack package
1 parent d8cbfbb commit c775d6f

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

packages/schema/src/language-server/zmodel-workspace-manager.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { isPlugin, Model } from '@zenstackhq/language/ast';
22
import { getLiteral } from '@zenstackhq/sdk';
33
import { DefaultWorkspaceManager, interruptAndCheck, LangiumDocument } from 'langium';
4+
import fs from 'fs';
45
import path from 'path';
56
import { CancellationToken, WorkspaceFolder } from 'vscode-languageserver';
67
import { URI, Utils } from 'vscode-uri';
@@ -17,7 +18,43 @@ export class ZModelWorkspaceManager extends DefaultWorkspaceManager {
1718
_collector: (document: LangiumDocument) => void
1819
): Promise<void> {
1920
await super.loadAdditionalDocuments(_folders, _collector);
20-
const stdLibUri = URI.file(path.join(__dirname, '../res', STD_LIB_MODULE_NAME));
21+
22+
let stdLibPath: string;
23+
// First, try to find the stdlib from an installed zenstack package
24+
// in the project's node_modules
25+
let installedStdlibPath: string | undefined;
26+
for (const folder of _folders) {
27+
const folderPath = URI.parse(folder.uri).fsPath;
28+
try {
29+
// Try to resolve zenstack from the workspace folder
30+
const languagePackagePath = require.resolve('zenstack/package.json', {
31+
paths: [folderPath]
32+
});
33+
const languagePackageDir = path.dirname(languagePackagePath);
34+
const candidateStdlibPath = path.join(languagePackageDir, 'res', STD_LIB_MODULE_NAME);
35+
36+
// Check if the stdlib file exists in the installed package
37+
if (fs.existsSync(candidateStdlibPath)) {
38+
installedStdlibPath = candidateStdlibPath;
39+
console.log(`Found installed zenstack package stdlib at ${installedStdlibPath}`);
40+
break;
41+
}
42+
} catch (error) {
43+
// Package not found or other error, continue to next folder
44+
console.error(`error happen when trying to find stdlib in folder ${folder.uri}:`, error);
45+
continue;
46+
}
47+
}
48+
49+
if (installedStdlibPath) {
50+
stdLibPath = installedStdlibPath;
51+
} else {
52+
// Fallback to bundled stdlib
53+
stdLibPath = path.join(__dirname, '../res', STD_LIB_MODULE_NAME);
54+
console.log(`Using bundled stdlib in extension`);
55+
}
56+
57+
const stdLibUri = URI.file(stdLibPath);
2158
console.log(`Adding stdlib document from ${stdLibUri}`);
2259
const stdlib = this.langiumDocuments.getOrCreateDocument(stdLibUri);
2360
_collector(stdlib);

0 commit comments

Comments
 (0)