typelevel / fs2

Compositional, streaming I/O library for Scala
https://fs2.io
Other
2.38k stars 603 forks source link

`writeUtf8Lines` evaluates leading effects in the input stream twice #3488

Closed ChenCMD closed 1 month ago

ChenCMD commented 1 month ago

Version

Scala - 3.5.1 fs2-core - 3.11.0 fs2-io - 3.11.0

Minimized code

import fs2.Stream
import fs2.io.file.Files
import fs2.io.file.Path

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

@main def main: Unit = {
  val program = Stream
    .eval(IO { println("side-effect 1"); "out1" })
    .append(Stream.eval(IO { println("side-effect 2"); "out2" }))
    .through(Files.forIO.writeUtf8Lines(Path("text.txt")))
    .compile
    .drain

  program.unsafeRunSync()
}

Console output

side-effect 1
side-effect 1
side-effect 2

Expected result

side-effect 1 is output only once.

Actual result

side-effect 1 is output twice.