1
1
import { isPlugin , Model } from '@zenstackhq/language/ast' ;
2
2
import { getLiteral } from '@zenstackhq/sdk' ;
3
3
import { DefaultWorkspaceManager , interruptAndCheck , LangiumDocument } from 'langium' ;
4
+ import fs from 'fs' ;
4
5
import path from 'path' ;
5
6
import { CancellationToken , WorkspaceFolder } from 'vscode-languageserver' ;
6
7
import { URI , Utils } from 'vscode-uri' ;
@@ -17,7 +18,42 @@ export class ZModelWorkspaceManager extends DefaultWorkspaceManager {
17
18
_collector : ( document : LangiumDocument ) => void
18
19
) : Promise < void > {
19
20
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 ) ;
21
57
console . log ( `Adding stdlib document from ${ stdLibUri } ` ) ;
22
58
const stdlib = this . langiumDocuments . getOrCreateDocument ( stdLibUri ) ;
23
59
_collector ( stdlib ) ;
0 commit comments