microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99.86k stars 12.36k forks source link

TypeError: Cannot read property 'get' of undefined at Object.getJSXImplicitImportBase #44024

Open salik1992 opened 3 years ago

salik1992 commented 3 years ago

Bug Report

🔎 Search Terms

ts getJSXImplicitImportBase ts jest getJSXImplicitImportBase ts file.pragmas

🕗 Version & Regression Information

4.2.4

⏯ Playground Link

I can't provide a link to a playground as this issue seems to be tied to the structure of the repository and dependencies. I have tried to reproduce a minimal example but without luck for now.

💻 Code

This seems to be tied to the structure of the repository and dependencies:

🙁 Actual behavior

Running tests with npx jest packages/ui/src/components/Toast/__tests__/Toast.spec.tsx crashes with:

TypeError: Cannot read property 'get' of undefined

      at Object.getJSXImplicitImportBase (../../node_modules/typescript/lib/typescript.js:19175:95)
      at transformSourceFile (../../node_modules/typescript/lib/typescript.js:88624:51)
      at transformSourceFileOrBundle (../../node_modules/typescript/lib/typescript.js:82540:57)
      at transformation (../../node_modules/typescript/lib/typescript.js:100190:24)
      at transformRoot (../../node_modules/typescript/lib/typescript.js:100217:82)
      at Object.transformNodes (../../node_modules/typescript/lib/typescript.js:100201:78)
      at emitJsFileOrBundle (../../node_modules/typescript/lib/typescript.js:100852:32)
      at emitSourceFileOrBundle (../../node_modules/typescript/lib/typescript.js:100799:13)
      at forEachEmittedFile (../../node_modules/typescript/lib/typescript.js:100520:34)
      at Object.emitFiles (../../node_modules/typescript/lib/typescript.js:100779:9)

when inspecting the issue I have noticed this behaviour:

This happens during local development as well as in travis CI.

🙂 Expected behavior

The jest tests run without crashing.

Maybe related issue

#43373

weswigham commented 3 years ago

Hm, this is difficult to verify without a repro, but I think the problem lies in ts-jest using our transformation API with a preTransform and our factory not retaining information.... maybe. In fact, looking over ts-jest's source code, I fully believe that, given how they implement their visitors - by using object spread and not our factory API, they're un-setting node prototypes. You can try to see if this build helps, but since ts-build doesn't use our factory to manufacture the nodes, I don't think it'll help. If you can locate this transform and this transform in your install of ts-jest, and modify them so that every spread is wrapped like Object.setPrototypeOf({ ... }, Object.getPrototypeOf(original)), and that fixes the crash, that change to ts-jest may be the solution.

Ofc, this is only the case because our internal RedirectedFile construct relies on prototypical inheritance to function, which makes maintaining prototypes a quiet requirement of a transformer...

salik1992 commented 3 years ago

Sorry for getting back to this now. I was blocked by some other work.

I have tried that specific build but no luck.

I have found those two transforms in generated js files inside node_modules. However, I haven't managed to fix it by wrapping those spreads. Just out of curiosity I have tried to set pragmas if it was undefined from that redirectTarget and it crashes further down the line on text being undefined in computeLineStarts so I feel like your belief is correct.

Should I open an issue in ts-jest or do you want to do it since you know a lot more about the specifics?

ahnpnl commented 3 years ago

Hm, this is difficult to verify without a repro, but I think the problem lies in ts-jest using our transformation API with a preTransform and our factory not retaining information.... maybe. In fact, looking over ts-jest's source code, I fully believe that, given how they implement their visitors - by using object spread and not our factory API, they're un-setting node prototypes. You can try to see if this build helps, but since ts-build doesn't use our factory to manufacture the nodes, I don't think it'll help. If you can locate this transform and this transform in your install of ts-jest, and modify them so that every spread is wrapped like Object.setPrototypeOf({ ... }, Object.getPrototypeOf(original)), and that fixes the crash, that change to ts-jest may be the solution.

Ofc, this is only the case because our internal RedirectedFile construct relies on prototypical inheritance to function, which makes maintaining prototypes a quiet requirement of a transformer...

Hi, I guess migrating to factory API should be the right direction?

weswigham commented 3 years ago

That's what I'd assume would fix the issue, yeah.