Skip to content

Files[IO].writeAll creates file when it shouldn't #3667

@nikiforo

Description

@nikiforo

fs2 version: "3.12.2"

import cats.effect.IO  
import cats.effect.IOApp  
import fs2.Stream  
import fs2.io.file.Files  
import fs2.io.file.Path  
  
object FilesBugApp extends IOApp.Simple {  
  
  def run: IO[Unit] = {  
    val bytes: Stream[IO, Byte] = Stream.raiseError[IO](new RuntimeException)  
    bytes.through(Files[IO].writeAll(Path("should-not-be-created"))).compile.drain  
  }  
}

I expect the file not to be created. However, the file is created. This bug also breaks the logic of saving the last file in Stream context. Below is a pseudo-scala description of bug:

val last = Files[F].list("directory").compile.toList.map(_.max)
val saveNow = F.now.flatMap(now => Files[F].writeAll(Path(s"$directory/$now"))))

Stream.eval(last).flatMap(read).map(...).through(saveNow).compile.drain

Here, last always equals to saveNow file. However, if one shifts into F context by removing the Stream.eval the code works as expected.

last.flatMap { file =>
  read(file).map(...).through(saveNow).compile.drain
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions