deno icon indicating copy to clipboard operation
deno copied to clipboard

fix(deno_node/require): Fix error with require.resolve when called with paths option

Open b123400 opened this issue 2 years ago • 3 comments

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

b123400 avatar Feb 05 '23 13:02 b123400

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Feb 05 '23 13:02 CLAassistant

Thanks for the PR @b123400, could you add a test case that exercises this code path?

bartlomieju avatar Feb 06 '23 22:02 bartlomieju

@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?

b123400 avatar Feb 07 '23 06:02 b123400

@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...

aapoalas avatar Mar 14 '23 20:03 aapoalas

This is fixed in #18447

b123400 avatar Mar 30 '23 06:03 b123400