cincheo / jsweet

A Java to JavaScript transpiler.
http://www.jsweet.org
Other
1.46k stars 160 forks source link

Unreachable code creates org.apache.maven.plugin.MojoFailureException: transpilation failed #569

Closed keithphw closed 4 years ago

keithphw commented 4 years ago

Hi, I was porting a java project to JSweet and met a JSweet transpilation error, despite the Netbeans IDE editor not showing any errors in the java code. By trial and error I discovered that the problem was unreachable code which creates an error in JSweet transpilation, leading to a failure. This code caused the error:

String str = null;
if (true){
str = "On";
}else{
str = "Off";
}

Here is the error and transpilation failure reported:

2020-04-04 15:06:41.041 ERROR output:55 - unreachable code detected at ABC.java(868)
2020-04-04 15:06:41.041 INFO  JSweetTranspiler:784 - transpilation process finished in 4656 ms
> java2ts: 2878.8097ms
> ts2js: 1709.4229ms

[ERROR] transpilation failed
org.apache.maven.plugin.MojoFailureException: transpilation failed with 1 error(s) and 7 warning(s)
    at org.jsweet.AbstractJSweetMojo.transpile (AbstractJSweetMojo.java:618)
    at org.jsweet.JSweetMojo.execute (JSweetMojo.java:43)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call (MultiThreadedBuilder.java:190)
    at org.apache.maven.lifecycle.internal.builder.multithreaded.MultiThreadedBuilder$1.call (MultiThreadedBuilder.java:186)
    at java.util.concurrent.FutureTask.run (FutureTask.java:264)
    at java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:515)
    at java.util.concurrent.FutureTask.run (FutureTask.java:264)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1128)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:628)
    at java.lang.Thread.run (Thread.java:834)

Of course unreachable statements are a bit silly, but they're useful as a switch for running different code during development testing. javac doesn't fail compilation when it finds unreachable statements so I don't think JSweet should either.

I suspect that this potential bug may be the cause of other mystery bugs such as: https://github.com/cincheo/jsweet/issues/147 Cheers, Keith

PS: Thank you for this fantastic project.

lgrignon commented 4 years ago

Hello @keithphw Thanks for reporting! JSweet uses TypeScript compilation which indeed does not allow unreachable code. It could be an option (using this) but it is a side case, I am not sure it would be high priority. What do you think @renaudpawlak ?

keithphw commented 4 years ago

Thanks for your reply, that's interesting! I 100% respect your decision and would hate for this issue to sap your valuable time making other features or squishing more important bugs. But for me this error seems quite important because such failures are a total mystery for Java developers such as myself who know nothing about Typescript (and JavaScript) so it makes JSweet appear unreliable and buggy. While the error is reported in the transpilation step, it's difficult to recognise it in the Java code since the Java IDE doesn't highlight it and the error is one amongst a large number of other warnings in the maven output. Best wishes, Keith

lgrignon commented 4 years ago

I understand, I will take a look soon. Please note that we are open to contributions if you are willing to help :)

That's weird, I wrote a sample test case, and cannot reproduce: https://github.com/cincheo/jsweet/commit/912f4ba9cbc837358e25b119045dbeec7c86548c

This unit test passes

lgrignon commented 4 years ago

Which version of Java / Maven / JSweet are you using?

keithphw commented 4 years ago

Hi, I'm using: maven version: 3.6.3 [INFO] JSweet transpiler version 3.0.0-SNAPSHOT (build date: 2019-12-08 16:26:22) The if statement code that you posted causes the error for me if I put it in my program's main method. Strange that the error doesn't show up for you. EDIT: My Maven POM says:

11 By the way, I'd like to contribute but I will take a while since I've actually never used Maven or Typescript before trying this project.
lgrignon commented 4 years ago

Yeah, actually it's not because this commit is on a JSweet 2 branch. I will merge and try on JSweet 3 (I guess this is just working with JSweet 2)

lgrignon commented 4 years ago

Hum, can't reproduce on JSweet 3 either. Could you please try again with JSweet 3.0 RC2 and send me a small repro (zip or GitHub repo), thanks

keithphw commented 4 years ago

I'm not sure how to change to 3.0 RC2 I'm sorry. I changed my pom.xml to say: <jsweet.transpiler.version>3.0.0-RC2/</jsweet.transpiler.version> rather than the quickstart default of <jsweet.transpiler.version>3.0.0-SNAPSHOT</jsweet.transpiler.version> But the new version failed with lots of errors:

[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR]   The project com.k:3.0.0-RC2/ (C:\...\pom.xml) has 1 error
[ERROR]     'build.plugins.plugin.version' for org.jsweet:jsweet-maven-plugin must not contain any of these characters \/:"<>|?* but found / @ ...
lgrignon commented 4 years ago

Let me check on the quickstart

lgrignon commented 4 years ago

Hum, this one might be on your side. It "works on my machine" ^^ and on GitHub https://github.com/cincheo/jsweet-quickstart/runs/560328736?check_suite_focus=true

keithphw commented 4 years ago

Thank you for the specific instructions. After that change to RC2, it no longer fails! Fantastic! Thank you very much

lgrignon commented 4 years ago

Well, that's very good news. I am glad and close this ticket. Enjoy using JSweet