sbt not showing full output of 'runMain' - scala

When running scala in sbt via runMain I have the issue that some output written via println is cut off. If I run the following code by running sbt "runMain aw.OutputTry" the output start to get cut off around at some point.
package aw
object OutputTry {
def main(args: Array[String]) {
for(i <- 1 to 5000) {
println(f"${i}")
}
}
}
Example output (I snipped the output at the '...'):
uhu01#DESKTOP-4LSJM58:~/git/spinal$ sbt "runMain aw.OutputTry"
[info] Loading settings for project spinal-build from metals.sbt,plugins.sbt ...
[info] Loading project definition from /home/uhu01/git/spinal/project
[info] Loading settings for project spinal from build.sbt ...
[info] Set current project to aw (in build file:/home/uhu01/git/spinal/)
[info] sbt server started at local:///home/uhu01/.sbt/1.0/server/771a115d7899feb4b3f3/sock
sbt:aw> runMain aw.OutputTry
[warn] Multiple main classes detected. Run 'show discoveredMainClasses' to see the list
[info] Running (fork) aw.OutputTry
[info] 1
[info] 2
[info] 3
...
[info] 963
[info] 964
[info] 965
[success] Total time: 4 s, completed Apr 13, 2020 11:15:55 PM
I assume some buffering is going on in sbt to e.g. prepend the output with the [info] tag? To me the behavior looks a bit like a buffer in sbt is not flushed after the program exists.
Things I tried:
Flushing the output in the scala code (by calling Console.flush() in the loop, after println) - does not help
In the sbt documentation I could find the setting logBuffered, I checked this and it's already set to false
Pipe the output of the sbt call to a file - then all lines are visible as expected
Calling Thread.sleep(1000) before exiting only moves the problem, and would not be a solution in any case
I first suspected my environment (shell, etc.) but running the code directly in a scala REPL works as expected
Is there some sbt setting that I did overlook during my search? Any tips how to get shown the full output?
Environment: I'm using sbt 1.2.7, Scala 2.11.12 and openjdk 1.8.0_424 on Ubuntu 18.04 in WSL

sbt 1.2.7 is a pretty old version. Try 1.3.9 (latest as of April 2020).
(And remember that in order to keep builds reproducible, your sbt version is determined by your project/build.properties file, not by what you have installed.)

Related

Issue with installing CMAK - kafka manager

I am trying CMAK on ubuntu 20.4 and I am following instruction on https://github.com/yahoo/CMAK and https://codeforgeek.com/setting-up-kafka-management-for-kafka-cluster/. After running command ./sbt clean dist and trying to switch to directory /target/ in am not seeing universal directory. Is there any thing I am missing?
Please see below screenshot.
After running: ./sbt clean dist. I am getting below result.
root#ubuntu-s-1vcpu-1gb-blr1-01-testing-server:/opt/CMAK-master# ./sbt clean dist
[info] Loading settings for project cmak-master-build from plugins.sbt ...
[info] Loading project definition from /opt/CMAK-master/project
[info] Loading settings for project root from build.sbt ...
[info] Set current project to cmak (in build file:/opt/CMAK-master/)
[success] Total time: 1 s, completed Apr 4, 2021, 10:40:07 AM
Warning: node.js detection failed, sbt will use the Rhino based
Trireme JavaScript engine instead to run JavaScript assets compilation, which in some cases may be orders of magnitude slower than using node.js.
[info] Wrote /opt/CMAK-master/target/scala-2.12/cmak_2.12-3.0.0.5.pom
[warn] There may be incompatibilities among your library dependencies; run 'evicted' to see detailed eviction warnings.
[info] Main Scala API documentation to /opt/CMAK-master/target/scala-2.12/api...
[info] Compiling 136 Scala sources and 2 Java sources to /opt/CMAK-
master/target/scala-2.12/classes ...
model contains 640 documentable templates
[info] Main Scala API documentation successful.
[info] LESS compiling on 1 source(s)
./sbt: line 241: 7926 Killed "$#"
root#ubuntu-s-1vcpu-1gb-blr1-01-testing-server:/opt/CMAK-master#
| => root / Web-assets / less 122s
| => root / Compile / compileIncremental 352s
After that when navigating to target directory, there is no universal directory.
root#ubuntu-s-1vcpu-1gb-blr1-01-testing-server:/opt/CMAK-master# ls
LICENSE README.md app build.sbt conf img project public sbt
src target test
root#ubuntu-s-1vcpu-1gb-blr1-01-testing-server:/opt/CMAK-master# cd target/
root#ubuntu-s-1vcpu-1gb-blr1-01-testing-server:/opt/CMAK-
master/target# ls
scala-2.12 streams web / less 122s
root#ubuntu-s-1vcpu-1gb-blr1-01-testing-server:/opt/CMAK-master/target# ls
scala-2.12 streams web
root#ubuntu-s-1vcpu-1gb-blr1-01-testing-server:/opt/CMAK-master/target#

