deno
deno copied to clipboard
fix(deno_node/require): Fix error with require.resolve when called with paths option
When running the following code from a node module, an error occurs:
require.resolve('anything', {paths:['any path']})
Error: Empty filepath.
at pathDirname (deno:ext/node/02_require.js:50:13)
at new Module (deno:ext/node/02_require.js:257:17)
at Function.Module._resolveFilename (deno:ext/node/02_require.js:566:30)
at Function.resolve (deno:ext/node/02_require.js:824:21)
This is caused by using an empty string for the fake package's id:
https://github.com/denoland/deno/blob/a09296322e1fd4451e903515a497d9e02e14ace6/ext/node/02_require.js#L566
while at the same time enforcing package id to be non-empty:
https://github.com/denoland/deno/blob/a09296322e1fd4451e903515a497d9e02e14ace6/ext/node/02_require.js#L48-L51
It looks like pathDirname
is an implementation for path.dirname
, but the real one returns "."
when an empty string is passed.
This PR fixes the issue by returning "."
for ""
.
Related issue: https://github.com/denoland/deno/issues/17175 https://github.com/denoland/deno/issues/17176
Thanks for the PR @b123400, could you add a test case that exercises this code path?
@bartlomieju Thanks for the reply. I am not familar with Deno, can you point to me where should I look / add a test case? The related test cases I can find are from deno_std, however it's testing the Module implementation in deno_std instead of the one in this repository. Why are there 2 implementations?
@bartlomieju Thanks for the reply. I am not familar with Deno, can you point to me where should I look / add a test case? The related test cases I can find are from deno_std, however it's testing the Module implementation in deno_std instead of the one in this repository. Why are there 2 implementations?
The Node compat code recently moved from the deno_std
repository into this repository. If you look at the main branch of deno_std
you'll see that those files are no longer there. Though, I am not sure where the module_test.ts
is now...
This is fixed in #18447