Skip to content

Commit

Permalink
(consoleapp) Fix exception in mintty-based shells on Windows (#278)
Browse files Browse the repository at this point in the history
Closes #107
  • Loading branch information
perlun authored Feb 15, 2022
1 parent 44485bb commit ab3c391
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
4 changes: 0 additions & 4 deletions docs/download/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ If you have a previous build installed and want to overwrite it, use the followi
<pre><code class="lang-shell hljs"><span class="hljs-meta">$ </span><span class="bash">curl -sSL https://perlang.org/install.sh | sh -s -- --force</span>
</code></pre>

**Note**: If you are running the installer in Git Bash on Windows, running `perlang` after installation will unfortunately not work in the Bash shell (because of limitations preventing us from reading individual keystrokes in Git Bash). Please close the `bash` session after installation and open a plain `cmd` shell instead (Command Prompt on the Start menu). In that shell, `cd %userprofile%\.perlang\nightly\bin`. You should then be able to run `perlang` to open up the Perlang REPL.

For more details about this bug, please see https://github.com/perlang-org/perlang/issues/107.

## Video tutorial

For those of you who prefer to learn by watching videos, here's a short screencast (courtesy of [Asciinema](https://asciinema.org/)) which shows what the installer looks like when you run it:
Expand Down
21 changes: 19 additions & 2 deletions src/Perlang.ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
using Perlang.Parser;
using ParseError = Perlang.Parser.ParseError;

#if _WINDOWS
using System.Diagnostics;
#endif

namespace Perlang.ConsoleApp
{
public class Program
Expand Down Expand Up @@ -201,7 +205,7 @@ public static int MainWithCustomConsole(string[] args, IConsole console)
replMode: true,
standardOutputHandler: console.Out.WriteLine,
disabledWarningsAsErrors: disabledWarningsAsErrorsList
).RunPrompt();
).RunPrompt(args);

return Task.FromResult(0);
}
Expand Down Expand Up @@ -328,8 +332,20 @@ private int RunFile(string path)
return (int)ExitCodes.SUCCESS;
}

private void RunPrompt()
#pragma warning disable S1172 // Remove this unused method parameter 'args'.
private void RunPrompt(string[] args)
{
#if _WINDOWS
if (Console.IsInputRedirected)
{
Console.WriteLine("Input redirection detected, relaunching with winpty");
var process = Process.Start("winpty", Environment.ProcessPath! + String.Join(' ', args));
process!.WaitForExit();

return;
}
#endif

PrintBanner();
ReadLine.HistoryEnabled = true;
ReadLine.AutoCompletionHandler = new AutoCompletionHandler();
Expand All @@ -349,6 +365,7 @@ private void RunPrompt()
Run(command, CompilerWarningAsWarning);
}
}
#pragma warning restore S1172

internal int Run(string source, CompilerWarningHandler compilerWarningHandler)
{
Expand Down

0 comments on commit ab3c391

Please sign in to comment.