-
-
Notifications
You must be signed in to change notification settings - Fork 75
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
fix: esm module resolve issues #754
Conversation
this commit convert fileUrl to path before compile in esm, this may cause path alias resolve issue close swc-project#753
🦋 Changeset detectedLatest commit: bd4e32f The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@yeliex Hi I have a similar issue, but when the resolve's diff --git a/esm/esm.mjs b/esm/esm.mjs
index 8453ae2480391c8eb5cc18d5460bd55b723592ce..a62fa5753df9a00f9680adc8dea7b005dea82f6e 100644
--- a/esm/esm.mjs
+++ b/esm/esm.mjs
@@ -13,7 +13,8 @@ const host = {
};
const EXTENSIONS = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts];
export const resolve = async (specifier, context, nextResolve) => {
- const isTS = EXTENSIONS.some((ext) => specifier.endsWith(ext));
+ const modulePath = specifier.startsWith('file://') ? fileURLToPath(specifier) : specifier;
+ const isTS = EXTENSIONS.some((ext) => modulePath.endsWith(ext));
// entrypoint
if (!context.parentURL) {
return {
@@ -26,7 +27,7 @@ export const resolve = async (specifier, context, nextResolve) => {
if (context.parentURL.includes('/node_modules/') && !isTS) {
return nextResolve(specifier);
}
- const { resolvedModule } = ts.resolveModuleName(specifier, fileURLToPath(context.parentURL), tsconfig, host, moduleResolutionCache);
+ const { resolvedModule } = ts.resolveModuleName(modulePath, fileURLToPath(context.parentURL), tsconfig, host, moduleResolutionCache);
// import from local project to local project TS file
if (resolvedModule &&
!resolvedModule.resolvedFileName.includes('/node_modules/') && |
@psychobolt This caused a ci failure, maybe a complication issue with @swc/core. I am trying to resolve this, and filling more test cases |
The pr should fix these issues as well, but not test for each. feedback required. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for looking into this @yeliex. I tried this against my original bug report and can confirm that this runs as expected with the newer --import
loader. However it unfortunately also breaks the original register method.
Error: failed to handle: base_dir(``) must be absolute. Please ensure that
jsc.baseUrl
is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other words, SWC does not know which directory should be used as a base directory. It can be deduced if.swcrc
is used, but if not, there are many candidates. e.g. the directory containingpackage.json
, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it.
Full stack trace
❯ pn esm
> swc-node-esm-register-issue@ esm /Users/myles/dev/issue-swc-node-1
> node --import @swc-node/register/esm-register src/index.ts
hello
❯ pn cjs
> swc-node-esm-register-issue@ cjs /Users/myles/dev/issue-swc-node-1
> node -r @swc-node/register src/index.ts
thread '<unnamed>' panicked at /Users/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swc_ecma_transforms_module-0.179.5/src/path.rs:122:13:
base_dir(``) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other words, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
/Users/myles/dev/issue-swc-node-1/node_modules/.pnpm/@[email protected]/node_modules/@swc/core/index.js:244
return bindings.transformSync(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
^
Error: failed to handle: base_dir(``) must be absolute. Please ensure that `jsc.baseUrl` is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other words, SWC does not know which directory should be used as a base directory. It can be deduced if `.swcrc` is used, but if not, there are many candidates. e.g. the directory containing `package.json`, or the current working directory. Because of that, the caller (typically the developer of the JavaScript package) should specify it. If you see this error, please report an issue to the package author.
at Compiler.transformSync (/Users/myles/dev/issue-swc-node-1/node_modules/.pnpm/@[email protected]/node_modules/@swc/core/index.js:244:29)
at transformSync (/Users/myles/dev/issue-swc-node-1/node_modules/.pnpm/@[email protected]/node_modules/@swc/core/index.js:351:21)
at transformSync (/Users/myles/dev/issue-swc-node-1/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]/node_modules/@swc-node/core/index.ts:90:26)
at compile (/Users/myles/dev/issue-swc-node-1/node_modules/.pnpm/file+.yalc+@swc-node+register_@[email protected]_@[email protected][email protected]/node_modules/@swc-node/register/register.ts:105:40)
at exts (/Users/myles/dev/issue-swc-node-1/node_modules/.pnpm/file+.yalc+@swc-node+register_@[email protected]_@[email protected][email protected]/node_modules/@swc-node/register/register.ts:116:38)
at Module._compile (/Users/myles/dev/issue-swc-node-1/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:113:29)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Object.newLoader [as .ts] (/Users/myles/dev/issue-swc-node-1/node_modules/.pnpm/[email protected]/node_modules/pirates/lib/index.js:121:7)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Function.Module._load (node:internal/modules/cjs/loader:1023:12) {
code: 'GenericFailure'
}
Node.js v20.11.0
ELIFECYCLE Command failed with exit code 1.
@yeliex I am willing to test this on our project. Any instructions on how I can install the module from this pr? Or is it already published ? |
install package as git url or link locally |
it looks not the same issue. i would looking this later today |
@mylesj add |
Thanks that does indeed address the issue locally, I just mentioned it to highlight the breaking API change. TypeScript documents the following under the
|
I have created a new PR to fix this, keeping the behavior with I Agree that it is better for |
So, there's still some issue with resolves. TS paths now work correctly which is awesome but now deep imports from
|
@alfaproject could u please provide a minimal reproduction |
@yeliex the problem seems to be lack of |
maybe the same as packages/register/test/ts-compiler-options-to-swc-config.spec.ts check your |
I get the same error with or without I really appreciate your help but I think I'll need to drop this for the time being. I couldn't get our Jest tests to work with full ESM either so either way, we are kind of blocked in having everything ESM first, unfortunately |
This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [@swc-node/register](https://github.com/swc-project/swc-node) | [`1.9.0` -> `1.9.1`](https://renovatebot.com/diffs/npm/@swc-node%2fregister/1.9.0/1.9.1) | [![age](https://developer.mend.io/api/mc/badges/age/npm/@swc-node%2fregister/1.9.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/@swc-node%2fregister/1.9.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/@swc-node%2fregister/1.9.0/1.9.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@swc-node%2fregister/1.9.0/1.9.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>swc-project/swc-node (@​swc-node/register)</summary> ### [`v1.9.1`](https://github.com/swc-project/swc-node/releases/tag/%40swc-node/register%401.9.1) [Compare Source](https://github.com/swc-project/swc-node/compare/@swc-node/[email protected]...@swc-node/[email protected]) #### What's Changed - fix: esm module resolve issues by [@​yeliex](https://github.com/yeliex) in [https://github.com/swc-project/swc-node/pull/754](https://github.com/swc-project/swc-node/pull/754) - fix: add default tsconfig.baseUrl to align with tsc behavior by [@​yeliex](https://github.com/yeliex) in [https://github.com/swc-project/swc-node/pull/759](https://github.com/swc-project/swc-node/pull/759) - fix(register): fix esm entry resolver for third-party executer, close [#​762](https://github.com/swc-project/swc-node/issues/762) by [@​yeliex](https://github.com/yeliex) in [https://github.com/swc-project/swc-node/pull/766](https://github.com/swc-project/swc-node/pull/766) - fix: support compile js files. close [#​761](https://github.com/swc-project/swc-node/issues/761) by [@​yeliex](https://github.com/yeliex) in [https://github.com/swc-project/swc-node/pull/767](https://github.com/swc-project/swc-node/pull/767) - chore: upgrade dependencies by [@​Brooooooklyn](https://github.com/Brooooooklyn) in [https://github.com/swc-project/swc-node/pull/771](https://github.com/swc-project/swc-node/pull/771) **Full Changelog**: https://github.com/swc-project/swc-node/compare/[@​swc-node/register](https://github.com/swc-node/register)[@​1](https://github.com/1).8.0...[@​swc-node/register](https://github.com/swc-node/register)[@​1](https://github.com/1).9.1 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zNDAuNiIsInVwZGF0ZWRJblZlciI6IjM3LjM0MC42IiwidGFyZ2V0QnJhbmNoIjoiZGV2ZWxvcCIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiXX0=--> Co-authored-by: tsang-bot[bot] <146107447+tsang-bot[bot]@users.noreply.github.com>
this commit fix some esm module resolve bugs
@swc-node/[email protected]
/esm-register
does not recognisetsconfig.json
paths #753