google / google/closure-compiler
Optional (by flag) System.exit when no error occurs
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
There are some few cases where call **System.exit** can have undesirable results. It would be nice if this call was optional, maybe activated by a flag.
When using this library in a container or in a Maven plugin, call **System.exit** make the entire system to shutdown.
### Test Case 1
1. Create an empty folder and put the `pom.xml` bellow. Note that Closure is going to run with only _--version_ argument.
```xml
4.0.0
com.google.javascript
closure-test-no-system-exit
0.1
UTF-8
.
org.codehaus.mojo
exec-maven-plugin
1.6.0
default-minify
process-resources
java
com.google.javascript.jscomp.CommandLineRunner
--version
true
com.google.javascript
closure-compiler
v20190121
```
2. Type `mvn clean package`. Everything works fine.
This happens because Closure doesn't call System.exit when we don't call [shouldRunCompiler](https://github.com/google/closure-compiler/blob/bea483eb54bcced63550699652ab092416c7e8fe/src/com/google/javascript/jscomp/CommandLineRunner.java#L2163) and there is no error in arguments.
### Test Case 2
1. Again, create an empty folder. This time touch an empty file named `script.js` inside the folder. After that, put the `pom.xml` below. Note that, **the only difference between the two poms** is that, this time, we are going to run the compiler on the new empty file `script.js`.
```xml
4.0.0
com.google.javascript
closure-test-no-system-exit
0.1
UTF-8
.
org.codehaus.mojo
exec-maven-plugin
1.6.0
default-minify
process-resources
java
com.google.javascript.jscomp.CommandLineRunner
${project.build.outputDirectory}/script.js
true
com.google.javascript
closure-compiler
v20190121
```
2. Type `mvn clean package`. Note that, this time, Maven doesn't show any errors, but exits right after process the resources, without finish the lifcecycle (compile and package).
This happens because Closure calls System.exit [on every exit code](https://github.com/google/closure-compiler/blob/bea483eb54bcced63550699652ab092416c7e8fe/src/com/google/javascript/jscomp/AbstractCommandLineRunner.java#L2872), even when everything ends well with the compiler, finishing with return code 0.
### Solution proposal
I think the solution could be just check [here](https://github.com/google/closure-compiler/blob/bea483eb54bcced63550699652ab092416c7e8fe/src/com/google/javascript/jscomp/AbstractCommandLineRunner.java#L527) if the result code is equal zero and, in this case, avoid calling System.exit. Note that the application still returns code zero when there is no error and the main function ends normally. That's exactly what happens when we don't use the [compiler](https://github.com/google/closure-compiler/blob/bea483eb54bcced63550699652ab092416c7e8fe/src/com/google/javascript/jscomp/CommandLineRunner.java#L2163).
Just in case we still need to call System.exit even when the return code is zero, maybe it would be optional by a flag.
Contributor guide
Assessment
This issue has not been assessed yet.