From 0bacc35a36da684ce14385b433c2b720bf09e670 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Mon, 24 Mar 2025 22:17:14 -0400 Subject: [PATCH] fix(@angular/build): normalize karma asset paths before lookup When handling a request for a configured asset with the application- based karma unit testing, the asset URL is now normalized to the executing platform's path format before looking up the asset. This is required due to the build file paths being based on the underlying operating system's paths which may not align with a URL's path separator such as when using Windows. --- .../build/src/builders/karma/application_builder.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/angular/build/src/builders/karma/application_builder.ts b/packages/angular/build/src/builders/karma/application_builder.ts index 7651ecaaff5d..eb724ba00700 100644 --- a/packages/angular/build/src/builders/karma/application_builder.ts +++ b/packages/angular/build/src/builders/karma/application_builder.ts @@ -25,6 +25,7 @@ import { findTests, getTestEntrypoints } from './find-tests'; import { Schema as KarmaBuilderOptions } from './schema'; const localResolve = createRequire(__filename).resolve; +const isWindows = process.platform === 'win32'; interface BuildOptions extends ApplicationBuilderInternalOptions { // We know that it's always a string since we set it. @@ -69,7 +70,14 @@ class AngularAssetsMiddleware { let err = null; try { const url = new URL(`http://${req.headers['host']}${req.url}`); - const file = this.latestBuildFiles.files[url.pathname.slice(1)]; + // Remove the leading slash from the URL path and convert to platform specific. + // The latest build files will use the platform path separator. + let pathname = url.pathname.slice(1); + if (isWindows) { + pathname = pathname.replaceAll(path.posix.sep, path.win32.sep); + } + + const file = this.latestBuildFiles.files[pathname]; if (file?.origin === 'disk') { this.serveFile(file.inputPath, undefined, res);