Skip to content

UnmappableCharacterException / MalformedInputException (problems with encoding) #68

Closed
@eyalroth

Description

@eyalroth

I have a hybrid scala/java gradle project with multiple sub-projects and I'm trying to make scoverage work for me.

reportScoverage fails to execute on one of my sub-projects with the following exception:

Exception in thread "main" java.nio.charset.UnmappableCharacterException: Input length = 1
        at java.nio.charset.CoderResult.throwException(CoderResult.java:282)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.read1(BufferedReader.java:210)
        at java.io.BufferedReader.read(BufferedReader.java:286)
        at java.io.Reader.read(Reader.java:140)
        at scala.io.BufferedSource.mkString(BufferedSource.scala:96)
        at scoverage.report.CodeGrid.source(CodeGrid.scala:63)
        at scoverage.report.CodeGrid.<init>(CodeGrid.scala:17)
        at scoverage.report.ScoverageHtmlWriter.filePage(ScoverageHtmlWriter.scala:78)
        at scoverage.report.ScoverageHtmlWriter.scoverage$report$ScoverageHtmlWriter$$writeFile(ScoverageHtmlWriter.scala:44)
        at scoverage.report.ScoverageHtmlWriter$$anonfun$scoverage$report$ScoverageHtmlWriter$$writePackage$1.apply(ScoverageHtmlWriter.scala:37)
        at scoverage.report.ScoverageHtmlWriter$$anonfun$scoverage$report$ScoverageHtmlWriter$$writePackage$1.apply(ScoverageHtmlWriter.scala:37)
        at scala.collection.immutable.List.foreach(List.scala:381)
        at scoverage.report.ScoverageHtmlWriter.scoverage$report$ScoverageHtmlWriter$$writePackage(ScoverageHtmlWriter.scala:37)
        at scoverage.report.ScoverageHtmlWriter$$anonfun$write$1.apply(ScoverageHtmlWriter.scala:27)
        at scoverage.report.ScoverageHtmlWriter$$anonfun$write$1.apply(ScoverageHtmlWriter.scala:27)
        at scala.collection.immutable.List.foreach(List.scala:381)
        at scoverage.report.ScoverageHtmlWriter.write(ScoverageHtmlWriter.scala:27)
        at org.scoverage.ScoverageWriter.write(ScoverageWriter.java:65)
        at org.scoverage.SingleReportApp.main(SingleReportApp.java:41)

I found an issue on the SBT plugin issue tracker (sbt-scoverage#204) with the same exception, and it implied using the proper encoding setting when invoking the scala compiler. Well then, I added this to my project:

gradle.projectsEvaluated {
    tasks.withType(AbstractScalaCompile) {
        options.encoding = "UTF-8"
        scalaCompileOptions.setEncoding("UTF-8")
        List<String> parameters = ['-encoding', 'UTF-8']
        List<String> existingParameters = scalaCompileOptions.additionalParameters
        if (existingParameters) {
            parameters.addAll(existingParameters)
        }
        scalaCompileOptions.additionalParameters = parameters
    }
}

This code is an overkill to make sure Scala compiles with UTF-8. But it didn't change a thing.

I looked at source code of where the exception is thrown at CodeGrid.source(), and I saw that it's using a custom encoding propagated by ScoverageHtmlWriter. The problem here though is that ScoverageHtmlWriter is never initialized with any custom encoding; not in the original SBT plugin code nor in this project's ScoverageWriter.

This obviously should be fixed. But anyway I thought "let's just try overriding the default encoding with a simple -Dfile.encoding=utf-8". Well, this caused an exception in an even earlier stage:

Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
        at java.nio.charset.CoderResult.throwException(CoderResult.java:281)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
        at java.io.BufferedReader.read1(BufferedReader.java:210)
        at java.io.BufferedReader.read(BufferedReader.java:286)
        at java.io.Reader.read(Reader.java:140)
        at scala.io.BufferedSource.mkString(BufferedSource.scala:96)
        at scoverage.Serializer$.deserialize(Serializer.scala:130)
        at scoverage.Serializer.deserialize(Serializer.scala)
        at org.scoverage.SingleReportApp.main(SingleReportApp.java:36)

Again, problems with encoding. I'm assuming this is a problem with the original SBT plugin and not with the gradle plugin, but I thought I'd file the issue here since I'm using the gradle plugin (well, I'm trying to use it, but it seems it needs to be fixed in order for it to be relevant to me).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions