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." #14538

Closed
ChrisMBarr opened this issue Mar 8, 2017 · 111 comments
Closed

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

ChrisMBarr opened this issue Mar 8, 2017 · 111 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@ChrisMBarr
Copy link

TypeScript Version: 2.2.1

When using Visual Studio 2015 Update 3 I am getting hundreds of errors in the error list like:

Cannot write file 'C:/{{my-project}}/node_modules/buffer-shims/index.js' because it would overwrite input file.

It looks like this all the time. It doesn't actually prevent building, and everything works just fine, but the error list is distracting and difficult to locate "real" errors when they occur.

Visual Studio Error List

My tsconfig.json file

{
  "compileOnSave": true,
  "compilerOptions": {
    "baseUrl": ".",
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "ES5",
    "forceConsistentCasingInFileNames": true,
    "strictNullChecks": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "noFallthroughCasesInSwitch": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    
    "typeRoots": [],
    "types": [] //Explicitly specify an empty array so that the TS2 @types modules are not acquired since we aren't ready for them yet.
  },
  "exclude": ["node_modules"]
}

How can I get rid of all these errors?

(I've also posted this question on StackOverflow with no responses yet)

@leovo2708
Copy link

I also get same problem.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 9, 2017

is --allowjs set? can you share the project?

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Mar 9, 2017
@ChrisMBarr
Copy link
Author

Sorry, I cannot share the project, and no that flag is not set. My tsconfig.json is above, and we just use that with VS2015 Update 3, which just triggers the build with MSBuild normally.

I have teammates complaining about the same issue happening to them on the same projects. I also work on a project at home on a different computer that has the exact same issue & the same setup (TS 2.2.1, VS2015 U3, etc.)

@mhegazy
Copy link
Contributor

mhegazy commented Mar 9, 2017

do you see the same behavior if you create a bare-bones project with the same configurations?

@Hotell
Copy link

Hotell commented Mar 10, 2017

We have the same problem wc-catalogue/blaze-elements#299 although with type definitions write access

@mhegazy
Copy link
Contributor

mhegazy commented Mar 13, 2017

@Hotell do you see this without "awesome-typescript-loader"? can you share repro steps with me?

@Hotell
Copy link

Hotell commented Mar 13, 2017

yup, as you can see in the issue the output is from running raw tsc second time.

screen shot 2017-03-10 at 5 08 04 pm

just clone the repo https://github.com/wc-catalogue/blaze-elements

  • hit yarn from root
  • hit yarn tsc -> first time compilation ( everything's ok ) => first time definitions/ folder generated
  • hit yarn tsc again -> errors

@BrainSlugs83
Copy link

BrainSlugs83 commented Mar 14, 2017

We're having a similar issue -- same version of typescript (2.2.1) and Visual Studio 2015 (Update 3); the first time the build runs, there are zero errors, but after that we get hundreds of these errors.

It appears that all of the errors (for us) are in the "node_modules" folder, which we have set to exclude in our tsconfig.json file. -- From looking at similar bugs, it seems like the excludes aren't treated the same in this version of typescript?

Our tsconfig.json file:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "node_modules",
    "wwwroot",
    "aot",
    "AngularApp/main-aot.ts"

  ],
  "compileOnSave": true
}

