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

npm link forces two imports of the same type to be compared structurally #14746

Closed
hiroshi-cl opened this issue Mar 20, 2017 · 4 comments
Closed
Assignees
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created

Comments

@hiroshi-cl
Copy link

TypeScript Version: 2.1.4, 2.1.6, 2.2.1

(my environment: node 7.7.3, OS X El Capitan)

Code

See > https://github.com/hiroshi-cl/bug-sample-tsc-crash

Expected behavior:

Some compiled code / user-friendly error message

Actual behavior:

crash (memory leak?)

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Mar 20, 2017
@mhegazy mhegazy added this to the TypeScript 2.3 milestone Mar 24, 2017
@sandersn
Copy link
Member

sandersn commented Apr 5, 2017

Still repros on master.

@sandersn sandersn changed the title tsc crash by heap out of memory when using npm link npm link forces two imports of the same type to be compared structurally Apr 7, 2017
@sandersn
Copy link
Member

sandersn commented Apr 7, 2017

This appears to be two bugs.

  1. npm link forces the type Either from import { Either } from 'monet' to be different in def/src/index.ts vs use/src/index.ts. Normally the compiler is able to recognise that these are the same type and only create one type object. With a linked package, however, it doesn't know the types are identical so it creates two and is forced to compare them structurally.
  2. monet is a monad library and the compiler can't compare these types structurally in a reasonable amount of time or space.

(1) is supposed to be fixed by #8486, I think. I will investigate why this is not the case.
(2) is a known limitation of Typescript's current semantics and architecture. See #9052 for an example.

@sandersn
Copy link
Member

sandersn commented Apr 7, 2017

Actually, this is a duplicate of #6496. The issue is actually that npm link causes there to be two definitions of monet loaded. You can see this if you pass --traceResolution to the compiler:

======== Resolving module 'monet' from '/media/nathansa/src2/src/bug-sample-tsc-crash/use/src/index.ts'. ========
...
...
======== Module name 'monet' was successfully resolved to '/media/nathansa/src2/src/bug-sample-tsc-crash/use/node_modules/monet/index.d.ts'. ========
...
...
======== Resolving module 'monet' from '/home/nathansa/.nvm/versions/node/v7.4.0/lib/node_modules/@local/def/dist/index.d.ts'. ========
...
...
======== Module name 'monet' was successfully resolved to '/home/nathansa/.nvm/versions/node/v7.4.0/lib/node_modules/monet/index.d.ts'. ========

The first monet is the one that use got from running npm install. The second monet is the one that def get from running npm install followed by npm install g . monet. So the two Eithers are coming from two different monets. The workaround is to explicitly specify the path to use for monet when compiling def via baseUrl and paths:

{
  "compilerOptions": {
      "outDir": "dist",
      "baseUrl": ".",
      "paths": {
          "monet": ["node_modules/monet"]
      }
  },
  "include": [
    "./src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

This will force both use/index.ts and the linked node_modules/@local/def/index.d.ts to resolve to node_modules/monet instead of the linked reference resolving to the global installation of monet.

@sandersn sandersn added the Duplicate An existing issue was already created label Apr 7, 2017
@sandersn sandersn closed this as completed Apr 7, 2017
@hiroshi-cl
Copy link
Author

thanks for your investigation

@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants