Skip to content

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

Closed

Description

@lvpro

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.

Activity

  1. RyanCavanaugh commented on Oct 1, 2018

    @RyanCavanaugh
    Member

    We need a way to reproduce the issue.

  2. lvpro commented on Oct 2, 2018

    @lvpro
    Author

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

  3. NickIliev commented on Jan 18, 2019

    @NickIliev

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

  4. memetican commented on Apr 2, 2019

    @memetican

    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.

  5. rxgx commented on Aug 15, 2019

    @rxgx

    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.

  6. mt0erfztxt commented on Dec 9, 2019

    @mt0erfztxt

    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
  7. pjercic commented on Dec 13, 2019

    @pjercic

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

    • dist is the problem, it is a loop, it tries to recompile itself
  8. leilapearson commented on May 8, 2020

    @leilapearson

    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.

  9. leilapearson commented on May 8, 2020

    @leilapearson

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

  10. outrowender commented on Sep 9, 2020

    @outrowender

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

  11. dbelarmino commented on Sep 11, 2020

    @dbelarmino

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

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

    This worked for me

  12. impssngr commented on Dec 3, 2020

    @impssngr

    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

  13. RyanCavanaugh commented on Dec 3, 2020

    @RyanCavanaugh
    Member

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

    #40011 should help in the future for diagnosing such problems

  14. asins commented on Sep 24, 2021

    @asins

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions