Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Scala on Mac fails when run in unicode directory

I am attempting to simply compile a helloworld scala file from within a directory named using unicode characters but the scala compiler fails to run. It succeeds when I change directory's out the unicode named folder.

Detail

Given a classic Helloworld.scala class such as the one below that is located in the directory:

/Users/me/Dev/Company/𝔘𝔫𝔦𝔠𝔬𝔡𝔢/code_folder

I am unable to run scalac on the code if my current directory is in code_folder which is the root directory for the Helloworld.scala. When I change directories out of the folder with the unicode name /𝔘𝔫𝔦𝔠𝔬𝔡𝔢, the compiler works just fine on the same code. So it would seem that scalac doesnt work when attempting to compile code while in a folder with unicode characters. Why is that? Is this a system error? An unparsed unicode error between the OS starting the scalac process?

object Main{
    def main(args:  Array[String]): Unit = {
        println("Hello world")
    }
}

I expected a compiled file. The error I get is this

Exception in thread "main" java.lang.ExceptionInInitializerError
    at scala.tools.util.PathResolver$Environment$.scalaExtDirs(PathResolver.scala:77)
    at scala.tools.util.PathResolver$Defaults$.scalaExtDirs(PathResolver.scala:127)
    at scala.tools.nsc.settings.StandardScalaSettings.$init$(StandardScalaSettings.scala:31)
    at scala.tools.nsc.settings.MutableSettings.<init>(MutableSettings.scala:28)
    at scala.tools.nsc.Settings.<init>(Settings.scala:19)
    at scala.tools.nsc.Driver.process(Driver.scala:53)
    at scala.tools.nsc.Driver.main(Driver.scala:80)
    at scala.tools.nsc.Main.main(Main.scala)
Caused by: java.lang.IllegalArgumentException: Error decoding percent encoded characters
    at java.base/sun.net.www.ParseUtil.decode(ParseUtil.java:209)
    at java.base/jdk.internal.loader.FileURLMapper.getPath(FileURLMapper.java:64)
    at java.base/jdk.internal.loader.FileURLMapper.exists(FileURLMapper.java:73)
    at java.base/jdk.internal.loader.URLClassPath$JarLoader.getJarFile(URLClassPath.java:802)
    at java.base/jdk.internal.loader.URLClassPath$JarLoader.access$900(URLClassPath.java:692)
    at java.base/jdk.internal.loader.URLClassPath$JarLoader$1.run(URLClassPath.java:751)
    at java.base/jdk.internal.loader.URLClassPath$JarLoader$1.run(URLClassPath.java:744)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/jdk.internal.loader.URLClassPath$JarLoader.ensureOpen(URLClassPath.java:743)
    at java.base/jdk.internal.loader.URLClassPath$JarLoader.<init>(URLClassPath.java:718)
    at java.base/jdk.internal.loader.URLClassPath$3.run(URLClassPath.java:486)
    at java.base/jdk.internal.loader.URLClassPath$3.run(URLClassPath.java:469)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/jdk.internal.loader.URLClassPath.getLoader(URLClassPath.java:468)
    at java.base/jdk.internal.loader.URLClassPath.getLoader(URLClassPath.java:437)
    at java.base/jdk.internal.loader.URLClassPath.findResource(URLClassPath.java:280)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findResourceOnClassPath(BuiltinClassLoader.java:479)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findResource(BuiltinClassLoader.java:303)
    at java.base/java.lang.ClassLoader.getResource(ClassLoader.java:1393)
    at java.base/java.lang.ClassLoader.getSystemResource(ClassLoader.java:1658)
    at java.base/java.lang.ClassLoader.getSystemResourceAsStream(ClassLoader.java:1762)
    at java.base/java.lang.Class.getResourceAsStream(Class.java:2607)
    at scala.util.PropertiesTrait.scalaProps(Properties.scala:39)
    at scala.util.PropertiesTrait.scalaProps$(Properties.scala:37)
    at scala.tools.reflect.WrappedProperties$AccessControl$.scalaProps$lzycompute(WrappedProperties.scala:49)
    at scala.tools.reflect.WrappedProperties$AccessControl$.scalaProps(WrappedProperties.scala:49)
    at scala.util.PropertiesTrait.scalaPropOrNone(Properties.scala:71)
    at scala.util.PropertiesTrait.scalaPropOrNone$(Properties.scala:71)
    at scala.tools.reflect.WrappedProperties$AccessControl$.scalaPropOrNone(WrappedProperties.scala:49)
    at scala.util.PropertiesTrait.$init$(Properties.scala:83)
    at scala.tools.reflect.WrappedProperties$AccessControl$.<init>(WrappedProperties.scala:49)
    at scala.tools.reflect.WrappedProperties$AccessControl$.<clinit>(WrappedProperties.scala)
    ... 8 more

