google/truth

Compilation error when running maven tests

junyan59 opened this issue · 4 comments

Hi, I'm trying to compile and run truth tests in my local. I cloned the master branch and ran mvn test and got many similar errors, one of them being

[ERROR] testFromSetFields_comparingExpectedFieldsOnly[PROTO3](com.google.common.truth.extensions.proto.FieldScopesTest)  Time elapsed: 0.001 s  <<< ERROR!
java.lang.Error: 
Unresolved compilation problems: 
        Syntax error, type annotations are available only when source level is at least 1.8
        The annotation @Nullable is disallowed for this location at com.google.common.truth.extensions.proto.FieldScopesTest.testFromSetFields_comparingExpectedFieldsOnly(FieldScopesTest.java:522)

My OS:
Big Sur version 11.1

Java version:
java 11.0.6 2020-01-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.6+8-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.6+8-LTS, mixed mode)

Maven version:
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)

Please advise, thank you!

The tests are automatically built with -source 1.8, even though the main code is automatically built with -source 1.7:

truth/pom.xml

Lines 296 to 299 in bc8666f

<source>1.7</source>
<target>1.7</target>
<testSource>1.8</testSource>
<testTarget>1.8</testTarget>

"Unresolved compilation problems" suggests to me that Maven is seeing class files compiled by an IDE. Maybe you're using an IDE (Eclipse?) that tries to compile the entire project with -source 1.7? If so, then the ideal solution would for the IDE to understand the split between 1.7 and 1.8. But assuming that it can't:

  • You could try telling it to use -source 1.8 for the whole project. This might work, but there's a chance that compilation will fail for other reasons. You could try. If it compiles, it should work fine.
  • You could try running mvn clean test to try to get Maven to build the tests, rather than use the class files that were already compiled by the IDE. (I'm not completely sure if this will work, though: Maybe the IDE will just rebuild them itself first!)
  • Similarly, you could remove the tests from your IDE setup entirely. That way, Maven should always build them itself (though again, you might need to run mvn clean first).

In a few more months, we expect to switch to whole project to -source 1.8, at which point this problem should go away. Sorry for the trouble in the meantime. And let us know how the workarounds above work out for you.

Thank you Chris! You are correct, I found the problem was with my IDE (VS Code + Java extension pack). VS code watches the whole directory and it does something I can't understand that interferes with the build. If I do a fresh clone and run mvn test from CLI, every test passes. If I open the directory with VS Code, it automatically adds a bunch of dirs/files like .classpath and .settings, then even when I run mvn test or mvn clean test from CLI, it fails.

VS Code seems to recognize that there are several projects with different source version,
Screen Shot 2021-02-28 at 9 04 14 PM
I tried to promote everything to 1.8 too, it still failed. I have no clue about how to fix it.

I don't run into the same issue with IntelliJ, guess I'll close the issue and switch to that. Although if you have any advice on getting it working in VS Code, I'm happy to dig into it.

Thanks! It's good for us to know about this issue: It gives us reason to avoid different versions for the main and test code in the future.

Unfortunately, I know nothing about VS Code, so my only suggestion there is to ask on Stack Overflow :)

Ah, I see that it does recognize a Java version per project -- but still probably not a different version for a given project's main code and tests. It's probably a built-in limitation. But again, I have no specific knowledge of VS Code.