ts-loader icon indicating copy to clipboard operation
ts-loader copied to clipboard

When dependencies are installed with pnpm, ts-loader behaves differently than tsc

Open ccstr opened this issue 8 years ago • 9 comments

Expected Behaviour

ts-loader should not report TS2305

Actual Behaviour

ts-loader report TS2305 error

Steps to Reproduce the Problem

  1. clone https://github.com/xbtsw/typescript-pnpm
  2. npm i -g pnpm
  3. pnpm install
  4. run npm run tsc and observe that the compilation success
  5. run npm start and observe error

This happens to both Linux and Windows 10 host.

Location of a Minimal Repository that Demonstrates the Issue.

https://github.com/xbtsw/typescript-pnpm

It looks like the type information is not visible to TypeScript when the dependency are symlinked and are compiled via ts-loader. I am willing to look into it but not sure where to start.

ccstr avatar Apr 25 '18 18:04 ccstr

@iclanton FYI

octogonz avatar Apr 25 '18 19:04 octogonz

Thanks for the detail! Wasn't there in one of the recent TypeScript releases about symlinks? I forget the details...

johnnyreilly avatar Apr 25 '18 20:04 johnnyreilly

@johnnyreilly Indeed, I read @zkochan mentioned that TypeScript resolved some issues on their end in order to get it working with pnpm, maybe he has more insight into those issues.

It will be great if we can get ts-loader working too, as almost all front-end projects using TypeScript will go thru webpack and ts-loader.

ccstr avatar Apr 25 '18 21:04 ccstr

If I remember it correctly, the issue with TypeScript was that it was preserving symlinks when resolving typings in node_modules. This issue was present in typescript@1

zkochan avatar Apr 25 '18 21:04 zkochan

The main idea is simply calling fs.realpathSync() in the right places, to be consistent with how NodeJS resolves modules. This was solved a long time ago in typescript@2 by adding realpathSync to their OS abstraction and plumbing it through all the needed places.

octogonz avatar Apr 25 '18 22:04 octogonz

Thanks for the insight @zkochan and @pgonzal. The reasoning I have so far is that since tsc works while ts-loader doesn't. It should be the case that they setup the TypeScript compiler differently, the problem shouldn't be with the compiler itself, otherwise tsc would fail too.

I compared the compiler setup code in ts-loader and tsc but wasn't be able to get very far there.

ccstr avatar Apr 25 '18 22:04 ccstr

I'm guessing the change would just be switching readFile implementation in https://github.com/TypeStrong/ts-loader/blob/master/src/utils.ts to use fs.realpathSync() in some way? Why don't you fork and experiment?

Feel free to submit a WIP PR if you get somewhere. I'm totally happy to help with tests etc

johnnyreilly avatar Apr 26 '18 04:04 johnnyreilly

These info are very insightful. I am taking a look into it now. Thanks guys.

ccstr avatar Apr 26 '18 09:04 ccstr

Thanks for your work! Released with https://github.com/TypeStrong/ts-loader/releases/tag/v4.3.0

johnnyreilly avatar May 07 '18 18:05 johnnyreilly