Version: scalac -version Scala compiler version 2.12.8 -- Copyright 2002-2018, LAMP/EPFL and Lightbend, Inc.

MacOS version 10.13.6

like image 511
Theko Lekena Avatar asked Apr 09 '19 14:04

Theko Lekena


1 Answers

Not really a solution, but some piece of information:

I set up my folder this way:

/home/nicolas/Private/𝔘𝔫𝔦𝔠𝔬𝔡𝔢/

When the ClassLoader tries to load the classes of your project it makes a list of paths where to lookup classes as a list of URLClassPath Now when the code goes on and it tries to parse these paths, it calls sun.net.www.ParseUtil.decode()

When I put a breakpoint in here, I can see that the path for our class is:

/home/nicolas/Private/%ed%a0%b5%ed%b4%98%ed%a0%b5%ed%b4%ab%ed%a0%b5%ed%b4%a6%ed%a0%b5%ed%b4%a0%ed%a0%b5%ed%b4%ac%ed%a0%b5%ed%b4%a1%ed%a0%b5%ed%b4%a2/target/scala-2.12/classes/

so it somehow translated 𝔘𝔫𝔦𝔠𝔬𝔡𝔢 into a percent encoded string : %ed%a0%b5%ed%b4%98%ed%a0%b5%ed%b4%ab%ed%a0%b5%ed%b4%a6%ed%a0%b5%ed%b4%a0%ed%a0%b5%ed%b4%ac%ed%a0%b5%ed%b4%a1%ed%a0%b5%ed%b4%a2

This in turn creates a CharsetDecoder for UTF-8:

CharsetDecoder dec = ThreadLocalCoders.decoderFor("UTF-8")
                                      .onMalformedInput(CodingErrorAction.REPORT)
                                      .onUnmappableCharacter(CodingErrorAction.REPORT);

and tries to decode the above url but it fails and that is why the classes can't be loaded.

Now if I go here https://www.branah.com/unicode-converter and tries to convert 𝔘𝔫𝔦𝔠𝔬𝔡𝔢 to a % encoded string, it gives me

%f0%9d%94%98%f0%9d%94%ab%f0%9d%94%a6%f0%9d%94%a0%f0%9d%94%ac%f0%9d%94%a1%f0%9d%94%a2

And if when I debug, I change the url using this encoded string value, ie

/home/nicolas/Private/%f0%9d%94%98%f0%9d%94%ab%f0%9d%94%a6%f0%9d%94%a0%f0%9d%94%ac%f0%9d%94%a1%f0%9d%94%a2/target/scala-2.12/classes/

Then it parses it properly and keeps going.

So I don't know where it gets that %ed%a0%b5%ed%b4%98%ed%a0%b5%ed%b4%ab%ed%a0%b5%ed%b4%a6%ed%a0%b5%ed%b4%a0%ed%a0%b5%ed%b4%ac%ed%a0%b5%ed%b4%a1%ed%a0%b5%ed%b4%a2

The interesting thing is that if I use the same website and try to convert back %ed%a0%b5%ed%b4%98%ed%a0%b5%ed%b4%ab%ed%a0%b5%ed%b4%a6%ed%a0%b5%ed%b4%a0%ed%a0%b5%ed%b4%ac%ed%a0%b5%ed%b4%a1%ed%a0%b5%ed%b4%a2 then I also get 𝔘𝔫𝔦𝔠𝔬𝔡𝔢

So I am a bit confused

like image 77
Bentaye Avatar answered Oct 19 '22 14:10

Bentaye