Thursday, September 8, 2016

The dreaded 'error MSB6006: "tsc.exe" exited with code 1.' message when building

TypeScript is an amazing tool when working with JavaScript in MVC projects. If you haven't checked it out yet you really should. It makes working with JavaScript so much better and easier. Pluralsight has a terrific course on it here.

This blog post isn't about TypeScript and all its greatness. But rather, it is about quickly debugging build errors when building your web project in Visual Studio and you get the dreaded "tsc.exe exited with code 1".

Just like any build error, the built in tools are usually your best bet for debugging the issue. Whether it is a command in your project's pre-build or post-build events; or a tool like TypeScript failing as part of the build. Usually when calling an external tool from Visual Studio, I have always seen the error reported as "error code 1" or something similar. As an example, try to run the gacutil command from a post-build event when you do NOT run Visual Studio as an admin to see a quick example of this.

Anyway, back to the issue at hand. Today, I pulled down the latest code from MASTER and merged into my branch, build the solution and (insert sad horn sound here) I get the following lone build error:

'tsc.exe' exited with code 1.

To get better insight into the build error, I turn to the tools already at my disposal in Visual Studio. This is a build error, so what better tool to use than MSBUILD? First step, let's change the build logging level so that we can see exactly what is going on and what _exact_ command is being run with tsc.exe.

Go into Visual Studio, Tools - Options, Projects and Solutions - Build and Run, then change the MSBuild project build output verbosity to Diagnostic, click Ok.
Build and Run Settings

Rebuild the solution and then we head to the Output window. Use CTRL-F to bring up the Find window in the Build Output window, and enter tsc.exe. Make sure that you limit the search to the current window. Click the find icon.

This will take you to the first instance of the tsc command:
If you scroll down from here you'll see that this is setting up all of your options for the TypeScript command. The key parameters to note are:
  • FullPathsToFiles - this parameter contains all of the TypeScript (*.ts) files that will be used to generate your JavaScript files.


  • ToolsVersion- the version of TypeScript to use. I'm currently using version 1.8. The default location for installing TypeScript is:  C:\Program Files (x86)\Microsoft SDKs\TypeScript, and then the sub-folder will be which ever version(s) of TypeScript you have installed on your development environment. I have two sub-folders: 1.7 and 1.8,


Click on the find icon again, and you will be taken to the actual TypeScript command that Visual Studio is running to generate your JavaScript files. The command that is run for my particular build is:

 C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.8\tsc.exe  --module AMD --sourcemap --target ES5 --noEmitOnError --locale en-US "[All files that were in my FullPathsToFiles]"

I removed the entire block of files from the command just to shorten things up a bit for illustration. I don't want my entire blogpost to be the 40 or so TypeScript files from my build command.  :)

Now that we know what command Visual Studio is trying to run for us, we can run it ourselves in a command window and identify the specific build error, and fix it. I run the Developer Command Prompt for VS 2015 as an Administrator. Copy and paste the full tsc.exe command from my Visual Studio 2015 Output Window, to the command prompt and run it.

NOTE: I had to add double quotes around the first part of the command: "C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.8\tsc.exe" because of the spaces in the command path. YMMV.

I run the tsc.exe command and my build error becomes crystal clear:  in my latest merge from MASTER a file was deleted from the repo, but was still referenced in my web project:

"Cannot open file 'C:\{My Path}\{ProjectName}.Web\Scripts\typings\moment\moment-node.d.ts'.
error TS6053: File 'C:/{My Path}\{ProjectName}.Web/Scripts/typings/moment/moment-node.d.ts' not found."

I go to the folder path in my web project, and the .TS file is not there.



A quick phone call to my team and the TS file is added back into the MASTER repo, I merge that into my branch and the build compiles.

So, that's how to resolve the very specific tsc.exe build error. But, I hope this also helps you to debug any build error that is not very detailed (SomeTool exited with error code 1) when Visual Studio runs an "external" tool for you as part of the build process.

See ya around.

Tim

PS: Be sure to change your MSBuild logging level back to minimal. :)