Sending error messages to a file in TypeScript

Wally96333 71 Reputation points
2021-09-06T16:58:46.57+00:00

Hi all,

I am trying to run the TypeScript transpiler on some code that is loaded from a file
in a separate process.

        //  THIS ProcessStartInfo set threw the Win32Exception listed below.

        //psi = new ProcessStartInfo
        //            ("tsc", " --project C:\\a01_temp\\tsconfig.json");

        //    System.ComponentModel.Win32Exception
        //    HResult=0x80004005
        //    Message=The system cannot find the file specified
        //    Source=System
        //    StackTrace:
        //   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
        //   at NS_TypeScript.TypeScriptCompiler.Compile(String P_tsPath, CLS_Options P_options)
        //     in K:\Software Development\CLS_TypeScript_01.cs:line 235
        //   at NS_TypeScript.Program.Main(String[] args)
        //     in K:\Software Development\CLS_TypeScript_01.cs:line 48

I then tried writing the command to a batch file, then running the batch file
in a separate process. The transpiler did execute, but I cannot get error messages
into a file. If I run the generated batch file from a command window, it does indeed
write the error messages to a file.

        L_cmd = "tsc" + $" --project C:/a01_temp/tsconfig.json  > cmpl_diag_02.txt 2>&1";

        //  Creating the batch file.
        L_bat_TS_file_spec = Path.Combine (L_file_path, "test.bat");
        using (StreamWriter
               L_SW = new StreamWriter (L_bat_TS_file_spec))
        {
            L_SW.Write (L_cmd);
        }
        psi = new ProcessStartInfo (L_bat_TS_file_spec);

The tsconfig.json is listed at the bottom. If there is an attribute that
would specify a file to which the error messages are to be written,
then that might fix my problem. I do not see anything in the documentation
that would indicate that there is any such attribute.

Here is my code, with comments as to what I tried, and what happened.


static class TypeScriptCompiler
{
static string L_file_path;
static string L_TS_file_spec;
static string L_bat_TS_file_spec;
static string L_JS_file_spec;
static string L_tsc_file_spec;

public static void Compile(string P_tsPath, CLS_Options P_options = null)
{
    L_file_path = @"C:\a01_temp";
    L_TS_file_spec = Path.Combine (L_file_path, "test.ts");
    L_JS_file_spec = Path.Combine (L_file_path, "test.js");
    L_tsc_file_spec
      = "C:\\Users\\Ron Smith\\AppData\\Roaming\\npm\\node_modules\\typescript\\bin\\tsc";

    string L_cmd;

    Process L_tsc_process = new Process();
    ProcessStartInfo psi;

    //  THIS ProcessStartInfo set threw the Win32Exception listed below.

    //psi = new ProcessStartInfo
    //            ("tsc", " --project C:\\a01_temp\\tsconfig.json");

    //    System.ComponentModel.Win32Exception
    //    HResult=0x80004005
    //    Message=The system cannot find the file specified
    //    Source=System
    //    StackTrace:
    //   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
    //   at NS_TypeScript.TypeScriptCompiler.Compile(String P_tsPath, CLS_Options P_options)
    //     in K:\Software Development\CLS_TypeScript_01.cs:line 235
    //   at NS_TypeScript.Program.Main(String[] args)
    //     in K:\Software Development\CLS_TypeScript_01.cs:line 48

    //  I do not know why the process could not find "tsc" like it should have.
    //  If anyone could figure this out, it might solve my problem.

    //  NEXT APPROACH: Try writing the command to a batch file,
    //  then execute the batch file.

    //  The batch file DID execute properly,
    //  and the errors were displayed in the command box,
    //  but the output was not redirected when this was run from a separate process
    //  within .NET (C#).

    //  The error messages were indeed redirected to the given log file (cmpl_diag.txt)
    //  when the generated batch file was run directly from the command line.

    //  THIS is stuff that was tried before but did not work,
    //  including a pipe to another process that might handle the redirect.
    //L_cmd = L_tsc_file_spec + $" --project C:/a01_temp/tsconfig.json";
    //L_cmd = "cmd tsc" + $" --project C:/a01_temp/tsconfig.json";
    //L_cmd = "tsc" + $" --project C:/a01_temp/tsconfig.json  cmpl_diag.txt 2>&1";
    //L_cmd = "tsc" + $" --project C:/a01_temp/tsconfig.json | more >cmpl_diag_02.txt";
    //L_cmd = "tsc" + " --project C:\\a01_temp\\tsconfig.json | more >cmpl_diag_02.txt";
    //L_cmd = "tsc" + " --project C:\\a01_temp\\tsconfig.json   > cmpl_diag_02.txt 2>&1";

    //  THIS is the most-recent attempt.

    L_cmd = "tsc" + $" --project C:/a01_temp/tsconfig.json  > cmpl_diag_02.txt 2>&1";

    //  Creating the batch file.
    L_bat_TS_file_spec = Path.Combine (L_file_path, "test.bat");
    using (StreamWriter
           L_SW = new StreamWriter (L_bat_TS_file_spec))
    {
        L_SW.Write (L_cmd);
    }
    psi = new ProcessStartInfo (L_bat_TS_file_spec);

    // run without showing console windows
    psi.CreateNoWindow = true;
    psi.UseShellExecute = false;
    psi.Verb = "runas";

    // redirects the compiler error output, so we can read
    // and display errors if any
    psi.RedirectStandardOutput = true;
    psi.RedirectStandardError = true;

    L_tsc_process.StartInfo = psi;
    L_tsc_process.Start();

    // SHOULD read the error output
    //  ... but nothing was read.
    var msg = L_tsc_process.StandardError.ReadToEnd();

    // make sure it finished executing before proceeding 
    L_tsc_process.WaitForExit();

    // TRIED moving this down below the "WaitForExit"
    //  ... but still nothing was read.
    //var msg = L_tsc_process.StandardError.ReadToEnd();
}

}


C:\a01_temp\tsconfig.json

{
    "compilerOptions": {
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "es5",
        "allowJs": true,
        "removeComments": false,
        "typeRoots": [
            "C:\\TypeScript\\a16_bin\\node_modules\\@types"
        ]
    },
    "include": [
        "C:\\a01_temp\\test.ts"
    ]
}

THANKS!

Developer technologies C#
{count} votes

Accepted answer
  1. P a u l 10,761 Reputation points
    2021-09-06T17:18:54.46+00:00

    What happens if you specify an absolute path for your stdout redirect?

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Wally96333 71 Reputation points
    2021-09-06T18:39:49.767+00:00

    Specifying the absolute file spec did the trick for using the batch file approach.

    C:\a01_temp\cmpl_diag_03.txt

    THANK YOU Viorel and Paul!!!

    There should be an attribute in the config file though.
    I have posted a feature request on GitHub for that
    (IF it does not exist and I missed it).

    THANKS AGAIN!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.