SBT console encoding for test results - sbt

I have an SBT + Scalatest project. Now my tests log in console something like this:
[info] - should do something *** FAILED ***
[info] java.lang.Exception: ╧ЁштхЄ!
That's not very useful of cause.. Exception text is in Cyrillic so I have to set cp866 charset on console stream to display it correctly.
I've tried
Console.setOut(new PrintStream(System.out, true, "cp866"))
But SBT ignores it. It seems that SBT constructs it's own stream for logging various messages, but I can't find where and how to alter it..
There is a way to add custom logger, but it's an overkill.

I've found a solution.
I can create a custom LogManager
val customLogManager = LogManager.defaultManager(ConsoleOut.printStreamOut(new java.io.PrintStream(System.out, true, "cp866")))
And set it in project settings:
logManager := customLogManager
Though, I'm not sure if it is the best solution. One flaw is that you have to provide logManager setting for every project. Inheriting it from build settings doesn't work for some reason.

Related

How to disable specific plugins for generated source code in sbt? [duplicate]

I generate code with the scalaxb-sbt plugin that, when compiled, generates a good number of warning messages. Is there any way to hide compilation warnings for generated code or by package?
The silencer compiler plugin allows to suppress compiler warnings. It supports filtering out files by path. This will filter out all generated files from warnings:
scalacOptions += "-P:silencer:pathFilters=src_managed"
For Scala 2.12.13+ or 2.13.2+
Recent versions of the Scala compiler integrate the silencer plugin, see configurable warnings.
So now you don't need any plugin, just add the following line to build.sbt:
ThisBuild / scalacOptions += "-Wconf:src=src_managed/.*:silent"
Using this option will suppress warnings for generated code that lives under a directory called src_managed anywhere in your source tree.
This solved my problem with code generated by zio-grpc, where the compiler emitted warnings like parameter value evidence$3 in method live is never used (adding this info only for better searchability).
In your sbt console you could try the following:
set logLevel in compile := Level.Error or eventually set logLevel in sourceGenerators := Level.Error
and experiment with different settings. Once you are happy you could apply this setting in your build.sbt.
More detailed information can be found in the sbt documentation: http://www.scala-sbt.org/release/docs/Howto/logging.html
I found that I needed to do set logLevel in Compile := Level.Error in my sbt console session in order for this to work. This is with a capital C in Compile. This is with sbt version 0.13.11. This is also to turn all warnings off, though.
Put the code in a subproject, and set scalacOptions differently in that project? Whether this will work depends on whether the support even exists in scalac for suppressing the particular kind of warning you are getting. See for example https://issues.scala-lang.org/browse/SI-1781 . What kind of warnings are you needing to suppress exactly? Certain warnings like unchecked warnings can be suppressed with e.g. #unchecked without having to do the subproject thing.

Is it possible to disable publish without disabling publishLocal in sbt?

