Skip to content

Typeroots not inferred with TS2.1+ on Windows #283

Closed

Description

@filipesilva

I have a simple repo at https://github.com/filipesilva/tsnode-typeroots that shows this issue.

To reproduce, do:

git clone https://github.com/filipesilva/tsnode-typeroots
cd tsnode-typeroots
npm install
npm run tsc
npm run tsnode

On a OSX machine, both npm scripts will complete successfully. On a Windows machine, npm run tsnode will fail compilation with the following error:

TSError: ⨯ Unable to compile TypeScript
Cannot find type definition file for 'node'. (2688)
index.ts (1,13): Cannot find name 'module'. (2304)

This can be fixed either by:

  • installing typescript@2.0
  • adding "typeRoots": [ "node_modules/@types" ] to compilerOptions in tsconfig.json

I believe this is a bug because the tsc compilation itself completes successfully, whereas the tsnode one does not.

Activity

  1. blakeembrey commented on Mar 6, 2017

    @blakeembrey
    Member

    Is this the same issue as the other Windows issues? See #216.

  2. filipesilva commented on Mar 6, 2017

    @filipesilva
    Author

    @blakeembrey I'm not sure if it's the same. I agree that it seems similar, but in my case I also see this:

    Cannot find type definition file for 'node'. (2688)
    

    Which seems to indicate that the node types are being looked for explicitly and not found.

    I also tried adding jasmine+types into the example and updating it, and it seems to not be specific to node typings as the original poster in #216 hypothesized.

    kamik@T460p MINGW64 /d/sandbox/tsnode-typeroots (master)
    $ npm run tsc
    
    > tsnode-typeroots@1.0.0 tsc D:\sandbox\tsnode-typeroots
    > tsc -p ./tsconfig.json
    
    
    kamik@T460p MINGW64 /d/sandbox/tsnode-typeroots (master)
    $ npm run tsnode
    
    > tsnode-typeroots@1.0.0 tsnode D:\sandbox\tsnode-typeroots
    > ts-node -P ./tsconfig.json index.ts
    
    
    D:\sandbox\tsnode-typeroots\node_modules\ts-node\src\index.ts:319
              throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
                    ^
    TSError: ⨯ Unable to compile TypeScript
    Cannot find type definition file for 'jasmine'. (2688)
    Cannot find type definition file for 'node'. (2688)
    index.ts (1,13): Cannot find name 'module'. (2304)
    index.ts (3,1): Cannot find name 'describe'. (2304)
    index.ts (4,3): Cannot find name 'it'. (2304)
    index.ts (5,5): Cannot find name 'expect'. (2304)
        at getOutput (D:\sandbox\tsnode-typeroots\node_modules\ts-node\src\index.ts:319:17)
    
  3. blakeembrey commented on Mar 6, 2017

    @blakeembrey
    Member

    I'm pretty sure it's the same issue, just a different situation. It seems to all be related to the paths, but no one on Windows has offered a PR (you could be the first!). Do you have an explicit types in tsconfig.json causing it to search for node and jasmine?

  4. filipesilva commented on Mar 6, 2017

    @filipesilva
    Author

    I do, yes:

    {
      "compilerOptions": {
        "types": [
          "node",
          "jasmine"
        ]
      },
      "files": [
        "index.ts"
      ]
    }
    

    In the original issue it's happening with TS@2.0, but in here it only happens on TS@2.1 and up. In fact, reverting to TS@2.0 actually fixes it.

  5. filipesilva commented on Mar 7, 2017

    @filipesilva
    Author

    I tried doing some investigation by tracing directory checks (which is usually how stuff breaks on windows but not on linux/osx) and I think I'm on the right track.

    I tried tracking cwd (index.tx#190) and failed directory lookups (index.ts#587):

    cwd: D:\sandbox\tsnode-typeroots
    dir doesn't exist: D:/node_modules/@types
    dir doesn't exist: D:/node_modules
    dir doesn't exist: D:/node_modules/@types
    dir doesn't exist: D:/node_modules
    

    So cwd is right but directory resolution seem to be broken somewhere, since it really shouldn't be looking for node_modules at my drive root.

    I fired up the debugger and tried to figure out where that went wrong:
    image

    ts.getDirectoryPath is called with D:\sandbox\tsnode-typeroots\tsconfig.json but returns D: instead of D:\sandbox\tsnode-typeroots.

    This seems to be because TypeScript expects the path separator to always be "/" and for getDirectories/directoryExists to correctly handle that.

    I digged a bit more as to how the tsconfig path got generated, and it turns out it's in the TS loadSync function:
    image

    I tried changing the result there manually to use / separators, which also required getDirectories/directoryExists to always normalize paths.

    This seemed to fix the Cannot find type definition file for 'x' errors, and node types were recognized, but the jasmine types still were not recognized:

    kamik@T460p MINGW64 /D/sandbox/tsnode-typeroots (master)
    $ node ./node_modules/ts-node/dist/_bin.js -P ./tsconfig.json index.ts
    .
    
    D:\sandbox\tsnode-typeroots\index.ts:3
    describe('test', () => {
    ^
    ReferenceError: describe is not defined
        at Object.<anonymous> (D:\sandbox\tsnode-typeroots\index.ts:3:1)
    

    At this point I stopped. I get the feeling there's some kind of systematic issue with path handling on windows that tsc is doing but ts-node is not. If you have a hint as to how I should proceed from here, let me know.

  6. blakeembrey commented on Mar 7, 2017

    @blakeembrey
    Member

    @filipesilva That is exactly the issue (see my comment in #284 (comment) too). The Jasmine issue you later ran into is actually because TypeScript compilation passed but at runtime the describe keyword did not exist - so it looks like whatever you tweaked was correct. In terms of where the fix is, TypeScript exports a normalizeSlashes utility they use in places and we can use that on whichever location is needed (tsconfig isn't a TypeScript utility, but a library that was needed to get around a similar paths issue when resolving the tsconfig.json file consistently).

  7. filipesilva commented on Mar 14, 2017

    @filipesilva
    Author

    I was just getting ready to push a PR when I noticed #245 - it's exactly what I was doing plus a few more fixes. Cheers @Jontem!

  8. added a commit that references this issue on Mar 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions