Skip to content

--preserve-symlinks and --experimental-specifier-resolution=node fails to find file without extension #47649

@znewsham

Description

@znewsham

Version

16.20

Platform

Linux ed7d7cb37749 5.19.0-38-generic #39~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 17 21:16:15 UTC 2 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

create two JS files:

// index.js
import "./test"
// test.js
// whatever

and a package.json

{ "type": "module" }

run:

node  --preserve-symlinks --experimental-specifier-resolution=node index.js

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior? Why is that the expected behavior?

It should be able to find the correct JS file without an extension

What do you see instead?

[Error: ENOENT: no such file or directory, open '/path/to/test'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/path/to/test'
}

Additional information

This worked correctly in node 14. I know this flag is going away in node 19, but I don't really want to have to implement an entire loader just to get extensionless loading working the same as it did in 14 - particularly irksome because the loader API changed between 14 and 16, so to support both versions of node I need to support both these experimental features.

Adding the .js extension isn't practical.

The error is caused by this code in internal/modules/esm/resolve

  if (!preserveSymlinks) {
    const real = realpathSync(path, {
      [internalFS.realpathCacheKey]: realpathCache
    });
    const { search, hash } = resolved;
    resolved =
        pathToFileURL(real + (StringPrototypeEndsWith(path, sep) ? '/' : ''));
    resolved.search = search;
    resolved.hash = hash;
  }

  return resolved;

there should probably be an else:

else { 
  resolved = path
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions