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

[TypeScript 2] Relative paths in typeRoots not resolved (tsconfig.json) #1055

Closed
elisabethallan opened this issue Aug 18, 2016 · 14 comments · Fixed by #1166
Closed

[TypeScript 2] Relative paths in typeRoots not resolved (tsconfig.json) #1055

elisabethallan opened this issue Aug 18, 2016 · 14 comments · Fixed by #1166

Comments

@elisabethallan
Copy link

elisabethallan commented Aug 18, 2016

Hi,

I'm having trouble with type definitions being picked up using TypeScript 2/atom-typescript when the tsconfig.json is not in the same directory as node_modules. I have a src folder containing tsconfig.json, refrencing ../node_modules/@types in the typeRoots, and a Jasmine test in src/app. atom-typescript does not pick up the Jasmine declarations (e.g. describe, it etc) and highlights them as errors.

My folder structure is as follows (some folders removed):

- node_modules
-- @types
- src
-- tsconfig.json
-- app
--- app.component.spec.ts
--- app.component.ts

My tsconfig.json in src is as follows:

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "mapRoot": "./",
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ],
    "types": [
      "jasmine"
    ]
  }
}

app.component.spec.ts:

import { AppComponent } from './app.component';

describe('Example', () => {
  beforeEach(() => {
    // Stuff
  });

  it('should create the app', () => {
    expect(true).toBeTruthy();
  });
});

Environment info:

OS: OS X El Capitan 10.11.6
Node.js: 6.3.1
Typescript: 2.0.0
Atom: 1.9.8
atom-typescript: 10.1.6

This configuration works with tsc on the command line (either tsc -p src in the project root, or tsc -p . in src). It also works in atom-typescript if I put the entire path in typeRoots - e.g. /Users/sean/someProject/node_modules/@types - which makes me think that this is an atom-typescript specific issue in resolving relative paths.

Is this a bug or just something I'm doing wrong with my config?

@selkinvitaly
Copy link

selkinvitaly commented Aug 19, 2016

Yes, I confirm it.

My environment:

OS: Debian x64 stretch
Node.js: 5.11.0
Typescript: 2.1.0-dev.20160812
Atom: 1.5.3
atom-typescript: 10.1.6

@superchris
Copy link

Also seeing the same thing. OSX, node v 5.1

@oailloud
Copy link

Same thing here, none of the jasmine types are resolved.
I confirm it's working when typeRoots is set to the absolute path to node_modules/@types (with jasmine listed in types) in the tsconfig.json.
OS : Fedora 24
Node v6.6.0
NPM 3.10.3
Atom 1.9.9
atom-typescript 10.1.6

@spali
Copy link

spali commented Sep 30, 2016

similar problem with angular2-seed project (mgechev/angular2-seed).
type core-js not recognized by atom typescript.
defined in src/client/tsconfig.json with

    "typeRoots": [
      "../../node_modules/@types",
      "../../node_modules"
    ],
    "types": [
      "core-js",
      ...

Array.from or Object.assign etc. is not recognized when used.
if I replace the the relative path in typeRoots with absolute path, it works.

OS: Windows 7
Node: 5.6.0
NPM: 3.10.8
Atom: 1.10.2
atom-typescript: 10.1.7

@jorgeas80
Copy link

Having the same problem with jasmine, but in VSCode. I don't know if ts plugin for VSCode and for atom are related, but looks suspicious...

@chrissloan
Copy link

Same issue here. To fix this as a hack until plugin is changed, I pulled in the node_modules/@types directory to be direct relative to the tsconfig.json file. Looking at the source code for the atom package, there is a default typeRoots path that is always set in relation to the read tsconfig file. I wonder if this is the issue as it does not get overwritten.

Seems dirty, but works and the Angular-cli project I am running does not care as it still looks to the node_modules directory outside the src directory.

@iamolivinius
Copy link

Looks like this is the same issue I'm facing right now.

Excluding node_modules due to performance issues results in compiler errors within atom-typescript only.

In the screenshot you can see that VSCode@1.6 as well as tsc@2.0.3 compile the same code without any problems (as it should be). Browserify with tsify don't throw any errors either.

screenshot from 2016-10-13 14-32-20

@addisonj
Copy link

I am having the same issue as @iamolivinius. This issue only started when I migrated a few projects to go from storing typings in ./typings' in the root of my project to primarily using TS 2.0@typesin node_modules. If I removenode_modules` from my excluded list, it works, but that just kills the performance of atom-typescript...

Not sure if this is new with typescript 2.0, but in tsconfig, if you don't set exclude it by defaults excludes node_modules as well as any outDir specific, which is exactly the behavior I want but it doesn't seem like atom-typescript follows it.

@nycdotnet
Copy link
Contributor

I think this is definitely a bug - or rather it had been implemented correctly and then the new functionality of TypeScript wasn't accounted for. PRs definitely accepted.

@nikoTM
Copy link

nikoTM commented Oct 18, 2016

This works as a temporary solution for me:

  "exclude": [
    "!/node_modules/@types"
  ]

@spali
Copy link

spali commented Nov 10, 2016

Did not work for me in the angular2-seed project (mgechev/angular-seed) I mentioned.
But the following workaround did it:

in src/client/tsconfig.json change

    "typeRoots": [
      "../../node_modules/@types",
      "../../node_modules"
    ],
    "types": [
      "core-js",
      ...

to

    "baseUrl": "../../",
    "typeRoots": [
      "node_modules/@types",
      "node_modules"
    ],
    "types": [
      "core-js",
      ...

niik added a commit to desktop/desktop that referenced this issue Dec 8, 2016
Without this we're getting an error while using ts-node on Windows.

See
TypeStrong/ts-node#168
TypeStrong/atom-typescript#1055

Note that this works around the issue but as far as I understand
things it shouldn't be necessary.
@jhm-ciberman
Copy link

+1

@Shijir
Copy link

Shijir commented Jan 18, 2017

Using include instead of exclude is working too:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "typeRoots": [
            "node_modules/@types"
        ],
        "types": [
            "jasmine",
            "node",
            "core-js",
            "selenium-webdriver"
        ]
    },
    "include": [
        "src/**/*.ts",
        "node_modules/@types"
    ],
    "atom": {
        "rewriteTsconfig": false
    }
}

@kamok
Copy link

kamok commented May 18, 2017

@Shijir I can confirm removing the exclude array fixed it for me. Thanks.

@TypeStrong TypeStrong locked and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.