diff --git a/packages/schema/src/language-server/zmodel-workspace-manager.ts b/packages/schema/src/language-server/zmodel-workspace-manager.ts index 734a785cd..d8ba7bd8a 100644 --- a/packages/schema/src/language-server/zmodel-workspace-manager.ts +++ b/packages/schema/src/language-server/zmodel-workspace-manager.ts @@ -1,6 +1,7 @@ import { isPlugin, Model } from '@zenstackhq/language/ast'; import { getLiteral } from '@zenstackhq/sdk'; import { DefaultWorkspaceManager, interruptAndCheck, LangiumDocument } from 'langium'; +import fs from 'fs'; import path from 'path'; import { CancellationToken, WorkspaceFolder } from 'vscode-languageserver'; import { URI, Utils } from 'vscode-uri'; @@ -17,7 +18,42 @@ export class ZModelWorkspaceManager extends DefaultWorkspaceManager { _collector: (document: LangiumDocument) => void ): Promise { await super.loadAdditionalDocuments(_folders, _collector); - const stdLibUri = URI.file(path.join(__dirname, '../res', STD_LIB_MODULE_NAME)); + + let stdLibPath: string; + // First, try to find the stdlib from an installed zenstack package + // in the project's node_modules + let installedStdlibPath: string | undefined; + for (const folder of _folders) { + const folderPath = this.getRootFolder(folder).fsPath; + try { + // Try to resolve zenstack from the workspace folder + const languagePackagePath = require.resolve('zenstack/package.json', { + paths: [folderPath] + }); + const languagePackageDir = path.dirname(languagePackagePath); + const candidateStdlibPath = path.join(languagePackageDir, 'res', STD_LIB_MODULE_NAME); + + // Check if the stdlib file exists in the installed package + if (fs.existsSync(candidateStdlibPath)) { + installedStdlibPath = candidateStdlibPath; + console.log(`Found installed zenstack package stdlib at ${installedStdlibPath}`); + break; + } + } catch (error) { + // Package not found or other error, continue to next folder + continue; + } + } + + if (installedStdlibPath) { + stdLibPath = installedStdlibPath; + } else { + // Fallback to bundled stdlib + stdLibPath = path.join(__dirname, '../res', STD_LIB_MODULE_NAME); + console.log(`Using bundled stdlib in extension`); + } + + const stdLibUri = URI.file(stdLibPath); console.log(`Adding stdlib document from ${stdLibUri}`); const stdlib = this.langiumDocuments.getOrCreateDocument(stdLibUri); _collector(stdlib);