sbt test using scala-cucumber runner class running but not executing scenarios

I am trying to run a program developed in scala cucumber and trying to run same via command line. This job is executed successfully via Intellij when ran Runner class but while running it via command line, it shows "No tests run"
[info] Loading global plugins from /Users/user/.sbt/0.13/plugins
[info] Loading project definition from /Users/user/Documents/Spark-Scala/Spark_Scala_Cucumber/project
[info] Set current project to Spark_Scala_Cucumber (in build file:/Users/user/Documents/Spark-Scala/Spark_Scala_Cucumber/)
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for test:testOnly
[success] Total time: 2 s, completed Aug 29, 2019 11:15:02 PM
tried following commands:
sbt test
&
sbt "testOnly /Users/user/Documents/Spark-Scala/Spark_Scala_Cucumber/src/test/scala/features/steps/testRunner.scala"
#RunWith(classOf[Cucumber])
#CucumberOptions(
features = Array("classpath:features"),
glue = Array("classpath:features.steps"),
tags = Array("#my-tag"),
monochrome = true,
plugin = Array("pretty",
"html:target/cucumber",
"json:target/cucumber/test-report.json",
"junit:target/cucumber/test-report.xml")
)
class testRunner {}
Path for this file:
/Users/user/Documents/Spark-Scala/Spark_Scala_Cucumber/src/test/scala/features/steps/testRunner.scala
Expecting this should be executed command line and cucumber report is generated command line.
I missed to add the dependency in build.sbt
I found this solution which was answered earlier by below thread:
How do you run cucumber with Scala 2.11 and sbt 0.13?
dependency to be added to build.sbt:
"com.novocode" % "junit-interface" % "0.11" % Test
With this dependency the code can be executed via command line :
sbt test
This will execute all the scenarios/feature files, mentioned tags from src/test/scala/features folders.

Why does the sbt ~compile command do not recompile on code change?

I currently have a problem with recompile on code change with sbt.
I was following the sbt reference 'sbt by example'
I installed sbt 1.2.8 and followed the instructions:
Create a minimum sbt build
$ mkdir foo-build
$ cd foo-build
$ touch build.sbt
Start sbt shell
$ sbt
[info] Loading global plugins from C:\Users\hce\.sbt\1.0\plugins
[info] Loading project definition from E:\learn\Scala\demo\foo-build\project
[info] Loading settings for project foo-build from build.sbt ...
[info] Set current project to foo-build (in build file:/E:/learn/Scala/demo/foo-build/)
[info] sbt server started at local:sbt-server-57c501e502d72a00d890
Recompile on code change (Note the ~ prefix before the compile command)
sbt:foo-build> ~compile
[success] Total time: 0 s, completed Jul 6, 2019 12:01:24 PM
1. Waiting for source changes in project foo-build... (press enter to interrupt)
Create a source file
Leave the previous command running. From a different shell or in your file manager create in the project directory the following nested directories: src/main/scala/example. Then, create Hello.scala in the example directory using your favorite editor as follows:
package example
object Hello extends App {
println("Hello")
}
This new file should be picked up by the running command. But it is not working on my system.
Expected Behaviour:
[info] Compiling 1 Scala source to /tmp/foo-build/target/scala-2.12/classes ...
[info] Done compiling.
[success] Total time: 2 s, completed May 6, 2018 3:53:42 PM
2. Waiting for source changes... (press enter to interrupt)
Here are some Information about my environment
$ java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
$ sbt sbtVersion
[info] Loading global plugins from C:\Users\hce\.sbt\1.0\plugins
[info] Loading project definition from E:\learn\Scala\demo\foo-build\project
[info] Loading settings for project foo-build from build.sbt ...
[info] Set current project to foo-build (in build file:/E:/learn/Scala/demo/foo-build/)
[info] 1.2.8
$ systeminfo.exe | grep '^OS'
OS Name: Microsoft Windows 10 Enterprise LTSC
OS Version: 10.0.17763 N/A Build 17763
OS Manufacturer: Microsoft Corporation
OS Configuration: Standalone Workstation
OS Build Type: Multiprocessor Free
What I already tried:
reinstall sbt
try it with windows commad line
try it mingw64 bash
What am I missing to run the sbt ~compile command correctly?
I found the answer.
The instruction contains an error.
We are supposed to put the src/main/scala/example directory in the project directory.
From a different shell or in your file manager create in the project directory the following nested directories: src/main/scala/example.
But this leads to the problem described. foo-build/project/ is for build definition code.
If I put the src/main/scala/example directory in the foo-build directory it is working.
I should have executed the run command beforehand, which gives the 'No main class detected' error. Which in turn helped to discover, that the directory structure was incorrect by reading the following stackoverflow question: how to set main class in sbt project .
My mistake. Sorry for bothering you.

