Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modules imported dynamically from an absolute path are not loaded/handled by tsx v4.9.1 #543

Closed
5 of 6 tasks
fardjad opened this issue May 5, 2024 · 3 comments
Closed
5 of 6 tasks
Labels
bug Something isn't working pending triage

Comments

@fardjad
Copy link
Contributor

fardjad commented May 5, 2024

Acknowledgements

  • I read the documentation and searched existing issues to avoid duplicates
  • I understand this is a place to report a confirmed bug (not seek debugging help)
  • I understand this is a collaborative open source project, and relies on community contributions
  • I have read and understood the Contribution guide

Minimal reproduction URL

https://stackblitz.com/edit/tsx-4-9-1-import-from-absolute-path-issue

Version

4.9.1

Node.js version

v20.12.2

Package manager

npm

Operating system

macOS

Problem & expected behavior (under 200 words)

  • What happened

The newly released version v4.9.1 doesn't handle absolute paths (4.9.0 doesn't have this issue).

  • What I expected

I expected to be able to run the following with tsx index.mts:

// index.mts

import path from "node:path";
import { fileURLToPath } from "node:url";
import fs from "node:fs";

const __dirname = path.dirname(fileURLToPath(new URL(import.meta.url)));
const moduleRelativePath = "./hello.mts";
const moduleAbsolutePath = path.resolve(__dirname, moduleRelativePath);

fs.writeFileSync(moduleAbsolutePath, `export const message = "Hello, world!";`);

await import(moduleAbsolutePath); // Doesn't work
  • Error stack trace
node:internal/process/esm_loader:34
      internalBinding('errors').triggerUncaughtException(
                                ^
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".mts" for <redacted>/hello.mts
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:160:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:203:36)
    at defaultLoad (node:internal/modules/esm/load:143:22)
    at async nextLoad (node:internal/modules/esm/hooks:866:22)
    at async x (file:///<redacted>/.npm/_npx/dcefadc2074ecd1f/node_modules/tsx/dist/esm/index.mjs?1714902597374:2:1744)
    at async nextLoad (node:internal/modules/esm/hooks:866:22)
    at async Hooks.load (node:internal/modules/esm/hooks:449:20)
    at async handleMessage (node:internal/modules/esm/worker:196:18) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
  • Documentation links

It happens because an absolute path is not considered a path in the custom resolver and then the early return at the end of the if block skips the resolution:

tsx/src/esm/hook/resolve.ts

Lines 130 to 150 in 96bc596

if (!isPath) {
// TS path alias
if (
tsconfigPathsMatcher
&& !context.parentURL?.includes('/node_modules/')
) {
const possiblePaths = tsconfigPathsMatcher(specifier);
for (const possiblePath of possiblePaths) {
try {
return await resolve(
pathToFileURL(possiblePath).toString(),
context,
nextResolve,
);
} catch {}
}
}
// npm package -- use default resolution
return nextResolve(specifier, context);
}

  • Screenshots

image

Contributions

  • I'm interested in working on this issue
  • I'm willing to offer financial support
@hatemjaber

This comment was marked as spam.

@privatenumber
Copy link
Owner

privatenumber commented May 6, 2024

@hatemjaber As per the contribution guide, please refrain from "me too" comments as they're not constructive towards closing the issue.


Fixed via #544

@hatemjaber

This comment was marked as spam.

Repository owner locked and limited conversation to collaborators May 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working pending triage
Projects
None yet
Development

No branches or pull requests

3 participants