mdoc icon indicating copy to clipboard operation
mdoc copied to clipboard

Output from System.out doesn't get printed

Open Daenyth opened this issue 4 years ago • 5 comments

I'm trying to write some cats-effect documentation using IO.println, which boils down to a System.out call. The text doesn't get output.

Input doc:

```scala mdoc
import cats.effect._
import cats.effect.unsafe.implicits.global
IO.println("text").unsafeRunSync()
```

Output doc:

```scala
import cats.effect._
import cats.effect.unsafe.implicits.global
IO.println("text").unsafeRunSync()
```

Daenyth avatar Jun 22 '21 14:06 Daenyth

@tpolecat has a workaround using this: https://github.com/tpolecat/skunk/blob/main/modules/docs/src/main/scala/mdoc/package.scala

// Copyright (c) 2018-2021 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT

package skunk

import cats.effect.IO
import cats.effect.unsafe.implicits.global

package object mdoc {

  implicit class IOOps[A](fa: IO[A]) {
    def unsafeRunSyncWithRedirect(): A = {
      val oldOut = System.out
      val newOut = Console.out // important to do this on the calling thread!
      try {
        System.setOut(newOut)
        fa.unsafeRunSync()
      } finally System.setOut(oldOut)
    }
  }
}

Daenyth avatar Jun 22 '21 14:06 Daenyth

For CE3 it's slightly simpler btw, I believe a similar approach is used in the mdoc documentation of cats effect itself: https://github.com/indoorvivants/mdoc-effect/blob/master/core/src/main/scala/captureStdOut.scala#L43-L48

keynmol avatar Jun 22 '21 14:06 keynmol

CE Console uses System.out to avoid the thread affinity associated with scala.Console. I don't think there is a good solution, we just need to reach some consensus on the right workaround. Baking the above in to the CE3 runtime might end up being an ok compromise, dunno. It's all bad. /cc @djspiewak

tpolecat avatar Jun 22 '21 15:06 tpolecat

For CE3 it's slightly simpler btw, I believe a similar approach is used in the mdoc documentation of cats effect itself: https://github.com/indoorvivants/mdoc-effect/blob/master/core/src/main/scala/captureStdOut.scala#L43-L48

Could you point out where that's done in the CE docs? I wasn't able to find anything like that, but maybe I missed it

Daenyth avatar Jun 22 '21 15:06 Daenyth

Sorry, I messed up - I meant the tests :( https://github.com/typelevel/cats-effect/blob/d9285906a0b8f3c1b902549cae2781ebc4b90ee7/tests/shared/src/test/scala/cats/effect/std/ConsoleSpec.scala#L28

The docs don't do this and that's why in a few of them there's no output - mdoc sends it to terminal during building

keynmol avatar Jun 22 '21 15:06 keynmol