From a70c111c0c98924d5026c317cbb6de2cb7dddeba Mon Sep 17 00:00:00 2001 From: MxKevinBeqo Date: Thu, 23 Apr 2026 15:09:23 +0200 Subject: [PATCH] fix: consider absolute paths when resolving normalPath --- packages/metro-file-map/src/lib/RootPathUtils.js | 3 +++ .../src/lib/__tests__/RootPathUtils-test.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/packages/metro-file-map/src/lib/RootPathUtils.js b/packages/metro-file-map/src/lib/RootPathUtils.js index 5720d60ee9..cf84c7d48e 100644 --- a/packages/metro-file-map/src/lib/RootPathUtils.js +++ b/packages/metro-file-map/src/lib/RootPathUtils.js @@ -150,6 +150,9 @@ export class RootPathUtils { if (right.length === 0) { return left; } + if (path.isAbsolute(right)) { + return right; + } // left may already end in a path separator only if it is a filesystem root, // '/' or 'X:\'. if (i === this.#rootDepth) { diff --git a/packages/metro-file-map/src/lib/__tests__/RootPathUtils-test.js b/packages/metro-file-map/src/lib/__tests__/RootPathUtils-test.js index 8fb693c498..6475058098 100644 --- a/packages/metro-file-map/src/lib/__tests__/RootPathUtils-test.js +++ b/packages/metro-file-map/src/lib/__tests__/RootPathUtils-test.js @@ -110,6 +110,20 @@ describe.each([['win32'], ['posix']])('RootPathUtils on %s', platform => { expect(pathUtils.normalToAbsolute(normalPath)).toEqual(expected); }); + if (platform === 'win32') { + test.each(['D:\\some\\file.js', 'D:\\some\\', 'D:\\'])( + `normalToAbsolute('%s') returns cross-drive absolute normal path as-is`, + normalPath => { + // On Windows, path.relative() returns an absolute path when source + // and target are on different drives. Such paths are stored as the + // "normal path" in the file map and must be returned as-is by + // normalToAbsolute rather than being prepended with rootDir (which + // would produce invalid paths like C:\project\root\D:\file.js). + expect(pathUtils.normalToAbsolute(normalPath)).toEqual(normalPath); + }, + ); + } + test.each([ p('..'), p('../root'),