diff --git a/libs/interpreter-lib/src/parsing-util.ts b/libs/interpreter-lib/src/parsing-util.ts index f3b1473d..e12fd97e 100644 --- a/libs/interpreter-lib/src/parsing-util.ts +++ b/libs/interpreter-lib/src/parsing-util.ts @@ -10,7 +10,7 @@ import { type JayveeServices, initializeWorkspace, } from '@jvalue/jayvee-language-server'; -import { type AstNode, type LangiumDocument } from 'langium'; +import { type AstNode, type LangiumDocument, UriUtils } from 'langium'; import { type LangiumServices } from 'langium/lsp'; import { DiagnosticSeverity } from 'vscode-languageserver-protocol'; import { URI } from 'vscode-uri'; @@ -44,9 +44,9 @@ export async function extractDocumentFromFile( return Promise.reject(ExitCode.FAILURE); } - const document = services.shared.workspace.LangiumDocuments.getDocument( - URI.file(path.resolve(filePath)), - ); + const fileUri = getFileUriLikeLangiumImpl(filePath); + const document = + services.shared.workspace.LangiumDocuments.getDocument(fileUri); if (document === undefined) { logger.logErr(`Did not load file ${filePath} correctly.`); return Promise.reject(ExitCode.FAILURE); @@ -55,6 +55,20 @@ export async function extractDocumentFromFile( return await validateDocument(document, services, logger); } +/** + * Creates the URI for a file path in a way similar to Langium. + * This is necessary to make sure that the document lookup works on Windows. + * Fixed https://github.com/jvalue/jayvee/issues/623. + * Workaround needs to be removed once the issue is fixed in Langium: + * https://github.com/eclipse-langium/langium/issues/1725 + */ +function getFileUriLikeLangiumImpl(filePath: string): URI { + const folderPath = path.dirname(filePath); + const folderUri = URI.parse(path.resolve(folderPath)); + const fileName = path.basename(filePath); + return UriUtils.joinPath(folderUri, fileName); +} + /** * Extracts a document from a string that contains a model. * Does not load an additional working directory.