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

[ESM / Node v22] Broken import of a correctly bundled library #553

Closed
4 tasks done
damianobarbati opened this issue May 14, 2024 · 3 comments
Closed
4 tasks done

Comments

@damianobarbati
Copy link

damianobarbati commented May 14, 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://github.com/damianobarbati/tsx-bug

How to reproduce:

git clone https://github.com/damianobarbati/tsx-bug
cd tsx-bug
nvm install 
npm install -g yarn
yarn install
yarn start

Result:

$ yarn start
{
  default: [Function: MapsSearch],
  isUnexpected: [Function: isUnexpected]
} [Function: isUnexpected]

Expected:

$ yarn start
[Function: MapsSearch], [Function: isUnexpected]

Code is simple:

import MapsSearch, { isUnexpected } from '@azure-rest/maps-search';
console.log(MapsSearch, isUnexpected);

Tried both the following tsconfig.json:

{
  "compilerOptions": {
    "target": "esnext"
  }
}

and

{
  "compilerOptions": {
    "target": "esnext",
    "moduleResolution": "NodeNext"
  }
}

Version

v4.10.2

Node.js version

v22.1

Package manager

yarn

Operating system

macOS

@damianobarbati damianobarbati added bug Something isn't working pending triage labels May 14, 2024
@privatenumber
Copy link
Owner

privatenumber commented May 14, 2024

Your reproduction seems to show a different error than what you're reporting:

$ pnpm start

> tsx-starter@ start ~/Downloads/node-cfvrct
> NODE_ENV=development ENVK=.env tsx --no-warnings -r envk --watch ./index.ts


~/Downloads/node-cfvrct/node_modules/.pnpm/@esbuild-kit+cjs-loader@2.4.0/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1
"use strict";var F=require("path"),N=require("fs"),S=require("module"),r=require("@esbuild-kit/core-utils"),l=require("get-tsconfig");function f(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var m=f(F),y=f(N),d=f(S);const P=/^\.{0,2}\//,E=/\.[cm]?tsx?$/,O=`${m.default.sep}node_modules${m.default.sep}`,i=process.env.ESBK_TSCONFIG_PATH?{path:process.env.ESBK_TSCONFIG_PATH,config:l.parseTsconfig(process.env.ESBK_TSCONFIG_PATH)}:l.getTsconfig(),b=i==null?void 0:i.config,_=i&&l.createPathsMatcher(i),h=r.installSourceMapSupport(),j=r.compareNodeVersion([13,2,0])>=0||r.compareNodeVersion([12,20,0])>=0&&r.compareNodeVersion([13,0,0])<0;function v(e,s){process.send&&process.send({type:"dependency",path:s});let t=y.default.readFileSync(s,"utf8");if(s.endsWith(".cjs")&&j){const o=r.transformDynamicImport(s,t);o&&(t=h(o,s))}else{const o=r.transformSync(t,s,{tsconfigRaw:b});t=h(o,s)}e._compile(t,s)}const T=d.default._extensions;[".js",".ts",".tsx",".jsx"].forEach(e=>{T[e]=v}),Object.defineProperty(T,".mjs",{value:v,enumerable:!1});const x=r.compareNodeVersion([16,0,0])>=0||r.compareNodeVersion([14,18,0])>=0,u=d.default._resolveFilename;d.default._resolveFilename=function(e,s,t,o){if(!x&&e.startsWith("node:")&&(e=e.slice(5)),_&&!P.test(e)&&!(s!=null&&s.filename.includes(O))){const a=_(e);for(const c of a){const p=g.call(this,c,s,t,o);if(p)return p;try{return u.call(this,c,s,t,o)}catch{}}}const n=g.call(this,e,s,t,o);return n||u.call(this,e,s,t,o)};function g(e,s,t,o){const n=r.resolveTsPath(e);if(s&&E.test(s.filename)&&n)try{return u.call(this,n,s,t,o)}catch(a){const{code:c}=a;if(c!=="MODULE_NOT_FOUND"&&c!=="ERR_PACKAGE_PATH_NOT_EXPORTED")throw a}}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ^
TypeError: Cannot read properties of null (reading 'includes')
    at Function.d.default._resolveFilename (~/Downloads/node-cfvrct/node_modules/.pnpm/@esbuild-kit+cjs-loader@2.4.0/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:1284)
    at Function.Module._load (node:internal/modules/cjs/loader:1050:27)
    at internalRequire (node:internal/modules/cjs/loader:213:19)
    at Module._preloadModules (node:internal/modules/cjs/loader:1711:5)
    at loadPreloadModules (node:internal/process/pre_execution:744:5)
    at setupUserModules (node:internal/process/pre_execution:207:5)
    at prepareExecution (node:internal/process/pre_execution:160:5)
    at prepareMainThreadExecution (node:internal/process/pre_execution:54:10)
    at node:internal/main/run_main_module:13:19
Failed running './index.ts'
^C%
$ node -v
v22.1.

Is there a reason why you're using an outdated version of tsx (v3.12.1) when your report says v4.10.2? And are all the files and deps necessary? For example @types/node can definitely be removed.

Providing a minimal reproduction is important for saving each other time. Please let me know when it's ready and I'll re-open this issue.

@privatenumber privatenumber closed this as not planned Won't fix, can't repro, duplicate, stale May 14, 2024
@damianobarbati
Copy link
Author

damianobarbati commented May 14, 2024

@privatenumber sorry for this, I created a proper repro on Github as I can't enforce node version on StackBlitz.
I updated the description of the issue accordingly.
You should be able to reproduce copy-pasting the commands assuming you have nvm.
Let me know if I can help further.

@privatenumber
Copy link
Owner

Duplicate of #38

@privatenumber privatenumber marked this as a duplicate of #38 May 14, 2024
Repository owner locked and limited conversation to collaborators May 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants