From c775d6f98959062c939dfda9204f71a1ae26960f Mon Sep 17 00:00:00 2001 From: jiasheng Date: Fri, 13 Jun 2025 20:37:23 +0800 Subject: [PATCH 1/2] fix(vscode): try to load stdlib firstly from the installed zenstack package --- .../zmodel-workspace-manager.ts | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/schema/src/language-server/zmodel-workspace-manager.ts b/packages/schema/src/language-server/zmodel-workspace-manager.ts index 734a785cd..88c2bd4eb 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,43 @@ 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 = URI.parse(folder.uri).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 + console.error(`error happen when trying to find stdlib in folder ${folder.uri}:`, error); + 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); From 8a72be5150ea3697ddbffac3c7ce8414dee6953e Mon Sep 17 00:00:00 2001 From: jiasheng Date: Fri, 13 Jun 2025 20:51:06 +0800 Subject: [PATCH 2/2] fix: don't show the error log when can't find installed stdlib --- .../schema/src/language-server/zmodel-workspace-manager.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/schema/src/language-server/zmodel-workspace-manager.ts b/packages/schema/src/language-server/zmodel-workspace-manager.ts index 88c2bd4eb..d8ba7bd8a 100644 --- a/packages/schema/src/language-server/zmodel-workspace-manager.ts +++ b/packages/schema/src/language-server/zmodel-workspace-manager.ts @@ -24,7 +24,7 @@ export class ZModelWorkspaceManager extends DefaultWorkspaceManager { // in the project's node_modules let installedStdlibPath: string | undefined; for (const folder of _folders) { - const folderPath = URI.parse(folder.uri).fsPath; + const folderPath = this.getRootFolder(folder).fsPath; try { // Try to resolve zenstack from the workspace folder const languagePackagePath = require.resolve('zenstack/package.json', { @@ -41,7 +41,6 @@ export class ZModelWorkspaceManager extends DefaultWorkspaceManager { } } catch (error) { // Package not found or other error, continue to next folder - console.error(`error happen when trying to find stdlib in folder ${folder.uri}:`, error); continue; } }