Search code examples
npmnant

NAnt exec command garbling output


When I execute a batch file that calls npm from the command line I see nicely formatted output, such as:

├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected]
├── [email protected]
├── [email protected] ([email protected], [email protected], support
├── [email protected] ([email protected], [email protected], end-of-
├── [email protected] ([email protected], [email protected], [email protected],
├── [email protected] ([email protected], [email protected], [email protected], defa
└── [email protected] ([email protected], [email protected], array-uni
[email protected], [email protected], [email protected], [email protected])

However, when I call the same batch file from a NAnt script, using the exec task, the output becomes garbled (even if I use the "output" switch to pipe output to a file):

 [exec] Ôö£ÔöÇÔöÇ [email protected]
 [exec] Ôö£ÔöÇÔöÇ [email protected]
 [exec] Ôö£ÔöÇÔöÇ [email protected]
 [exec] Ôö£ÔöÇÔöÇ [email protected]
 [exec] Ôö£ÔöÇÔöÇ [email protected]
 [exec] Ôö£ÔöÇÔöÇ [email protected] ([email protected])
 [exec] Ôö£ÔöÇÔöÇ [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])
 [exec] Ôö£ÔöÇÔöÇ [email protected]
 [exec] Ôö£ÔöÇÔöÇ [email protected]
 [exec] Ôö£ÔöÇÔöÇ [email protected] ([email protected], [email protected], [email protected])
 [exec] Ôö£ÔöÇÔöÇ [email protected] ([email protected], [email protected], [email protected], [email protected])
 [exec] ÔööÔöÇÔöÇ [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], t
[email protected])

Is there a workaround?


Solution

  • This behaviour is caused by the internals of the NAnt exec task not setting an encoding on the StandardOutput stream reader. I have replicated the behaviour with my own console app.

    The method in question is here: https://github.com/nant/nant/blob/master/src/NAnt.Core/Tasks/ExternalProgramBase.cs#L511

    What's missing is a way to set the encoding on the ProcessStartInfo, such as:

    StandardErrorEncoding = Encoding.UTF8
    

    The only workaround I can see (short of submitting a pull request to Nant) is to write a custom task. It's as simple as this:

    [TaskName("customexec")]
    public class CustomExecTask : ExecTask
    {
        protected override void PrepareProcess(Process process)
        {
            base.PrepareProcess(process);
            process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
            process.StartInfo.StandardErrorEncoding = Encoding.UTF8;
        }
    }
    

    I've struggled to load my custom task any other way than using the loadtasks task, however.