diff --git a/packages/build-utils/src/clone-env.ts b/packages/build-utils/src/clone-env.ts index f6608d39d..95bbcd027 100644 --- a/packages/build-utils/src/clone-env.ts +++ b/packages/build-utils/src/clone-env.ts @@ -25,7 +25,11 @@ export function cloneEnv(...envs: (Env | undefined)[]): Env { // however we lose this proxied value when we destructure and // thus we must explicitly copy it, but we must also remove the // `Path` property since we can't have both a `PATH` and `Path` - obj.PATH = obj.Path; + + if (obj.Path !== undefined) { + obj.PATH = obj.Path; + } + delete obj.Path; } diff --git a/packages/build-utils/test/unit.clone-env.test.ts b/packages/build-utils/test/unit.clone-env.test.ts index dbc316f4d..cccc72218 100644 --- a/packages/build-utils/test/unit.clone-env.test.ts +++ b/packages/build-utils/test/unit.clone-env.test.ts @@ -36,6 +36,31 @@ it('should clone env with PATH', () => { }); }); +it('should not overwrite PATH when path is undefined', () => { + expect( + cloneEnv( + { + PATH: 'baz', + }, + new Proxy( + { + Path: undefined, + }, + { + get(target: typeof process.env, prop: string) { + if (prop === 'PATH') { + return target.PATH ?? target.Path; + } + return target[prop]; + }, + } + ) + ) + ).toEqual({ + PATH: 'baz', + }); +}); + it('should clone and merge multiple env objects', () => { // note: this also tests the last object doesn't overwrite `PATH` with // `undefined`