Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When fatal warnings set Java ones are also shown as errors #2341

Closed
tgodzik opened this issue May 27, 2024 · 10 comments · Fixed by #2346
Closed

When fatal warnings set Java ones are also shown as errors #2341

tgodzik opened this issue May 27, 2024 · 10 comments · Fixed by #2346
Labels
bug A defect or misbehaviour. difficulty / easy Any change that is easy to implement.

Comments

@tgodzik
Copy link
Contributor

tgodzik commented May 27, 2024

With this configuration:

val moduleB = project
  .in(file("moduleB"))
  .settings(
    scalaVersion := "3.3.3",
    scalacOptions ++= Seq(
      "-Xfatal-warnings"
    ),
    javacOptions ++= Seq(
      "-Xlint:unchecked"
    )
  )

and this file:

src/main/java/com/example/Service.java:

package com.example;

import java.util.List;

public class Service {
	void create(List<Object> args) {
		var second = (java.util.Optional<String>) args.get(2);
		System.out.println(second);
	}
}

Bloop will show the cast warning as error.

This is caused by the fact that we actually turn fatal warnings off to get all the artifacts compiler, we later upgrade diagnostics to errors. We probably don't check the source of them.

@masonedmison
Copy link
Contributor

@tgodzik Would you mind if I took a stab at this one?

@tgodzik
Copy link
Contributor Author

tgodzik commented May 29, 2024

Sure! We should probably filter Java files around

.foreach(f => sourceFilesWithFatalWarnings.put(f, true))

@tgodzik
Copy link
Contributor Author

tgodzik commented May 29, 2024

The tests are around https://github.com/scalacenter/bloop/blob/main/frontend/src/test/scala/bloop/BaseCompileSpec.scala#L1119

@masonedmison
Copy link
Contributor

Hi @tgodzik I'm having trouble reproducing this issue.

I'm currently running bloop version bloop v1.5.17. Using the set up shared above I see only a warning:

/home/edmisml/programming/scala/bug_repros/bloop_2341/moduleB/src/main/java/com/example/Service.java:8: warning: [unchecked] unchecked cast
		var second = (java.util.Optional<String>) args.get(2);
		                                                  ^
  required: Optional<String>
  found:    Object
1 warning.

@tgodzik
Copy link
Contributor Author

tgodzik commented May 30, 2024

I created a simple sbt project:

  • build.sbt :
resolvers += "scala-integration" at "https://scala-ci.typesafe.com/artifactory/scala-integration/"
ThisBuild / scalaVersion := "2.13.14"

name := "hello-world"
organization := "ch.epfl.scala"
version := "1.0"

scalacOptions ++= Seq(
  "-Xfatal-warnings"
)
javacOptions ++= Seq(
  "-Xlint:unchecked"
)
  • src/main/scala/Main.scala
import com.example.Service

object Main extends App {
  val service = new Service()
  service.create(List("").asInstanceOf[java.util.List[Object]])
  println("Hello, World!")
}
  • src/main/java/com/example/Service.java
package com.example;

import java.util.List;

public class Service {

    String aaa = 123;
	public void create(List<Object> args) {
		var second = (java.util.Optional<String>) args.get(2);
		System.out.println(second);
	}
}

And it seems to repro in Metals via BSP. The only thing I see is that the Java warning is not show neither as error nor as warning in the CLI if there is no additional actual error.

@masonedmison
Copy link
Contributor

masonedmison commented May 31, 2024

I see. Thanks for sharing that. Weirdly with this project setup I still don't see the unchecked warning being reflected as an error in metals via BSP (I'm using the vscode extension and am looking at the "output" logs view) -- this is true regardless if there are additional errors in the java file. e.g.

/home/edmisml/programming/scala/bug_repros/bloop_2341/src/main/java/com/example/Service.java:7: error: incompatible types: int cannot be converted to String
  String aaa = 123;
               ^
/home/edmisml/programming/scala/bug_repros/bloop_2341/src/main/java/com/example/Service.java:9: warning: [unchecked] unchecked cast
		var second = (java.util.Optional<String>) args.get(2);
		                                                  ^
  required: Optional<String>
  found:    Object
1 error
1 warning.

Here is my sample project. It should be identical to what you shared above except I had to use the jdk.CollectionConverters else I was getting a different error.

@tgodzik
Copy link
Contributor Author

tgodzik commented May 31, 2024

You can check the bloop configuration files inside .bloop to see if they indeed have fatal warnings set

@masonedmison
Copy link
Contributor

Fatal warnings are set in the bloop settings JSON file. I'm not sure what's going on. I tried cloning this repo mentioned in #6348 and the cast error still only shows as a warning in my metals output.

@masonedmison
Copy link
Contributor

masonedmison commented Jun 1, 2024

I've tried including "fatal warnings" (e.g. incomplete pattern matching) within scala files that are in the same project with the java file with the unchecked cast but it still only shows as a warning. I want to help but there isn't much to go off of if I can't reproduce it locally.

@tgodzik
Copy link
Contributor Author

tgodzik commented Jun 1, 2024

You could try writing a test and see if it reproduces there or on the CI.

But I can't really see how this would not reproduce, I'm put of ideas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A defect or misbehaviour. difficulty / easy Any change that is easy to implement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants