10
votes

This thread (Problem capturing error output) gives you a taste of the problem I'm struggling with. I'm trying to run grunt from MSBuild and the grunt errors are not displayed in the Visual Studio output window. I have a .NET project in Visual Studio Express 2012 for Web. I have imported an external project into the project build file with the IMPORT tag and in the imported project I have an Exec task attempting to run grunt. I obviously want to see the error messages that grunt outputs in my Visual Studio output window without too much fuss. I found an extremely simple workaround that at least sends the output to a text file.

grunt.cmd > grunt-output.txt

This output file is in my .NET project folder somewhere so a quick refresh and double click allows me to open the output file and see a slightly garbled version of the grunt output in Visual Studio.

As an example I'm running a lint task on the grunt.js file, which contains things which JSHint would object to. I deliberately didn't put a semi-colon after var hello and so you get the error message Missing semicolon.

From the command line I get a nicely formatted error message.

Running "lint:files" (lint) task
Linting grunt.js...ERROR
[L2:C10] Missing semicolon.
var hello

<WARN> Task "lint:files" failed. Use --force to continue. </WARN>

Aborted due to warnings.

When I run it from Visual Studio, the output file contains this cluttered format:

    [4mRunning "lint:files" (lint) task[24m
     Linting grunt.js...[31mERROR[39m
     [31m[[39m[33mL2[39m[31m:[39m[33mC10[39m[31m][39m [33mMissing semicolon.[39m
     var hello[31m[7m [27m[39m

     [31m<[39m[33mWARN[39m[31m>[39m [33mTask "lint:files" failed. Use --force to continue.     [39m [31m</[39m[33mWARN[39m[31m>[39m

     [31mAborted due to warnings.[39m

Does anyone recognise what all those square brackets and numbers are doing, and can anyone think of a command line switch or grunt switch or node.js switch that would interpret them and turn them into formatting? The don't look like some kind of encoding, they look more like tags to suggest to the command line environment how to format the message. Please don't suggest running some kind of regular expression replace function. I want something quick and easy otherwise it would become more trouble than it's worth.

UPDATE: this link Output gets cut off if piped into another application is pointing to a problem further upstream in node dating back 10 months. While that's getting sorted out it would be nice to at least get a more readable output file.

2
It looks like you already figured out how to turn them off, but just in case you were curious, those codes are ANSI escape codes. – Joe Chung

2 Answers

8
votes

This thread on the grunt message board Pipe-redirecting Grunt's output is broken addresses this issue perfectly and provides a quick workaround while we wait for the overall issue to get fixed. They are escape codes to colour the output and the workaround is to use the --no-color option to remove colouring.

When I run this command from MSBuild

grunt.cmd --no-color > grunt-output.txt

I get nicely formatted output with exactly the same content as the command line:

Running "lint:files" (lint) task
Linting grunt.js...ERROR
[L2:C10] Missing semicolon.
var hello 

<WARN> Task "lint:files" failed. Use --force to continue. </WARN>

Aborted due to warnings.

I can live without the colour. It would be nice if this could be sent to the output window, though, because MSBuild throws what seems like an error in the build process when in fact it's just JSHint tactfully hurting my feelings about my JavaScript.

0
votes

In response to "I obviously want to see the error messages that grunt outputs in my Visual Studio output window without too much fuss."

I'd have a look at VsCommandBuddy ... it helps you integrate your grunt (and any other command for that matter) right inside visual studio. Commands are configured per solution/project, and at the time of this writing are being made available via menus, toolbar, shortcuts and the quicklaunch ...

http://visualstudiogallery.msdn.microsoft.com/f5da988e-2ec1-4061-a569-46d09733c668

It's a scratch-my-own-itch project. It helps me getting thins done. In every solution I open in visual studio, I simply get presented al the external commands I put togheter for that solution / or porject.

Output goes thorugh the outputwindow as desired. The no-color option for grunt removes all the noise.

Hope it helps!!