Skip to content

Commit 94f9d2a

Browse files
authored
fix(vscode): try to load stdlib firstly from the installed zenstack package (#2148)
1 parent 704fb24 commit 94f9d2a

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

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

Lines changed: 37 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,42 @@ 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 = this.getRootFolder(folder).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+
continue;
45+
}
46+
}
47+
48+
if (installedStdlibPath) {
49+
stdLibPath = installedStdlibPath;
50+
} else {
51+
// Fallback to bundled stdlib
52+
stdLibPath = path.join(__dirname, '../res', STD_LIB_MODULE_NAME);
53+
console.log(`Using bundled stdlib in extension`);
54+
}
55+
56+
const stdLibUri = URI.file(stdLibPath);
2157
console.log(`Adding stdlib document from ${stdLibUri}`);
2258
const stdlib = this.langiumDocuments.getOrCreateDocument(stdLibUri);
2359
_collector(stdlib);

0 commit comments

Comments
 (0)