I have an sbt project where docker:publishLocal will create a docker image on my machine for testing, and docker:publish will publish the image to a repository and also publish jar files from the build to a repository.
If my project is a snapshot, I would like to disable publishing to the repositories, while still being able to build the local image.
ThisBuild / publishArtifact := ! isSnapshot.value
does the right thing for the publish command, but it also disables publishLocal.
I want to write something like
if (isSnapshot.value) {
publish := { }
}
but that gives me an error that I do not understand at all:
[info] Loading project definition from /Users/dev/project
/Users/dev/build.sbt:1: error: type mismatch;
found : Unit
required: sbt.internal.DslEntry
if (isSnapshot.value) {
^
Past experience dictates that redefining publish to conditionally call the original version won't work, as
publish := {
if (!isSnapshot.value) publish.value
}
gives warnings that the task is always evaluated.
Is there a way to do this?
The problem with this code is that it evaluates publish.value regardless of the if structure. I recommend reading the documentation on task dependencies. If you want to "delay" the evaluation of a task in one of the if branches, you need to use dynamic task definition:
publish := Def.taskDyn {
if (isSnapshot.value)
Def.task {} // doing nothing
else
Def.task { publish.value } // could be written as just publish
}.value
But apart from fixing your code, you should be aware that there is a special setting for the functionality you want, it's called skip:
publish/skip := isSnapshot.value
Another thing to notice, is the scoping. If you want to override docker:publish, which is the same as Docker/publish in the new syntax, you should add this Docker/ scope prefix to every mention of publish in the code above.

SBT support for custom ivy module status?

In my SBT build I have a dependency on an ivy artifact that makes use of a custom module status. This causes the following error in SBT:
[error] (*:update) sbt.ResolveException: unresolved dependency: my-org#myapp-core_2.11;1.0: java.text.ParseException: inconsistent module descriptor file found in 'http://artifacts.myorg.com/libs-snapshots-local/myapp-core_2.11/1.0/myapp-ivy.xml': bad status: 'snapshot';
I can work around this by telling SBT to use an external ivy settings, like so:
externalIvySettings(baseDirectory(_ / "ivySettings.xml"))
And then create an ivySettings.xml containing the following:
<statuses default="release">
<status name="release" integration="false"/>
<status name="snapshot" integration="false"/>
</statuses>
But surely there must be a better way? The problem with this work around is that now all my settings (such as resolvers) have to be in the ivy file too, because (IFAIK) it's all or nothing when you use externalIvySettings.
Is there a way to specify a set of custom statuses within my build.sbt? Or alternatively is there a way to tell sbt to combine external ivy settings with the ones it generates from the build.sbt.
Since specifying custom module statuses is a valid thing to do in ivy, this should really be supported in sbt too.
This is because for some repositories, they use non-standard status which fails the consistency check. We addressed this by constructing the customized resolver which doesn't do consistency check. You can also construct resolver with the custom status using the same approach. The following is the working snippet.
resolvers += {
val resolver = new org.apache.ivy.plugins.resolver.IBiblioResolver
resolver.setName("Custom Ivy Snapshots")
resolver.setRoot("http://Custom/snapshots/")
val settings = new org.apache.ivy.core.settings.IvySettings()
settings.setVariable("ivy.local.default.ivy.pattern", Pattern)
settings.setVariable("ivy.local.default.artifact.pattern", Pattern)
resolver.setSettings(settings)
resolver.setM2compatible(true)
resolver.setCheckconsistency(false)
new RawRepository(resolver)
}

sbt cross configuration dependencies

what is the reason SBT won't allow me to have dependencies between different configurations of different projects in a multi-project build?
consider the following setup in the main build.sbt file:
lazy val domain: Project = project in file("domain") dependsOn(testUtils % "test->test")
lazy val testUtils: Project = project in file("testUtils") dependsOn(domain % "compile->test")
...
I would want to write all my test helpers in testUtils, and have each of the other projects' test code to be clean test logic without the (sometimes duplicated among different projects) boilerplate of the aiding methods.
SBT is forcing me to put the : Project type, since it complains the value is "recursive". and upon reloading, I get:
...
at $281429c805669a7befa4$.domain(build.sbt:142)
at $281429c805669a7befa4$.testUtils$lzycompute(build.sbt:144)
at $281429c805669a7befa4$.testUtils(build.sbt:144)
at $281429c805669a7befa4$.domain$lzycompute(build.sbt:142)
at $281429c805669a7befa4$.domain(build.sbt:142)
[error] java.lang.StackOverflowError
[error] Use 'last' for the full log.
is there a way around this? or should I write test-related logic in each module test, even at the cost of getting the code less organize, many "test->test" dependencies, etc'...

sbt key that corresponds to command that I type in

I want to make my tests run every time I type universal:package-zip-tarball. I know that to do this, I have to put something like
someKey <<= someKey dependsOn (test in Test)
in my project/Build.scala, where someKey is the key that provides the task I want to depend on the test run, in this case, universal:package-zip-tarball.
But my generic question is: how do I find out what someKey should be?
Note that this is a Play framework project, and I don't even know if universal:package-zip-tarball is provided by Play, or by some other sbt plugin.
Is there any way sbt can just tell me, without me having to go searching for the source code repository containing the relevant code?
Use the inspect command:
$ inspect universal:package-zip-tarball
[...]
[info] Defined at:
[info] (com.typesafe.sbt.packager.universal.UniversalPlugin)
UniversalPlugin.scala:73
This is actually the location of the definition of the code of the task, but this is close enough to help, because it lets us find the key (the key will be in the same sbt plugin).
From this we can find out that the key is:
com.typesafe.sbt.packager.universal.Keys.packageZipTarball
Unfortunately, just substituting this in doesn't work - it says:
[error] Reference to undefined setting:
[error]
[error] my-project/*:packageZipTarball from my-project/*:packageZipTarball
[error] Did you mean my-project/universal-docs:packageZipTarball ?
[error]
[error] Use 'last' for the full log.
So to fix this, the only thing remaining is to translate the universal: prefix. It is in fact this:
packageZipTarball in Universal <<= packageZipTarball in Universal dependsOn (test in Test)
but it just needs an extra import to make it compile:
import com.typesafe.sbt.SbtNativePackager._
(In this case, SbtNativePackager is the main plugin object, I think. Other plugins might require importing something else, to translate such a prefix.)

Resources