microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.99k stars 12.48k forks source link

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

Closed ChrisMBarr closed 7 years ago

ChrisMBarr commented 7 years ago

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 commented 7 years ago

I also get same problem.

mhegazy commented 7 years ago

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

ChrisMBarr commented 7 years ago

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 commented 7 years ago

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

Hotell commented 7 years ago

We have the same problem https://github.com/wc-catalogue/blaze-elements/issues/299 although with type definitions write access

mhegazy commented 7 years ago

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

Hotell commented 7 years ago

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

BrainSlugs83 commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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

max-favilli commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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

kevindqc commented 7 years ago

@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 commented 7 years ago

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

mhegazy commented 7 years ago

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

max-favilli commented 7 years ago

@mhegazy like this?

screen shot 2017-03-21 at 02 08 13

Thanks for helping.

kevindqc commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

@mhegazy

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

right, it was fixed by https://github.com/wc-catalogue/blaze-elements/commit/cdb94bf8feb3a1ad7e21e6fce243e3322c1334cc

sry for delayed answer and thx 4 help!

max-favilli commented 7 years ago

@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 commented 7 years ago

@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 commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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

billti commented 7 years ago

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 commented 7 years ago

typescripterrors :(

max-favilli commented 7 years ago

@billti Ok, I just checked and the log files are there now. Please find them here attached tss-log.zip

billti commented 7 years ago

Thanks. I took a look in that log, and I don't see that error being reported by any calls in it. Did the error definitely occur in the time frame this log covers?

max-favilli commented 7 years ago

@billti Yes, the errors are occurring at all times: screen shot 2017-04-11 at 01 25 24

They are just there permanently. And refreshed on every single build.

billti commented 7 years ago

All of those errors related to incorrect variable declarations (a separate issue we have already fixed for an upcoming release). This issue is about Cannot write file... per the title and screen shots above. Do you not have the Cannot write file... issue?

max-favilli commented 7 years ago

@billti after posting my previous message (which I left there as a demonstration of my stupidity) I realized the errors are of a different kind. I can only guess the visual studio update I installed few days ago solved the original issue. But I am not sure. I got rid of these errors from the previous message just removing the types from @types. Thanks a lot.

kevindqc commented 7 years ago

@billti I'm trying to have some logs to send you, but none are created. I have set TSS_LOG to -file C:/temp/logs/tsserver.log -level verbose in my system (not user) variables.

C:\temp\logs exists, and I gave full control to Everyone.

If I restart my VS2015, nothing is created inside C:\temp\logs even after I get all my Cannot write file... errors. If I try to type node node_modules\typescript\lib\tsserver.js (and CTRL+C immediately) in a new command prompt, I do get two log files.

billti commented 7 years ago

Sorry @kevindqc , I didn't realize you were on VS 2015. That logging only applies to VS 2017 (which now uses tsserver.js out of process for the language service).

Going back and looking at your issue - if you are seeing the error list randomly change, I believe that is the same issue @zhengbli just fixed in https://github.com/Microsoft/TypeScript/pull/15080 .

kevindqc commented 7 years ago

@billti np. I think this issue is only hapenning on VS2015, no? The only user with the problem that was using VS2017 was having different errors related to typings. Someone who switched from VS2015 to VS2017 said the problem went away.

Also, the error list doesn't randomly change - it gets populated at a random time, once, with all those "Cannot write .." errors that this issue is about. Are you sure that #15080 fixes it? I see nothing about this issue except the reference you made to it yesterday? How can I test it? I see there's a 2.3 RC but it was released 9 days ago while the fix was merged 2 days ago :(

Also I noticed the errors stay in my error list even after I close the solution.

Also the person who posted his typescript virtual project was the one with the different errors, so I imagine it wasn't helpful. Here is mine: image

Is node_modules supposed to be there? It is in my tsconfig excludes. It doesn't contain everything that my physical node_modules folder has though (14 subfolders vs 854 subfolders in my node_modules folder)

{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "removeComments": false,
    "sourceMap": true,
    "suppressImplicitAnyIndexErrors": true,
    "target": "es5",
    "baseUrl": "./src",
    "skipLibCheck": true,
    "paths": {
    },
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "exclude": [
    "node_modules",
    "dist",
    "typings"
  ],
  "types": [
    "core-js",
    "jasmine",
    "lodash",
    "node",
    "webpack"
  ],
  "awesomeTypescriptLoaderOptions": {
    "forkChecker": true,
    "useWebpackText": true
  }
}
kevindqc commented 7 years ago

Haven't had the errors so far after upgrading to the TS2.3 tools. Thanks! Edit: Nop. The errors are still there after all, just took longer to appear.

IvanYur4enk0 commented 7 years ago

Seems that this issue related with this issue. Sample solution to reproduce - uploaded.

borislemke commented 7 years ago

I found a fix for me. In my case I used outDir and rootDir without specifying files array. When adding the path of outDir to the exclude array everything seems to be working normally.

{
    "compilerOptions": {
        ...,
        "outDir": "./dist",
        "rootDir": "./src",
    },
    "exclude": [
        "node_modules",
        "dist" <-- I had to add this to fix the errors
    ]
}

Maybe TypeScript is watching over the content of the dist folder as well even though it's set as the outDir.

billti commented 7 years ago

If you have your outDir under your project root, and you are just including everything under the root, then this would be expected. Generally you want to have your build output somewhere other than within the project source folder.

billti commented 7 years ago

Looks like all the problems above are addressed in this thread or linked issues. Let me know if that's not the case and I'll reopen. Thanks!

borislemke commented 7 years ago

@billti I assume that your comment is in response to my previous comment? If so, thanks for clarifying. This makes the matter more obvious to me now as I assumed when specifying the rootDir the compiler will only watch that particular folder and exclude everything else. But the current behavior makes sense.

chizdamasii commented 7 years ago

best solution is from borislemke . { "compilerOptions": { ..., "outDir": "./dist", "rootDir": "./src", }, "exclude": [ "node_modules", "dist" <-- I had to add this to fix the errors ] }

syntiro commented 7 years ago

I ran into the same issue and found that one of my imports was incorrectly referencing the class in my dist folder E.G import {ClassName} from "../../dist/ClassName";

As the importing class was in the same folder I changed it to: import {ClassName} from "./ClassName";

and everything is compiling again :)

soda-sorcery commented 6 years ago

We had a pre-defined 'dist' folder in our app. Deleting that fixed it for me.

steinhoff1989 commented 6 years ago

I think this is the underlaying problem (the last Windows Creators Update): visual-studio-2015-deletes-file-on-save-cordova-solution

jbgraug commented 6 years ago

Just add you "dist" folder to the exclusions list in tsconfig.json ex: "exclude": ["node_modules", "dist"]

intolerance commented 6 years ago

I corrected this by adding an Include section:

"include": [ "*.ts", ], "exclude": [ "node_modules" ]

uglycoyote commented 6 years ago

I ran in to this as well when using an outDir which was under my sources directory.

How is it that the typescript compiler does not know by default that it should not try to compile things in the outDir? That seems strange. Adding the outdir to the exclusion list did fix it though.

geomorillo commented 6 years ago

i have this problem with netbeans lol 👎