privatenumber / tsx

⚡️ TypeScript Execute | The easiest way to run TypeScript in Node.js
https://tsx.is
MIT License
9.53k stars 146 forks source link

tsx transpile failed for certain import #183

Closed wenerme closed 1 year ago

wenerme commented 1 year ago

Bug description

This code cause error

import { getBaseUrl } from 'common/src/runtime';

console.log(getBaseUrl());

but if I copy the target module and import like ./withBasePath it works, I expect the tsx will do transpile.

Reproduction

git clone https://github.com/wenerme/wode
cd wode
pnpm i
cd apps/apis

pnpm tsx ./src/t.ts 

ERROR

~/node_modules/.pnpm/@esbuild-kit+core-utils@3.0.0/node_modules/@esbuild-kit/core-utils/dist/index.js:9

EDIT

seems only common/src/runtime import failed, other import will not

import { WellKnownSchemas } from 'common/src/schemas';

console.log(Object.keys(WellKnownSchemas));

Environment

System:
    OS: macOS 13.2
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
    Memory: 3.44 GB / 32.00 GB
    Shell: 5.0.17 - /usr/local/bin/bash
  Binaries:
    Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
    npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm

Can you work on a fix?

privatenumber commented 1 year ago

Happy to help but please provide a minimal reproduction. Not a link to your large codebase.

wenerme commented 1 year ago

Sorry, failed to repro in a minimal project, but can repro by

