Typescript: Error "Cannot write file ... because it would overwrite input file."

Created on 28 Sep 2018  路  13Comments  路  Source: microsoft/TypeScript

Started getting this after upgrading from 3.0.3 to 3.1.1. Looks like the same problem as #14538

Using create-react-app Webpack 2 project with ts-loader 3.5.0. Still builds fine, but VS Code pops the tsconfig error. In our case, unlike most people in #14538, the file mentioned is one of our JS sources.

This is a TS/JS mixed project. Made no other changes other than upgrading TS to 3.1.1. No problems prior. Latest VS Code. Still builds fine otherwise.

Needs More Info

Most helpful comment

I'm not 100% certain this is related, however I'm attempting to compile and deploy a NestJS app, and was getting this error on npm run build.
Looking into it (I'm new to npm, typescript, ionic, and nest), my guess is that build is trying to also compile my output directory, and therefore has an output-will-overwrite-input conflict.
I resolved this by editing tsconfig.build.json, and adding my build directory to the exclude list. In NestJS, the build dir appears to default to dist.

All 13 comments

We need a way to reproduce the issue.

Setting outDir in tsconfig removed the error in VS Code for me. In my case, I set it to /dev/shm.

Having to explicitly set one option like outDir to enable other option like resolveJsonModule is not (in my opinion) a nice solution. There are many scenarios where, you can't have or do not want to have different outDir. For example in NativeScript the outDir is exactly the same which is resulting in that resolveJsonModule to be unusable with the latest TypeScript version.

I thing that the options should be decoupled - we shouldn't have to mandatory set one option to have another in a working state. Not to mention that you can still set the outDir` to the very same folder and cause the very same bug to reappear (which is creating a third rule - do not set outDir as the project dir...)

I'm not 100% certain this is related, however I'm attempting to compile and deploy a NestJS app, and was getting this error on npm run build.
Looking into it (I'm new to npm, typescript, ionic, and nest), my guess is that build is trying to also compile my output directory, and therefore has an output-will-overwrite-input conflict.
I resolved this by editing tsconfig.build.json, and adding my build directory to the exclude list. In NestJS, the build dir appears to default to dist.

I resolved this by editing tsconfig.build.json, and adding my build directory to the exclude list. In NestJS, the build dir appears to default to dist.

Make sure you have assigned an include in addition to an exclude in the tsconfig.json.

Looking into it (I'm new to npm, typescript, ionic, and nest), my guess is that build is trying to also compile my output directory, and therefore has an output-will-overwrite-input conflict.

Try to run TypeScript compiler with --listFiles flag

npx tsc --listFiles

in case when list have many entries it can be narrowed with

npx tsc --listFiles | grep dist

"exclude": ["node_modules", "*/.test.ts", "dist"]

  • dist is the problem, it is a loop, it tries to recompile itself

Might be able to help you reproduce this issue...

It seems like some part of the compilation process isn't recognizing that it is inside an excluded directory.

I don't don't see the problem if I do this:

  "exclude": ["**/*.d.ts", "dist", "node_modules"]

I do see the problem if I do this:

  "exclude": ["dist/**/*.d.ts", "dist", "node_modules"]

or this:

  "exclude": ["**/dist/**/*.d.ts", "dist", "node_modules"]

The error occurs despite the fact that the files it is complaining about are clearly inside dist:

error TS5055: Cannot write file '/Users/leila/dev/wip/jest-fp-ts/dist/index.d.ts' because it would overwrite input file.
error TS5055: Cannot write file '/Users/leila/dev/wip/jest-fp-ts/dist/matchers/index.d.ts' because it would overwrite input file.

In my case I have imports set up where src/index.ts imports and re-exports from src/matchers/index.ts which in turn imports and re-exports from src/matchers/eitherMatchers/index.ts.

The first two files are the ones with the compilation errors. The third file is fine. Other .d.ts files are fine too. So it looks like it might be related to how the import / export tree is affecting compilation.

Ah... another important piece of the puzzle. I discovered I had a manually created global.d.ts file that was mistakenly importing from dist instead of from src...

Once I fixed that, the problem went away. So maybe not the same problem others are seeing - but one way to reproduce it...

For me the problem was solved adding dist to exclude and "compilerOptions"."allowJs": false and "exclude": ["dist"]

"exclude": ["node_modules", "*/.test.ts", "dist"]

  • dist is the problem, it is a loop, it tries to recompile itself

This worked for me

This might help anyone who ran into this issue:

If adding the output directory to the exclude option didn't help, then check your source directory - you might accidentally import something from output directory in your sources. It also results in the aforementioned error

We haven't seen any instance of this that isn't a misconfiguration

https://github.com/microsoft/TypeScript/pull/40011 should help in the future for diagnosing such problems

Was this page helpful?
0 / 5 - 0 ratings