Some of the errors we get (they're all the same, but different files):

Severity	Code	Description	Project	File	Line	Suppression State
Error	TS5055	Cannot write file 'C:/XYZ/Project.AppWeb/node_modules/zone.js/dist/zone.js' because it would overwrite input file.	TypeScript Virtual Projects		1	Active
Error	TS5055	Cannot write file 'C:/XYZ/Project.AppWeb/node_modules/events/events.js' because it would overwrite input file.	TypeScript Virtual Projects		1	Active
Error	TS5055	Cannot write file 'C:/XYZ/Project.AppWeb/node_modules/core-js/modules/_wks.js' because it would overwrite input file.	TypeScript Virtual Projects		1	Active
Error	TS5055	Cannot write file 'C:/XYZ/Project.AppWeb/node_modules/core-js/modules/_uid.js' because it would overwrite input file.	TypeScript Virtual Projects		1	Active
Error	TS5055	Cannot write file 'C:/XYZ/Project.AppWeb/node_modules/core-js/modules/_to-primitive.js' because it would overwrite input file.	TypeScript Virtual Projects		1	Active

Again, if we delete the "node_modules" folder the build will work once, but then once it's been recreated, it will fail on the next rebuild.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 14, 2017

@Hotell i am not seeing this locally, what am i missing?

c:\test\14538\blaze-elements>yarn tsc
yarn tsc v0.18.1
$ "c:\test\14538\blaze-elements\node_modules\.bin\tsc"
Done in 5.46s.

c:\test\14538\blaze-elements>yarn tsc
yarn tsc v0.18.1
$ "c:\test\14538\blaze-elements\node_modules\.bin\tsc"
Done in 5.87s.

c:\test\14538\blaze-elements>dir /B definitions
packages
polyfills.d.ts
styles.d.ts
test-helpers.d.ts
vendors.d.ts

c:\test\14538\blaze-elements>yarn tsc
yarn tsc v0.18.1
$ "c:\test\14538\blaze-elements\node_modules\.bin\tsc"
Done in 4.48s.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 14, 2017

@BrainSlugs83 if you have a repro project i would love to take a look.

@max-favilli
Copy link

I am also facing the same problem, these are the errors:
screen shot 2017-03-16 at 23 34 13
And this is my tsconfig.json:
screen shot 2017-03-16 at 23 34 25

Since I am not using typescript at all I just created tsconfig to skip build, I also had to choose target=ES6 otherwise I was having other errors.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 16, 2017

is the tsconfig.json part of your project? and can you confirm the content type is "Content"? if so, can you share your project?

@max-favilli
Copy link

max-favilli commented Mar 17, 2017

@mhegazy Hi, yes I can confirm tsconfig.json is included in the project:

screen shot 2017-03-17 at 19 53 12

But I am sorry I don't know what "content type" are you referring to, can you please clarify?

I can't share the project.

@ChrisMBarr
Copy link
Author

ChrisMBarr commented Mar 17, 2017

On my project, I can also confirm that the tsconfig file is included in the project file, and it is listed as content.

@kevindqc
Copy link

@max-favilli I think @mhegazy means the "Build Action" when talking about "content type".

Select tsconfig.json in the solution explorer. Bring up the Properties window (F4 key by default). There will be a Build Action property.

@max-favilli
Copy link

Thanks @kevindqc , @mhegazy yes "Build Action" is set to "content"
screen shot 2017-03-18 at 11 21 28

@mhegazy
Copy link
Contributor

mhegazy commented Mar 20, 2017

Can you share your virtual project structure with me, to get that you need to:

  • go to Tools > Options > Text Editor > TypeScript > Project, and check Display Virtual Projects when no Solution is loaded;
  • restart VS, and open a file in your project
  • you should now see a new node in your Solution Explorer under the name of TypeScript Virtual Project

@max-favilli
Copy link

@mhegazy like this?

screen shot 2017-03-21 at 02 08 13

Thanks for helping.

@kevindqc
Copy link

kevindqc commented Mar 21, 2017

I have the same problem. I can't share the project either, but I will try to provide as much information as possible.

I have typescript compilation disabled in the .csproj (<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>)
Instead it is compiled by npm run build:prod, which is in a build event.

The errors are not always there. I have been trying to make them appear (there were shown in VS, so I restarted VS). I've tried building the typescript project, building the solution, running tests, running code analysis, running code coverage, etc. Nothing triggers it. Then, just as I was writing the last sentence, the errors appeared. So it seems to be some background tasks that does it? It seems to happen around 2 minutes after I started a build (at least the 2-3 times I tried it out)

Timeline:
1:37:20 - Opened Visual Studio
1:37:30 - Started solution build
1:38:20 - Build finished
1:39:39 - Errors appeared

I noticed my project was still using the v2.0.3 Microsoft.TypeScript.Compiler and Microsoft.TypeScript.MSBuild nuget packages. I updated them to v2.2.1. So far, no error. Will update if I see the errors again. (Edit: I see the errors again, altough it took a bit longer, ~5 minutes, without me doing anything but building when I opened VS. Actually, I don't even need to build anything to get the error - just opening the project and waiting a few minutes shows the errors)

About those outdated nuget packages, is that normal? I think I got a popup to upgrade my project typescript tools when I installed the new version of typescript, but it seems it only updated <TypeScriptToolsVersion>2.2</TypeScriptToolsVersion>. Should it not also update the nuget packages?

@johnlee
Copy link

johnlee commented Mar 22, 2017

I just updated to Typescript 2.2.1 and having this same issue. Its flagging all the the node_modules but the tsc compiler completes without a problem.

@Hotell
Copy link

Hotell commented Mar 25, 2017

@mhegazy

@Hotell i am not seeing this locally, what am i missing?

right, it was fixed by wc-catalogue/blaze-elements@cdb94bf

sry for delayed answer and thx 4 help!

@max-favilli
Copy link

@Hotell @mhegazy Doesn't seems to work for me.
Maybe it works if you have d.ts files in a "definitions" folder?
But I don't have that. I have @types under "node_modules" which should be inheritedly excluded?

@johnlee
Copy link

johnlee commented Mar 27, 2017

@ChrisMBarr May I ask if you got this resolved yet? I've been following the thread and even did the fix suggested by @Hotell couple of posts up but I'm still getting these Errors in Visual Studios. They all seem to be coming from the node_modules folder.

@billti
Copy link
Member

billti commented Mar 27, 2017

Would you be able to enable detailed logging and send us the results? Set the system environment variable TSS_LOG to a value such as -file C:/temp/logs/tsserver.log -level verbose (make sure the logs folder exists). Then after you repro the problem, send/attach the log for analysis. (Note: It may contain data such as file paths and completion lists, so be sure there isn't any data you don't want to share in it).

In my testing I've occasionally seen the project system construct a view of the project that contains the wrong files, causing intermittent errors at unpredictable times. I'd like to see if that may be the cause here.

I can provide my email address if you'd rather send the files direct than upload them (billti at microsoft dot com). Thanks.

@ChrisMBarr
Copy link
Author

ChrisMBarr commented Mar 27, 2017

@johnlee no, still the same in VS2015. However, I've since started to use Visual Studio 2017 and I have no errors in there!

@max-favilli
Copy link

@billti Variable added, visual studio (2017) restarted, solution rebuilded, but the log file is not being created. What should I check?

@billti
Copy link
Member

billti commented Apr 6, 2017

Is it definitely a system environment variable (i.e. if you open a new command prompt and run SET TSS is the setting listed)? If not that, is the folder the log is to be written to already present (the logger won't create directories that don't exist). Other than that, its also safer to use forward slashes rather than backslashes in the path.

@baktun14
Copy link

baktun14 commented Apr 7, 2017

typescripterrors
:(

@matthew-dean
Copy link

matthew-dean commented Apr 22, 2020

@Abadii So.... it appears that the reason this was happening was that, even though the JS files were not included, they were failing because they were CommonJS modules.... 🤔 ....

In other words, once everything had proper imports / exports, I stopped getting this issue. So, based on the comments of this thread, and the sheer number of fixes, I think that this is one of those red herring errors. As in, when there's X type of JS / Intellisense / compiling issue, then VSCode throws this Cannot write file error. But, in my case, this was resolved with a solution OTHER than every other of the numerous solutions offered in this thread. 🤷‍♂️ And again, it threw this error on a file that did not exist and would not be replaced. So maybe errors are caching, and an unknown error throws a cached error? Something like that? I think someone needs to inspect the code around this particular error.

@leilapearson
Copy link

Commenting here because it is the first thing that came up in my google search.

I ran into this issue too. 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 causing the compilation errors. The third file is fine. So it looks like it might be related to how the import / export tree is affecting compilation.

marcalexiei added a commit to marcalexiei/ractive that referenced this issue May 12, 2020
@jasonk
Copy link

jasonk commented Jun 4, 2020

I just stumbled across this issue while having the same problem, and thought I'd add what mine turned out to be in case other people stumble across it the same way.

In my case, I have a monorepo with a separate tsconfig file for each package, that all extend from a base tsconfig. Each packages config has references entries that point to the path of the packages it depends on. I also have a tsconfig.json in the root of the repo that includes files: [] and has references to all of the package directories. This way I can run tsc -b --watch from the root and have it rebuild on changes throughout the entire project.

This worked fine for quite a while, then abruptly started throwing this error even though none of the configs had changed.

I finally tracked it down by attempting to build the one package that was being reported in the error by itself, rather than building the whole project.

Turns out the problem was that I had attempted to have the project import itself. The package name was @my-project/utils, and it worked fine until I moved some code from another package into a file in the utils package. That code included import stuff from '@my-project/utils'; and that was what caused the error. By changing to to import stuff from '.'; instead the error went away..

@Ghirigoro
Copy link

@jasonk This probably is just revealing my ignorance but wouldn't that only work if your monorepo is being bundled together by something that will resolve all those imports (e.g. webpack)? If your monorepo is composed of modules that can be published and used independently changing to relative imports would cause errors no?

@jasonk
Copy link

jasonk commented Jun 4, 2020

@Ghirigoro No, the problem wasn't that I was using a package path, it's that it was using a package path to import a package from within itself. Even without Typescript, that won't work.

Basically what I had done was the equivalent of this:

$ mkdir problem
$ cd problem
$ npm init -y
$ echo 'console.log( "WORKED!" );' > index.js
$ echo 'require( "problem" );' > test.js

If you then try to run that with node:

$ node ./test.js
internal/modules/cjs/loader.js:985
  throw err;
  ^

Error: Cannot find module '/Users/jasonk/problem/test.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
    at Function.Module._load (internal/modules/cjs/loader.js:864:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

If you replace the package name then it works correctly:

$ echo 'require( "." );' > test.js
$ node ./test.js
WORKED!

I suspect that what happened was that when the build was run, all of the other packages that were importing from @my-project/utils had that import resolved to packages/utils (because they had the correct entries in the references array in tsconfig), so they built normally. But because that package wasn't supposed to be importing itself that import got resolved to node_modules/@my-project/utils, which was a symlink to packages/utils, but TypeScript didn't detect that they were actually the same project so it tried to build it twice, but since both builds had the same output directory I ended up with this error.

@edcu11
Copy link

edcu11 commented Jul 7, 2020

This happens when the declaration files are not excluded from the build. Whenever this occurs, the builder tries to build the existing ".d.ts" files and replace them with the same filename. So that's why you will get the error: Cannot write file ... because it would overwrite input file.

To prevent this, you can exclude your "outDir":"build" in jour tsconfig.json file:

"exclude": [
    "build",
    ....
]

or if you don't have outDir defined exclude all d.ts. extension files:

"exclude": [
    "**/*.d.ts"
    .....
]

Hope this helps

Excluding my build dir solved the issue for me

@ildar-dev
Copy link

Most likely, this happens when you try to run one project with two nodes.
For this hypothesis, you can test the count of processes on your computer named "node" after run build.
What I did to solve the problem:
Step 1.
Compare
node -v
and
nvm -ls, the version in use.
In terminal set current node version:
nvm use {neededVersion}
In principle, delete unnecessary versions of the node in nvm (this will help your IDE to automatically determine the normal version of the node).
Step 2.
Determine the current node in your IDE. i.e. in WebStorm:
Preferencies->Languages & Frameworks -> Node.js and NPM: Node interpreter - set needed version.
(In addition, you can check the version of the node for each item everywhere (Typescript))

Also, this problem can occur if the npm packages or git submodules used their own node inside

@munishusa
Copy link

I was facing this issue for Webdriver IO / WDIO project. It got resolved when I removed the "wdio.config.js" from "include" in tsconfig.json.

@aakilfernandes
Copy link

aakilfernandes commented Dec 9, 2020

Fixed this by adding "types": "./{name of dist directory}/**/**.d.ts" to my package.json file

@christian-bromann
Copy link

I am dealing with the same issue and the problem is only happening if I have:

"types": "./build/index.d.ts"

in my package.json. Anyone has a clue why that is?

@Arkanic
Copy link

Arkanic commented Jan 15, 2021

If tsc runs fine the first time then errors with this error every time after that it's because it doesn't want to overwrite the compiled files. Just delete the folder then run tsc again, and it'll be fine.

@lazyd3v
Copy link

lazyd3v commented Feb 1, 2021

Is there any way to overwrite the compiled files? Pass some special option in tsconfig maybe? Adding this in package.json

"prebuild": "rm -rf ./dist"

looks hacky in my opinion

@Ghirigoro
Copy link

@Arkanic Deleting the folder is a reasonable workaround, but it has downsides. For example if you have an incremental build, or there's other expensive steps required to prepare the build folder. It seems like the problem here is that TS doesn't listen to you when you tell it to exclude something.

@Limule
Copy link

Limule commented Feb 13, 2021

Same problem, I solve It by doing this :

Add in package.json
"files": [ "dist/**/*" // Your build repos ]

and add in tsconfig.json

"include": [ "src", "index.ts", // put your files here ],

ptbrowne added a commit to cozy/cozy-client that referenced this issue Mar 1, 2021
Importing recursively leads to tsc errors

> Cannot write file 'client/packages/cozy-client/types/models/note.d.ts' because it would overwrite input file.

microsoft/TypeScript#14538 (comment)
@Sajjon
Copy link

Sajjon commented Mar 11, 2021

Using a mono repo? Check for (implicit and unwanted/accidental) cyclic dependencies! That was what caused this issue for me. I wrote an answer here on StackOverlflow

@mkermani144
Copy link

If you want to test another solution and check if it works, here is mine:

Check if these conditions are met for your project:

  1. You have allowJs: true and declaration: true in your tsconfig
  2. You have set package.json main file to the compiled js file (e.g. main: './dist/index.js')
  3. You have imported your root index.ts using relative imports (e.g. import something from '../..') somewhere in your project

Then try changing from

import something from '../..' // Or any other relative import

to

import something from '../../index' // No .ts needed

Hope it helps.

@fider-apptimia
Copy link

3. You have imported your root `index.ts` using relative imports (e.g. `import something from '../..'`) somewhere in your project

Then try changing from

import something from '../..' // Or any other relative import

to

import something from '../../index' // No .ts needed

Hope it helps.

@mkermani144 thanks!

jamie-pate added a commit to jamie-pate/typed-cli that referenced this issue Oct 8, 2021
Due to the way that typescript works in nodejs module resolution, the
index.d.ts file will load .ts files incorrectly if you publish the .js
and .d.ts files in alongside the .ts files.

Unfortunately this causes my version of typescript to attempt to compile
the module's .ts files which are not working with typescript 4.4.3!

See microsoft/TypeScript#10704 which considers
this to be 'working as intended'

The solution is to avoid outputting your .js and .d.ts files into the
same location as the .ts files.

Unfortunately, adding 'dist/index.d.ts' to the types in the package.json
caused all imports of '..' (pointing to the /index.ts file) to trigger
this issue:
microsoft/TypeScript#14538 (comment)
which I solved by changing those to '../index' or '../../index' etc.

Here is the error I get while trying to compile my project which uses
"typescript": "^4.4.3" after `npm install typed-cli:

```
node_modules/typed-cli/src/pipeline.ts:23:9 - error TS2322: Type
'unknown' is not assignable to type 'Error | undefined'.
  Type 'unknown' is not assignable to type 'Error'.

23         return e;
           ~~~~~~~~~

Found 1 error.
```
@mati-o
Copy link

mati-o commented Oct 18, 2021

Just had the same issue for d.ts files. While everything was set correctly in terms of tsconfig dirs, I had this error because by accident my source code imported from the dist dir rather than src, so it was kinda circular I guess. Changing the import fixed this.

Hope someone will find this comment useful.

@craigphicks
Copy link

@mati-o - "because by accident my source code imported from the dist dir rather than src"

To be exact, the auto completion took over at

import { xxx } from

and incorrectly completed to

import { xxx } from '../dist/src'

or something like that. I think that is a bug.

@chrisdev3001
Copy link

chrisdev3001 commented May 12, 2024

If you want to test another solution and check if it works, here is mine:

Check if these conditions are met for your project:

  1. You have allowJs: true and declaration: true in your tsconfig
  2. You have set package.json main file to the compiled js file (e.g. main: './dist/index.js')
  3. You have imported your root index.ts using relative imports (e.g. import something from '../..') somewhere in your project

Then try changing from

import something from '../..' // Or any other relative import

to

import something from '../../index' // No .ts needed

Hope it helps.

I've spent a 2 hours looking for configs solutions, and this post save my day, thanks man!

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