TypeScript – Emit Error: Write to file failed | How to Resolve ?

TypeScript – Emit Error: Write to file failed | How to Resolve ?

Recently I came across this problem while configuring the TFS build server for Typescript. You may encounter “Emit Error: Write to file Failed” error while Typescript Complier (TSC) compiles the TS files and generate the associated JavaScript files . This happen due to the already pre-existing compiled JavaScript files that are already checked in, and they are in locked / read-only mode; hence TSC wont be able to overwrite.   You can easily reproduce this error in your local system by just marking any Typescript associated (complied ) JavaScript file as read-only and rebuild the project / solution file.

Emit Error: Write to file failed

This easiest option to avoid this problem to remove all checked in JavaScript (.js) and the map (.jsmap) files from the source control.  Because those are really not necessary to be checked in.

TypeScript and Its associated files

You just need to check-in only the TypeScript (.ts) file in your source control.  TSC complier will take care of generating the JS in build server ( you need to configure that) or in local ( that you already have installed).  So, here is your quick fix:

  1. Remove all .js and .jsmap files that are generated off .ts files from your project. ( Note : only .js can do the job for you as well)
  2. Check in your changes ( removing the .js and .jsmap) to TFS.

Now,  if your build server is configured for the Typescript run, TSC will run and generate the JavaScript files.

Here is another alternative solution, in case you don’t want to remove the JS file. Because, if you remove the JS for time being, and any other developer check in those file again into TFS, the same error will come occur. (more over those are hidden files, so if you don’t check-in carefully, they can be checked-in in TFS)

So, here is another trick for you. In this case,  you can run a Pre-Build event, that will remove all read-only attributes  of the JS files for that project.

1. Right Click on the Project and Open the Project Property Window

2. Select the Build Event Tab

3. Write the following commands in the pre-build event of the project.

attrib -r /s "$(ProjectDir)*.js"

Adding Pre-Build Event to remove readonly attributes

This will ensure, all the JS file release the Read-Only attributes and there will not be any error for write to file failed.

Hope this helps.

Feel free to share your comments.

Thanks,

Abhijit

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.