From 189a9376a0a95094d9e8bb3ceb7e2df0e6dca990 Mon Sep 17 00:00:00 2001 From: Christian Schneider Date: Fri, 21 Feb 2025 15:29:11 +0100 Subject: [PATCH] more supplements --- packages/langium/src/utils/uri-utils.ts | 12 ++++++++---- packages/langium/test/utils/uri-utils.test.ts | 6 ++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/langium/src/utils/uri-utils.ts b/packages/langium/src/utils/uri-utils.ts index 11d37e155..8fa081e1b 100644 --- a/packages/langium/src/utils/uri-utils.ts +++ b/packages/langium/src/utils/uri-utils.ts @@ -16,17 +16,17 @@ export namespace UriUtils { export const joinPath = Utils.joinPath; export const resolvePath = Utils.resolvePath; - const isWindows = process?.platform === 'win32'; + const isWindows = typeof process === 'object' && process?.platform === 'win32'; export function equals(a?: URI | string, b?: URI | string): boolean { return a?.toString() === b?.toString(); } export function relative(from: URI | string, to: URI | string): string { - const fromPath = typeof from === 'string' ? from : from.path; - const toPath = typeof to === 'string' ? to : to.path; + const fromPath = typeof from === 'string' ? URI.parse(from).path : from.path; + const toPath = typeof to === 'string' ? URI.parse(to).path : to.path; const fromParts = fromPath.split('/').filter(e => e.length > 0); - const toParts = toPath.split('/').filter(e => e.length > 0); + const toParts = toPath.split('/').filter(e => e.length > 0); if (isWindows) { const upperCaseDriveLetter = /^[A-Z]:$/; @@ -36,6 +36,10 @@ export namespace UriUtils { if (toParts[0] && upperCaseDriveLetter.test(toParts[0])) { toParts[0] = toParts[0].toLowerCase(); } + if (fromParts[0] !== toParts[0]) { + // in case of different drive letters, we cannot compute a relative path, so... + return toPath.substring(1); // fall back to full 'to' path, drop the leading '/', keep everything else as is for good comparability + } } let i = 0; diff --git a/packages/langium/test/utils/uri-utils.test.ts b/packages/langium/test/utils/uri-utils.test.ts index 8427db58e..845f6fc48 100644 --- a/packages/langium/test/utils/uri-utils.test.ts +++ b/packages/langium/test/utils/uri-utils.test.ts @@ -69,6 +69,12 @@ describe('URI Utils', () => { expect(UriUtils.relative(from, to)).toBe('../c/d.txt'); }); + test.skipIf(process.platform !== 'win32')('different win32 drive letters', () => { + const from = URI.file('c:\\a\\b'); + const to = URI.file('D:\\a\\c\\d.txt'); + expect(UriUtils.relative(from, to)).toBe('d:/a/c/d.txt'); + }); + test('Equal uris are equal', () => { const uri1 = 'file:///a/b'; const uri2 = 'file:///a/b';