VirtusLab / scala-cli

Scala CLI is a command-line tool to interact with the Scala language. It lets you compile, run, test, and package your Scala code (and more!)
https://scala-cli.virtuslab.org
Apache License 2.0
540 stars 128 forks source link

`fmt` prints to `stderr` instead of `stdout` #2416

Closed pawelprazak closed 10 months ago

pawelprazak commented 11 months ago

Version(s) Scala CLI version: 1.0.4

Describe the bug Looks like stdout contains stderr and vice versa 🤔

To Reproduce

scala-cli fmt . --check -F --respect-project-filters | grep "core"
...unfiltered output...

moreover:

scala-cli fmt . --check -F --respect-project-filters > lol.txt
cat lol.txt
Looking for unformatted files...
error: --test failed

Expected behaviour I should be able to grep the output form a CLI.


scala-cli fmt . --check -F --respect-project-filters 2>&1 | grep "core"
--- a/Users/pprazak/repos/besom/core/src/test/scala/besom/internal/ResultSpec.scala
+++ b/Users/pprazak/repos/besom/core/src/test/scala/besom/internal/ResultSpec.scala
Gedochao commented 10 months ago

scala-cli invokes scalafmt under the hood, and the stderr/stdout channels are inherited.

The same can be achieved when launching the scalafmt CLI class directly from cs

cs launch org.scalameta:scalafmt-cli_2.13:3.7.14 -M org.scalafmt.cli.Cli -- --check --respect-project-filters 2>&1|grep "tests"
# --- a/Users/pchabelski/IdeaProjects/scala-cli-tests/fmt-stderr/Hello.scala
# +++ b/Users/pchabelski/IdeaProjects/scala-cli-tests/fmt-stderr/Hello.scala

or scalafmt directly

scalafmt --check --respect-project-filters 2>&1 | grep "tests"
# --- a/Users/pchabelski/IdeaProjects/scala-cli-tests/fmt-stderr/Hello.scala
# +++ b/Users/pchabelski/IdeaProjects/scala-cli-tests/fmt-stderr/Hello.scala

You can raise this in https://github.com/scalameta/scalafmt, but I believe this behaviour is intentional.

pawelprazak commented 10 months ago

Thank you for you insight. This behavior is not intuitive IMHO, because it does not follow UNIX principles. Please consider altering the behavior in scala-cli regardless of the upstream, to not aggregate friction.

SethTisue commented 10 months ago

@pawelprazak I don't know whether the scala-cli maintainers would consider doing that if efforts to make the change upstream failed, but I'd be astonished if they did it before it was even tried. have you opened an upstream ticket?

Gedochao commented 10 months ago

As there seems to be no valid bug here, I'm closing ticket. Feel free to link it in a new issue in the scalafmt repo and continue the discussion there.

pawelprazak commented 10 months ago

FYI, upstream works as expected with --list:

Pawes-MBP:besom pprazak$ scalafmt --list --respect-project-filters --non-interactive core
core/src/main/scala/besom/future.scala
core/src/main/scala/besom/aliases.scala
core/src/main/scala/besom/internal/Exports.scala
core/src/test/scala/besom/internal/ContextTest.scala
core/src/main/scala/besom/internal/BesomModule.scala
core/src/main/scala/besom/internal/Input.scala
core/src/test/scala/besom/internal/DecoderTest.scala
core/src/main/scala/besom/util/NonEmptyString.scala
core/src/test/scala/besom/internal/InterpolatorTest.scala
core/src/main/scala/besom/util/interpolator.scala
core/src/test/scala/besom/internal/ResultSpec.scala
core/src/main/scala/besom/internal/Config.scala
core/src/test/scala/besom/internal/OutputTest.scala
core/src/main/scala/besom/internal/logging.scala
Pawes-MBP:besom pprazak$ scalafmt --list --respect-project-filters --non-interactive core | grep Config
core/src/main/scala/besom/internal/Config.scala

Also when --list is provided it works with scala-cli (with a minor unrelated problem):

scala-cli fmt . -F --list --respect-project-filters | grep "core"
/Users/pprazak/repos/besom/scripts/Packages.scala:16: error: [dialect scala3] ; expected but => found
    os.read.lines(pluginsListPath).filter(_.nonEmpty).foreach: line =>
                                                                    ^
core/src/test/scala/besom/internal/ResultSpec.scala
core/src/test/scala/besom/internal/OutputTest.scala
core/src/test/scala/besom/internal/InterpolatorTest.scala
core/src/test/scala/besom/internal/DecoderTest.scala
core/src/test/scala/besom/internal/ContextTest.scala
core/src/main/scala/besom/util/interpolator.scala
core/src/main/scala/besom/util/NonEmptyString.scala
core/src/main/scala/besom/future.scala
core/src/main/scala/besom/aliases.scala
core/src/main/scala/besom/internal/logging.scala
core/src/main/scala/besom/internal/Input.scala
core/src/main/scala/besom/internal/Exports.scala
core/src/main/scala/besom/internal/Config.scala
core/src/main/scala/besom/internal/BesomModule.scala

I've filed an issue upstream, thank you.

pawelprazak commented 10 months ago

Since scala-cli aggregates this issue, I'd assume it would be helpful to leave it open for upstream issue tracking.

Gedochao commented 10 months ago

alright, I can leave it open. @pawelprazak BTW what's the problem with scala-cli fmt . -F --list you mentioned? just tested it and it seems to work as expected for me

pawelprazak commented 10 months ago

alright, I can leave it open. @pawelprazak BTW what's the problem with scala-cli fmt . -F --list you mentioned? just tested it and it seems to work as expected for me

Thank you! :)

I'm sorry, my message was not clear.

I've just noticed that --list prints to stdout as expected, in contrast to --check printing to stderr (unexpected)

kitbellew commented 10 months ago

i closed the linked issue in scalafmt since it works as intended, as the intention was not for this particular use case.