Skip to content

tsc will attempt to compile source files in 'node_modules' when it shouldn't #12320

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

Closed
JustASquid opened this issue Nov 17, 2016 · 6 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@JustASquid
Copy link

TypeScript Version: 2.0.8

It appears that tsc will attempt to compile source files in node_modules that are part of modules being imported, even when node_modules is excluded and type definition files are present. It is my understanding that this is not intended behaviour.

In researching this issue, I came across #6964 which looks like it should have fixed the exact bug I am still encountering.

The reproduction is very simple - just try to import a package that contains broken ts code (In this case, the guilty party is aws-sdk):

in package.json:

"dependencies": {
    "@types/aws-sdk": "0.0.42",
    "aws-sdk": "^2.7.3"
  }

tsconfig.json (this is just the default tsconfig):

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true
  },
  "exclude": [
    "node_modules"
  ]
}

and finally, the entry point, index.ts:

import * as AWS from "aws-sdk";

When compiling, tsc will throw errors as if it is compiling the AWS project itself.

@vladima
Copy link
Contributor

vladima commented Nov 17, 2016

  1. exclude option tells compiler not to include files or folders when compiler searches for initial set of files to compile however it does not affect results of module resolution - i.e. @types/jquery will be included in the program if some file imports jquery even if node_modules is excluded in config file.
  2. as I see aws-sdk package also bundles typing files so compiler will prefer them to @types. These are .d.ts files and they don't produce any output or impact the runtime.

@vladima vladima added the Question An issue which isn't directly actionable in code label Nov 17, 2016
@JustASquid
Copy link
Author

@vladima OK, right - so is there a way to make tsc prefer the @types declaration instead of the included declarations? Or am I simply unable to use this package until it gets fixed by the package developers?

If I'm understanding this right, it feels like a major problem for package compatibility. If I have my tsc set up to have a stricter/different configuration than the developer of a package, or if a package doesn't get updated for a while and tsc has breaking changes, so long as the package 'helpfully' includes .d.ts files, I'm stuck?

@mhegazy
Copy link
Contributor

mhegazy commented Nov 17, 2016

The core of the issue here is that the aws-sdk package has an inviald declaration file. i owuld log a bug on thier repo first.

To override the typings I would use path mapping :

{
    "compilerOptions": {
        "baseUrl" : "./",
        "paths": {
            "*" : ["./node_modules/@types/*", "*"]
        }
    }
}

More info can be found about the original question at https://github.com/Microsoft/TypeScript/wiki/FAQ#why-is-a-file-in-the-exclude-list-still-picked-up-by-the-compiler

@JustASquid
Copy link
Author

@mhegazy Thank you!

@plee-nm
Copy link

plee-nm commented May 1, 2018

@mhegazy Is there a way to ignore an embedded .d.ts file like this one?

@mhegazy
Copy link
Contributor

mhegazy commented May 1, 2018

Is there a way to ignore an embedded .d.ts file like this one?

Add a path mapping in your tsconfig.json to map redux-thunk to a different .d.ts file that you provide.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants