Description
Version
v18.19.0, v20.6.0
Platform
Darwin Kernel Version 23.2.0: Wed Nov 15 21:53:18 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6000 arm64
Subsystem
url
What steps will reproduce the bug?
import {pathToFileURL} from 'url';
const filename = `test-${String.fromCharCode(0)}`;
console.log(pathToFileURL(filename).toString()); // file:///[...]/test-
How often does it reproduce? Is there a required condition?
always
What is the expected behavior? Why is that the expected behavior?
In Node v18.18.2 and v20.5.1:
import {pathToFileURL} from 'url';
const filename = `test-${String.fromCharCode(0)}`;
console.log(pathToFileURL(filename).toString()); // file:///[...]/test-%00
What do you see instead?
Where prior versions retained the trailing whitespace and returned a pathname ending in /test-%00
, starting in v18.19.0 and v20.6.0 the trailing whitespace is removed and results in a pathname ending in /test-
.
Additional information
This regression was introduced by optimizations in #48709, specifically the switch from:
const outURL = new URL('file://');
outURL.pathname = encodePathChars(resolved);
return outURL;
To:
resolved = encodePathChars(resolved);
return new URL(`file://${resolved}`);
While these seem equivalent, new URL
uses parse
, which trims leading and trailing whitespace from the path. In contrast, the earlier method of setting pathname
directly does not trim whitespace.
I'm not sure which way is actually preferred, but regardless this was an unexpected (and undocumented) breaking change.
Activity