From 7af0fea54ef677b5b57c082ebfbfce08fe56fa71 Mon Sep 17 00:00:00 2001 From: Martin Duhem Date: Fri, 30 Oct 2015 19:18:12 +0100 Subject: [PATCH 1/4] Don't pass `-J` option to local Java compiler These options can only be given to a forked Java compiler. If we run a local java compiler, we filter these options out and emit a warning. Fixes sbt/sbt#1968 --- .../src/main/scala/sbt/compiler/javac/LocalJava.scala | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/compile/src/main/scala/sbt/compiler/javac/LocalJava.scala b/compile/src/main/scala/sbt/compiler/javac/LocalJava.scala index 81f4e1293..42ef0ed6e 100644 --- a/compile/src/main/scala/sbt/compiler/javac/LocalJava.scala +++ b/compile/src/main/scala/sbt/compiler/javac/LocalJava.scala @@ -66,6 +66,14 @@ final class LocalJavaCompiler(compiler: javax.tools.JavaCompiler) extends JavaCo val diagnostics = new DiagnosticsReporter(reporter) val fileManager = compiler.getStandardFileManager(diagnostics, null, null) val jfiles = fileManager.getJavaFileObjectsFromFiles(sources.asJava) - compiler.getTask(logWriter, fileManager, diagnostics, options.asJava, null, jfiles).call() + + // Local Java compiler doesn't accept `-J` options. We emit a warning if we find + // such options and don't pass them to the compiler. + val (invalidOptions, cleanedOptions) = options partition (_ startsWith "-J") + if (invalidOptions.nonEmpty) { + log.warn("Javac is running in 'local' mode. These flags have been removed:") + log.warn(invalidOptions.mkString("\t", ", ", "")) + } + compiler.getTask(logWriter, fileManager, diagnostics, cleanedOptions.asJava, null, jfiles).call() } } \ No newline at end of file From d125a36b6c76a7fb8b67ebee533b1f6b4c7432e8 Mon Sep 17 00:00:00 2001 From: Martin Duhem Date: Mon, 2 Nov 2015 14:08:06 +0100 Subject: [PATCH 2/4] Parse javac non-compile-errors in JavaErrorParser Upon startup, javac may report errors because (for instance) because it received incorrect flags. These errors were not correctly parsed by the JavaErrorParser and were never reported to the user. This commit fixes this problem by adding a new parsing rule in JavaErrorParser: errors that start with the prefix "javac:" are now correctly parsed and reported to the user. Fixes sbt/sbt#2256 --- .../scala/sbt/compiler/javac/JavaErrorParser.scala | 11 ++++++++++- .../sbt/compiler/javac/javaErrorParserSpec.scala | 12 ++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/compile/src/main/scala/sbt/compiler/javac/JavaErrorParser.scala b/compile/src/main/scala/sbt/compiler/javac/JavaErrorParser.scala index 69218f159..690274a66 100644 --- a/compile/src/main/scala/sbt/compiler/javac/JavaErrorParser.scala +++ b/compile/src/main/scala/sbt/compiler/javac/JavaErrorParser.scala @@ -44,6 +44,7 @@ class JavaErrorParser(relativeDir: File = new File(new File(".").getAbsolutePath override val skipWhitespace = false + val JAVAC: Parser[String] = literal("javac") val CHARAT: Parser[String] = literal("^") val SEMICOLON: Parser[String] = literal(":") | literal("\uff1a") val SYMBOL: Parser[String] = allUntilChar(':') // We ignore whether it actually says "symbol" for i18n @@ -161,8 +162,16 @@ class JavaErrorParser(relativeDir: File = new File(new File(".").getAbsolutePath msg ) } + val javacError: Parser[Problem] = + JAVAC ~ SEMICOLON ~ restOfLine ^^ { + case _ ~ _ ~ error => + new JavaProblem( + JavaNoPosition, + Severity.Error, + s"javac:$error") + } - val potentialProblem: Parser[Problem] = warningMessage | errorMessage | noteMessage + val potentialProblem: Parser[Problem] = warningMessage | errorMessage | noteMessage | javacError val javacOutput: Parser[Seq[Problem]] = rep(potentialProblem) /** diff --git a/compile/src/test/scala/sbt/compiler/javac/javaErrorParserSpec.scala b/compile/src/test/scala/sbt/compiler/javac/javaErrorParserSpec.scala index aa3fa24c4..b7e088b2e 100644 --- a/compile/src/test/scala/sbt/compiler/javac/javaErrorParserSpec.scala +++ b/compile/src/test/scala/sbt/compiler/javac/javaErrorParserSpec.scala @@ -15,6 +15,7 @@ object JavaErrorParserSpec extends Specification { be able to parse linux errors $parseSampleLinux be able to parse windows file names $parseWindowsFile be able to parse windows errors $parseSampleWindows + be able to parse javac errors $parseSampleJavac """ def parseSampleLinux = { @@ -45,6 +46,15 @@ object JavaErrorParserSpec extends Specification { } } + def parseSampleJavac = { + val parser = new JavaErrorParser() + val logger = Logger.Null + val problems = parser.parseProblems(sampleJavacMessage, logger) + def rightSize = problems must haveSize(1) + def rightError = problems(0).message must beEqualTo(sampleJavacMessage) + rightSize and rightError + } + def sampleLinuxMessage = """ |/home/me/projects/sample/src/main/Test.java:4: cannot find symbol @@ -63,4 +73,6 @@ object JavaErrorParserSpec extends Specification { def windowsFile = """C:\Projects\sample\src\main\java\Test.java""" def windowsFileAndLine = s"""$windowsFile:4""" + + def sampleJavacMessage = "javac: invalid flag: -foobar" } From 590c4d07ef4efa989730d72340a58274d022acdc Mon Sep 17 00:00:00 2001 From: Martin Duhem Date: Wed, 11 Nov 2015 14:41:26 +0100 Subject: [PATCH 3/4] Notes for sbt/sbt#2272 --- notes/0.13.10/dont-pass-jflag-to-local-java.md | 11 +++++++++++ notes/0.13.10/parse-non-compile-errors.md | 11 +++++++++++ 2 files changed, 22 insertions(+) create mode 100644 notes/0.13.10/dont-pass-jflag-to-local-java.md create mode 100644 notes/0.13.10/parse-non-compile-errors.md diff --git a/notes/0.13.10/dont-pass-jflag-to-local-java.md b/notes/0.13.10/dont-pass-jflag-to-local-java.md new file mode 100644 index 000000000..a1e660b63 --- /dev/null +++ b/notes/0.13.10/dont-pass-jflag-to-local-java.md @@ -0,0 +1,11 @@ + + [@Duhemm]: http://github.com/Duhemm + [1968]: https://github.com/sbt/sbt/issues/1968 + [2272]: https://github.com/sbt/sbt/pull/2272 + +### Fixes with compatibility implications + +### Improvements + +### Bug fixes +- Don't pass `-J` options to the local java compiler (issue [#1968][1968]) [#2272][2272] by [@Duhemm][@Duhemm] diff --git a/notes/0.13.10/parse-non-compile-errors.md b/notes/0.13.10/parse-non-compile-errors.md new file mode 100644 index 000000000..3d45366b7 --- /dev/null +++ b/notes/0.13.10/parse-non-compile-errors.md @@ -0,0 +1,11 @@ + + [@Duhemm]: http://github.com/Duhemm + [2256]: https://github.com/sbt/sbt/issues/2256 + [2272]: https://github.com/sbt/sbt/pull/2272 + +### Fixes with compatibility implications + +### Improvements + +### Bug fixes +- Improve `JavaErrorParser` to parse non-compile-errors (issue [#2256][2256]) [#2272][2272] by [@Duhemm][@Duhemm] From 69411c1876752431f7fc3738429ae6542b6b2d09 Mon Sep 17 00:00:00 2001 From: Martin Duhem Date: Thu, 12 Nov 2015 18:50:03 +0100 Subject: [PATCH 4/4] Fix issues with Codacy --- .../sbt/compiler/javac/javaErrorParserSpec.scala | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/compile/src/test/scala/sbt/compiler/javac/javaErrorParserSpec.scala b/compile/src/test/scala/sbt/compiler/javac/javaErrorParserSpec.scala index b7e088b2e..6bd035658 100644 --- a/compile/src/test/scala/sbt/compiler/javac/javaErrorParserSpec.scala +++ b/compile/src/test/scala/sbt/compiler/javac/javaErrorParserSpec.scala @@ -18,7 +18,7 @@ object JavaErrorParserSpec extends Specification { be able to parse javac errors $parseSampleJavac """ - def parseSampleLinux = { + def parseSampleLinux: MatchResult[_] = { val parser = new JavaErrorParser() val logger = Logger.Null val problems = parser.parseProblems(sampleLinuxMessage, logger) @@ -27,7 +27,7 @@ object JavaErrorParserSpec extends Specification { rightSize and rightFile } - def parseSampleWindows = { + def parseSampleWindows: MatchResult[_] = { val parser = new JavaErrorParser() val logger = Logger.Null val problems = parser.parseProblems(sampleWindowsMessage, logger) @@ -46,7 +46,7 @@ object JavaErrorParserSpec extends Specification { } } - def parseSampleJavac = { + def parseSampleJavac: MatchResult[_] = { val parser = new JavaErrorParser() val logger = Logger.Null val problems = parser.parseProblems(sampleJavacMessage, logger) @@ -55,7 +55,7 @@ object JavaErrorParserSpec extends Specification { rightSize and rightError } - def sampleLinuxMessage = + def sampleLinuxMessage: String = """ |/home/me/projects/sample/src/main/Test.java:4: cannot find symbol |symbol : method baz() @@ -63,7 +63,7 @@ object JavaErrorParserSpec extends Specification { |return baz(); """.stripMargin - def sampleWindowsMessage = + def sampleWindowsMessage: String = s""" |$windowsFile:4: cannot find symbol |symbol : method baz() @@ -71,8 +71,8 @@ object JavaErrorParserSpec extends Specification { |return baz(); """.stripMargin - def windowsFile = """C:\Projects\sample\src\main\java\Test.java""" - def windowsFileAndLine = s"""$windowsFile:4""" + def windowsFile: String = """C:\Projects\sample\src\main\java\Test.java""" + def windowsFileAndLine: String = s"""$windowsFile:4""" def sampleJavacMessage = "javac: invalid flag: -foobar" }