diff --git a/io/shared/src/main/scala/fs2/io/file/Files.scala b/io/shared/src/main/scala/fs2/io/file/Files.scala index 09f25dd536..61f3bb1c36 100644 --- a/io/shared/src/main/scala/fs2/io/file/Files.scala +++ b/io/shared/src/main/scala/fs2/io/file/Files.scala @@ -443,7 +443,14 @@ sealed trait Files[F[_]] extends FilesPlatform[F] { * using the specified flags to open the file. */ def writeUtf8Lines(path: Path, flags: Flags): Pipe[F, String, Nothing] = in => - in.flatMap(s => Stream[F, String](s, lineSeparator)).through(writeUtf8(path, flags)) + in.pull.uncons + .flatMap { + case Some(_) => + in.intersperse(lineSeparator).append(Stream[F, String](lineSeparator)).underlying + case None => Pull.done + } + .stream + .through(writeUtf8(path, flags)) } private[fs2] trait FilesLowPriority { this: Files.type => diff --git a/io/shared/src/test/scala/fs2/io/file/FilesSuite.scala b/io/shared/src/test/scala/fs2/io/file/FilesSuite.scala index 62a5a5ff32..60b2d61c4c 100644 --- a/io/shared/src/test/scala/fs2/io/file/FilesSuite.scala +++ b/io/shared/src/test/scala/fs2/io/file/FilesSuite.scala @@ -169,6 +169,20 @@ class FilesSuite extends Fs2IoSuite with BaseFileSuite { |bar |""".stripMargin) } + + test("writeUtf8Lines - empty stream") { + Stream + .resource(tempFile) + .flatMap { path => + Stream.empty + .covary[IO] + .through(Files[IO].writeUtf8Lines(path)) ++ Files[IO] + .readUtf8(path) + } + .compile + .foldMonoid + .assertEquals("") + } } group("tail") {