git clone --depth 1 git@github.com:wenerme/wode.git
cd wode/
pnpm i
cd apps/apis
pnpm tsx ./src/t.ts
$HOME/wode/node_modules/.pnpm/@esbuild-kit+core-utils@3.0.0/node_modules/@esbuild-kit/core-utils/dist/index.js:9
`?o=!0:f!=="\r"&&o&&(o=!1,g===h.start||(this._splitChunk(h,g),h=h.next),h.prependRight(t))}g+=1}g=h.end,h=h.next}return this.outro=this.outro.replace(n,a),this}insert(){throw new Error("magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)")}insertLeft(t,A){return G.insertLeft||(console.warn("magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead"),G.insertLeft=!0),this.appendLeft(t,A)}insertRight(t,A){return G.insertRight||(console.warn("magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead"),G.insertRight=!0),this.prependRight(t,A)}move(t,A,n){if(n>=t&&n<=A)throw new Error("Cannot move a selection inside itself");this._split(t),this._split(A),this._split(n);const s=this.byStart[t],o=this.byEnd[A],a=s.previous,g=o.next,h=this.byStart[n];if(!h&&o===this.lastChunk)return this;const C=h?h.previous:this.lastChunk;return a&&(a.next=g),g&&(g.previous=a),C&&(C.next=s),h&&(h.previous=o),s.previous||(this.firstChunk=o.next),o.next||(this.lastChunk=s.previous,this.lastChunk.next=null),s.previous=C,o.next=h||null,C||(this.firstChunk=s),h||(this.lastChunk=o),this}overwrite(t,A,n,s){if(typeof n!="string")throw new TypeError("replacement content must be a string");for(;t<0;)t+=this.original.length;for(;A<0;)A+=this.original.length;if(A>this.original.length)throw new Error("end is out of bounds");if(t===A)throw new Error("Cannot overwrite a zero-length range \u2013 use appendLeft or prependRight instead");this._split(t),this._split(A),s===!0&&(G.storeName||(console.warn("The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string"),G.storeName=!0),s={storeName:!0});const o=s!==void 0?s.storeName:!1,a=s!==void 0?s.contentOnly:!1;if(o){const C=this.original.slice(t,A);Object.defineProperty(this.storedNames,C,{writable:!0,value:!0,enumerable:!0})}const g=this.byStart[t],h=this.byEnd[A];if(g){let C=g;for(;C!==h;){if(C.next!==this.byStart[C.end])throw new Error("Cannot overwrite across a split point");C=C.next,C.edit("",!1)}g.edit(n,o,a)}else{const C=new Z(t,A,"").edit(n,o);h.next=C,C.previous=h}return this}prepend(t){if(typeof t!="string")throw new TypeError("outro content must be a string");return this.intro=t+this.intro,this}prependLeft(t,A){if(typeof A!="string")throw new TypeError("inserted content must be a string");this._split(t);const n=this.byEnd[t];return n?n.prependLeft(A):this.intro=A+this.intro,this}prependRight(t,A){if(typeof A!="string")throw new TypeError("inserted content must be a string");this._split(t);const n=this.byStart[t];return n?n.prependRight(A):this.outro=A+this.outro,this}remove(t,A){for(;t<0;)t+=this.original.length;for(;A<0;)A+=this.original.length;if(t===A)return this;if(t<0||A>this.original.length)throw new Error("Character is out of bounds");if(t>A)throw new Error("end must be greater than start");this._split(t),this._split(A);let n=this.byStart[t];for(;n;)n.intro="",n.outro="",n.edit(""),n=A>n.end?this.byStart[n.end]:null;return this}lastChar(){if(this.outro.length)return this.outro[this.outro.length-1];let t=this.lastChunk;do{if(t.outro.length)return t.outro[t.outro.length-1];if(t.content.length)return t.content[t.content.length-1];if(t.intro.length)return t.intro[t.intro.length-1]}while(t=t.previous);return this.intro.length?this.intro[this.intro.length-1]:""}lastLine(){let t=this.outro.lastIndexOf(P);if(t!==-1)return this.outro.substr(t+1);let A=this.outro,n=this.lastChunk;do{if(n.outro.length>0){if(t=n.outro.lastIndexOf(P),t!==-1)return n.outro.substr(t+1)+A;A=n.outro+A}if(n.content.length>0){if(t=n.content.lastIndexOf(P),t!==-1)return n.content.substr(t+1)+A;A=n.content+A}if(n.intro.length>0){if(t=n.intro.lastIndexOf(P),t!==-1)return n.intro.substr(t+1)+A;A=n.intro+A}}while(n=n.previous);return t=this.intro.lastIndexOf(P),t!==-1?this.intro.substr(t+1)+A:this.intro+A}slice(t=0,A=this.original.length){for(;t<0;)t+=this.original.length;for(;A<0;)A+=this.original.length;let n="",s=this.firstChunk;for(;s&&(s.start>t||s.end<=t);){if(s.start<A&&s.end>=A)return n;s=s.next}if(s&&s.edited&&s.start!==t)throw new Error(`Cannot use replaced character ${t} as slice start anchor.`);const o=s;for(;s;){s.intro&&(o!==s||s.start===t)&&(n+=s.intro);const a=s.start<A&&s.end>=A;if(a&&s.edited&&s.end!==A)throw new Error(`Cannot use replaced character ${A} as slice end anchor.`);const g=o===s?t-s.start:0,h=a?s.content.length+A-s.end:s.content.length;if(n+=s.content.slice(g,h),s.outro&&(!a||s.end===A)&&(n+=s.outro),a)break;s=s.next}return n}snip(t,A){const n=this.clone();return n.remove(0,t),n.remove(A,n.original.length),n}_split(t){if(this.byStart[t]||this.byEnd[t])return;let A=this.lastSearchedChunk;const n=t>A.end;for(;A;){if(A.contains(t))return this._splitChunk(A,t);A=n?this.byStart[A.end]:this.byEnd[A.start]}}_splitChunk(t,A){if(t.edited&&t.content.length){const s=Kt(this.original)(A);throw new Error(`Cannot split a chunk that has already been edited (${s.line}:${s.column} \u2013 "${t.original}")`)}const n=t.split(A);return this.byEnd[A]=t,this.byStart[A]=n,this.byEnd[n.end]=n,t===this.lastChunk&&(this.lastChunk=n),this.lastSearchedChunk=t,!0}toString(){let t=this.intro,A=this.firstChunk;for(;A;)t+=A.toString(),A=A.next;return t+this.outro}isEmpty(){let t=this.firstChunk;do if(t.intro.length&&t.intro.trim()||t.content.length&&t.content.trim()||t.outro.length&&t.outro.trim())return!1;while(t=t.next);return!0}length(){let t=this.firstChunk,A=0;do A+=t.intro.length+t.content.length+t.outro.length;while(t=t.next);return A}trimLines(){return this.trim("[\\r\\n]")}trim(t){return this.trimStart(t).trimEnd(t)}trimEndAborted(t){const A=new RegExp((t||"\\s")+"+$");if(this.outro=this.outro.replace(A,""),this.outro.length)return!0;let n=this.lastChunk;do{const s=n.end,o=n.trimEnd(A);if(n.end!==s&&(this.lastChunk===n&&(this.lastChunk=n.next),this.byEnd[n.end]=n,this.byStart[n.next.start]=n.next,this.byEnd[n.next.end]=n.next),o)return!0;n=n.previous}while(n);return!1}trimEnd(t){return this.trimEndAborted(t),this}trimStartAborted(t){const A=new RegExp("^"+(t||"\\s")+"+");if(this.intro=this.intro.replace(A,""),this.intro.length)return!0;let n=this.firstChunk;do{const s=n.end,o=n.trimStart(A);if(n.end!==s&&(n===this.lastChunk&&(this.lastChunk=n.next),this.byEnd[n.end]=n,this.byStart[n.next.start]=n.next,this.byEnd[n.next.end]=n.next),o)return!0;n=n.next}while(n);return!1}trimStart(t){return this.trimStartAborted(t),this}hasChanged(){return this.original!==this.toString()}_replaceRegexp(t,A){function n(o,a){return typeof A=="string"?A.replace(/\$(\$|&|\d+)/g,(g,h)=>h==="$"?"$":h==="&"?o[0]:+h<o.length?o[+h]:`$${h}`):A(...o,o.index,a,o.groups)}function s(o,a){let g;const h=[];for(;g=o.exec(a);)h.push(g);return h}if(t.global)s(t,this.original).forEach(a=>{a.index!=null&&this.overwrite(a.index,a.index+a[0].length,n(a,this.original))});else{const o=this.original.match(t);o&&o.index!=null&&this.overwrite(o.index,o.index+o[0].length,n(o,this.original))}return this}_replaceString(t,A){const{original:n}=this,s=n.indexOf(t);return s!==-1&&this.overwrite(s,s+t.length,A),this}replace(t,A){return typeof t=="string"?this._replaceString(t,A):this._replaceRegexp(t,A)}_replaceAllString(t,A){const{original:n}=this,s=t.length;for(let o=n.indexOf(t);o!==-1;o=n.indexOf(t,o+s))this.overwrite(o,o+s,A);return this}replaceAll(t,A){if(typeof t=="string")return this._replaceAllString(t,A);if(!t.global)throw new TypeError("MagicString.prototype.replaceAll called with a non-global RegExp argument");return this._replaceRegexp(t,A)}}const eA=new Uint8Array(new Uint16Array([1]).buffer)[0]===1;function yt(i,t="@"){if(!w)return Lt.then(()=>yt(i));const A=i.length+1,n=(w.__heap_base.value||w.__heap_base)+4*A-w.memory.buffer.byteLength;n>0&&w.memory.grow(Math.ceil(n/65536));const s=w.sa(A-1);if((eA?rA:AA)(i,new Uint16Array(w.memory.buffer,s,A)),!w.parse())throw Object.assign(new Error(`Parse error ${t}:${i.slice(0,w.e()).split(`

Error: Parse error @:1:591
    at yt ($HOME/wode/node_modules/.pnpm/@esbuild-kit+core-utils@3.0.0/node_modules/@esbuild-kit/core-utils/dist/index.js:9:7926)
    at oA ($HOME/wode/node_modules/.pnpm/@esbuild-kit+core-utils@3.0.0/node_modules/@esbuild-kit/core-utils/dist/index.js:14:66)
    at dt ($HOME/wode/node_modules/.pnpm/@esbuild-kit+core-utils@3.0.0/node_modules/@esbuild-kit/core-utils/dist/index.js:22:85)
    at $A ($HOME/wode/node_modules/.pnpm/@esbuild-kit+core-utils@3.0.0/node_modules/@esbuild-kit/core-utils/dist/index.js:23:1132)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async ZA ($HOME/wode/node_modules/.pnpm/@esbuild-kit+core-utils@3.0.0/node_modules/@esbuild-kit/core-utils/dist/index.js:23:2207)
    at async x (file://$HOME/wode/node_modules/.pnpm/@esbuild-kit+esm-loader@2.5.4/node_modules/@esbuild-kit/esm-loader/dist/index.js:1:3035)
    at async nextLoad (node:internal/modules/esm/loader:163:22)
    at async ESMLoader.load (node:internal/modules/esm/loader:605:20)
    at async ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:11) {
  idx: 590
}
privatenumber commented 1 year ago

You can create a minimal reproduction by making a new copy of your repository and deleting all the files & dependencies until you're unable to reproduce the bug.

Will re-open once it's provided.

wenerme commented 1 year ago

@privatenumber https://github.com/wenerme/tsx-issues-183 try this repo, remove all unrelated code, problem still exists.

privatenumber commented 1 year ago

Doesn't look like all unrelated code is removed. Please go as far as deleting all files that are not used (e.g. README.md).

The final reproduction shouldn't even have a packages or apps directory. It should just be index.ts and node_modules/package/index.tsx.

When you ask someone for help with a bug in your project, the least you can do is make it as small as possible so they don't waste time filtering through unrelated files. The idea here is to be considerate of the person you're asking help from and to narrow the bug down as much as possible for them.

As per the bug report template:

👉 Tip: The smaller the reproduction, the faster we can debug and resolve your issue Remove all unnecessary files, code, & dependencies. This is the first step of debugging so doing this will save us work. If possible, convert TypeScript to plain JavaScript to eliminate the possibility of it being a TS bug

wenerme commented 1 year ago

Found the problem, because import a tsx file, event the import is not used.

privatenumber commented 1 year ago

That's why I said node_modules/package/index.tsx.

Again, will reopen once reproduction is provided.

wenerme commented 1 year ago

The problem is caused by "jsx": "preserve", after change to non preserve works as expected , sorry for the repro, maybe tsx can also improve the error message, I don't want to create an issues if I can understand what's wrong.

privatenumber commented 1 year ago

Feel free to open a bug report about the error message.