SBT scalariform plugin formatting same thing more than once, is it possible do it once?

SBT scalariform plugin is formatting files more than once in multi project setup. Here is example https://github.com/Seetaramayya/sbt-scalariform-example
if you compile the project you can see something like this in the console
[info] Formatting 5 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "root")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "module1")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "module2")(compile) ...
[info] Formatting 7 Scala sources ProjectRef(uri("file:/Users/seeta/projects/github/sbt-multi-scalariform/example/"), "main")(compile) ...
There are 5 scala files and 2 sbt files exist in the code base. Neither in SBT nor in scalariform documentation I could not find how to execute the task only once in multi module setup.
I would like to execute the task only once. Only option I see is enable plugin at root project level and disable at sub-projects but I did not like the option (I need to add disablePlugins in 15 sub-projects)
The issue seems to be that
Each subproject's scalariformFormat task in their different scopes
have the project root in their sourceDirectories setting. This just
leads to the whole project being crawled for scala sources and
formatted as many times as there are subprojects.
and has been addressed via scalariformWithBaseDirectory setting
val scalariformWithBaseDirectory = settingKey[Boolean]("Whether or not to format sources in project root (default: false)")
Thus setting withBaseDirectory=false in .scalariform.conf should give output
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "main")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module1")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module2")(compile) ...
Note, however, this will not format *.scala sources in the root project/ directory. To address that we could try keeping withBaseDirectory=false in .conf but switch it on for the root project like so
lazy val root = (project in file("."))
.aggregate(module1, module2, main)
.settings(scalariformWithBaseDirectory := true)
which outputs
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module2")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "module1")(compile) ...
[info] Formatting 1 Scala source ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "main")(compile) ...
[info] Formatting 5 Scala sources ProjectRef(uri("file:/Users/mario_galic/code/stackoverflow/sbt-scalariform-example/"), "root")(compile) ...
where we still get duplication but not as much as initially.

sbt console doesn't allow input

I've been trying to use sbt for one of my projects, however I've ran into the following problem - when I try to use sbt console to get the scala's REPL, it just doesn't allow any input. Example session:
[lared#lt foo]$ sbt
[info] Set current project to foo (in build file:/tmp/foo/)
> console
[info] Updating {file:/tmp/foo/}foo...
[info] Resolving org.scala-lang#scala-reflect;2.10.3 ...
[info] Done updating.
[info] Starting scala interpreter...
[info]
Welcome to Scala version 2.10.3 (OpenJDK 64-Bit Server VM, Java 1.8.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala>
I can not type anything. It doesn't seem to take much RAM/CPU time. The problem is reproducible in any directory, regardless of if it does have a proper project structure (for example the SBT Hello World tutorial) or not.
I'm using sbt 0.13.1 and the JDK/Scala versions as seen above on Fedora 21.
The problem was solved by deleting ~/.ivy2/ and (perhaps unrelated) migration to Oracle Java 1.7 SDK.
I could not find a way to get the Fedora 24 installed sbt 0.13.1 console to work either so I downloaded the latest sbt (currently 0.13.12), unzipped into /opt and add the sbt binary to my path before /usr/bin/sbt.
The earlier suggestion to delete ~/.ivy2 did not work for me.