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

Error "Cannot write file ... because it would overwrite input file." #27436

Closed
lvpro opened this issue Sep 28, 2018 · 14 comments
Closed

Error "Cannot write file ... because it would overwrite input file." #27436

lvpro opened this issue Sep 28, 2018 · 14 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@lvpro
Copy link

lvpro commented Sep 28, 2018

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.

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Oct 1, 2018
@RyanCavanaugh
Copy link
Member

We need a way to reproduce the issue.

@lvpro
Copy link
Author

lvpro commented Oct 2, 2018

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

@NickIliev
Copy link

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...)

@memetican
Copy link

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.

@rxgx
Copy link

rxgx commented Aug 15, 2019

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.

@mt0erfztxt
Copy link

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

@pjercic
Copy link

pjercic commented Dec 13, 2019

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

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

astoilkov added a commit to astoilkov/use-local-storage-state that referenced this issue Mar 15, 2020
Fails with "error TS5055: Cannot write file '/Users/astoilkov/Repos/use-local-storage-state/dist/index.d.ts' because it would overwrite input file.".
- https://stackoverflow.com/questions/42609768/typescript-error-cannot-write-file-because-it-would-overwrite-input-file
- microsoft/TypeScript#16394
- microsoft/TypeScript#27436
- microsoft/TypeScript#14538
@leilapearson
Copy link

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.

@leilapearson
Copy link

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...

@outrowender
Copy link

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

@dbelarmino
Copy link

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

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

This worked for me

@impssngr
Copy link

impssngr commented Dec 3, 2020

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

@RyanCavanaugh
Copy link
Member

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

#40011 should help in the future for diagnosing such problems

@asins
Copy link

asins commented Sep 24, 2021

This is because TypeScript defaults to parsing TS files to generate JS files that are placed in the same directory, which may have the potential to overwrite JS files.
So, if you configured "allowJs": true in the tsconfig.json file, you need to configure "outDir": "./dist", and "exclude”: ["./dist"].
This way TypeScript does not have an override when parsing TS files.


这是因为TypeScript默认在解析TS文件生成JS文件是放在相同目录中的,这就有可能存在覆盖JS文件的可能。
所以,如果你在tsconfig.json文件中配置了"allowJs": true,那么就需要配置"outDir": "./dist",以及配置"exclude”: ["./dist"]
这样TypeScript在解析TS文件时才不会存在覆盖的情况。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests