rikvdkleij / intellij-haskell

IntelliJ plugin for Haskell
https://rikvdkleij.github.io/intellij-haskell/
Apache License 2.0
1.31k stars 94 forks source link

Stack run does not print text in terminal #378

Open AllanWang opened 5 years ago

AllanWang commented 5 years ago

This isn't completely related to intellij-haskell, but the run task is created from this plugin, so perhaps someone knows something about this.

I have a very simple test:

main = do
  putStrLn "Hello, what's your name?"
  name <- getLine
  putStrLn ("Hey " ++ name)

If I run it in my terminal or in the intellij terminal, using either stack run or stack build --exec ***.exe (what the plugin template uses), it works.

However, if I run the template, which launches the run tab, I don't get the hello prompt until I type something and press enter. When I first tried this, I thought my project failed to build for many minutes.

Does anyone have any idea what's going on here?

Using v1.0.0-beta40 on macOS

Toerktumlare commented 5 years ago

i have the exact same problem

rikvdkleij commented 5 years ago

Thanks for reporting!

I can reproduce this issue but I can not find the cause.

develop7 commented 5 years ago

Affects me too on Linux. My guess is the executable somehow gets a non-pty stdout/err which causes output buffering to be enabled automatically.

rikvdkleij commented 5 years ago

Do not know if it really solution but got the example in this way working:

import System.IO

main = do
        hSetBuffering stdout NoBuffering
        putStrLn "Hello, what's your name?"
        name <- getLine
        putStrLn ("Hey " ++ name)
develop7 commented 5 years ago

changing the sample source in following way

module Main where

import System.Posix.IO (stdInput, stdOutput)
import System.Posix.Terminal (queryTerminal)

import Lib

main :: IO ()
main = do
  putStrLn "Hello, what's your name?"
  name <- getLine
  inTty <- queryTerminal stdInput
  outTty <- queryTerminal stdOutput
  putStrLn ("Hey " ++ name)
  putStrLn $ "Am I terminal? In - " ++ (show inTty) ++ ", out - " ++ (show outTty)

prints

[…skip…]
Am I terminal? In - False, out - False
rikvdkleij commented 5 years ago

Seems to be working again.

sir4ur0n commented 5 years ago

Still doesn't work for me with Beta 52 (no more recent release published on Intellij Market).

rikvdkleij commented 5 years ago

@Sir4ur0n Which version of IntelliJ are you using? I did not make changes to the plugin with respect to this issue.

For me the putStrLns are just working. When I use getLine I have to use NoBuffering as in this example:

import System.IO

main = do
        hSetBuffering stdout NoBuffering
        putStrLn "Hello, what's your name?"
        name <- getLine
        putStrLn ("Hey " ++ name)
sir4ur0n commented 5 years ago

Ah, I hadn't paid enough attention to the hSetBuffering stdout NoBuffering part, my apologies :( It works when disabling buffering.

Shouldn't this be documented? Maybe even better: somehow enabled by default? This may confuse many people learning Haskell with Intellij and not able to make simple "Hello world" examples work 😞 Alternative: In the Run/Debug configuration window, display a warning that it may be necessary to add this for basic IO, like getLine:

import System.IO

main = do
    hSetBuffering stdout NoBuffering
    -- Do whatever IO you want here

What do you think?

rikvdkleij commented 5 years ago

No problem :smile:

I do not know how to enable it by default because it's in the Haskell code. Also do not see a Stack option for this.

Alternative: In the Run/Debug configuration window, display a warning that it may be necessary to add this for basic IO, like

Yes, that is the most simple alternative solution.