Vertispan / j2clmavenplugin

Maven plugin to launch new J2CL compilation
https://vertispan.github.io/j2clmavenplugin/
Apache License 2.0
53 stars 26 forks source link

FileAlreadyExistsException from BytecodeTask when using Annotation Processor #168

Closed orielmaute closed 2 years ago

orielmaute commented 2 years ago

I created a very small testcase using the gwt3 annotation processors. When i call "mvn clean test" - i receive reproducable a FileAlreadyExistsException exception during BytecodeTask. The BytecodeTask.java:112 copies files, but it has no option set to replace files; maybe this causes the bug. I received this exception with by own annotation processors; but it is also reproducible with the gwt3 processors - see attachment.

Exception: java.nio.file.FileAlreadyExistsException: /home/xrc/projects/Xoricon_J2CL_Smoketest/j2cl-maven-apt-junit-bug/target/gwt3BuildCache/0.19/com.xoricon-j2cl-apt-junit-bug-1.0.0-SNAPSHOT-test/0b1fd204a32c8613b86f7d508c4ba170-bytecode/results/com/xoricon/j2cl/junit/ClassType.native.js
at sun.nio.fs.UnixCopyFile.copy (UnixCopyFile.java:573) at sun.nio.fs.UnixFileSystemProvider.copy (UnixFileSystemProvider.java:258) at java.nio.file.Files.copy (Files.java:1295) at com.vertispan.j2cl.build.provided.BytecodeTask.lambda$resolve$3 (BytecodeTask.java:112) at com.vertispan.j2cl.build.TaskScheduler$2.executeTask (TaskScheduler.java:172)

j2cl-maven-apt-junit-bug.zip exception.txt

niloc132 commented 2 years ago

Thanks, I'll check it out. The error suggests that the same file exists in both sources and generated outputs, which is not legal - @treblereel does this sound like something that should happen? At a guess, my hunch is that the .native.js already exists in your project, as ClassType.native.js, but you are also generating one. This isn't permitted, and can't be reasonably handled (which one should j2cl pick?), so the annotation processor for this should probably error out with a useful message if this happens?

If this isn't what's happening, I'll try it thursday and see what I can learn.

treblereel commented 2 years ago

@niloc132 yeap, it could be a reason but i need to double the reproducer first. @orielmaute thanks for your report

treblereel commented 2 years ago

@orielmaute APT runs twice : by maven-compiler-plugin and by j2cl-maven-plugin.

To fix it, you should disable apt in maven-compiler-plugin:

                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.10.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <compilerArgument>-proc:none</compilerArgument>
                    </configuration>
                </plugin>       

and please set compilationLevel to ADVANCED, it ll make your test green

            <plugin>
                <groupId>com.vertispan.j2cl</groupId>
                <artifactId>j2cl-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <compilationLevel>ADVANCED</compilationLevel>
                </configuration>
            </plugin>

mvn clean package [INFO] fetching http://localhost:54483/j2cl-apt-junit-bug/test-com.xoricon.j2cl.junit.SimpleTest.html [INFO] Test passed! [INFO] All tests were passed successfully! [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS

niloc132 commented 2 years ago

Hmm, that should not be necessary. Where is the annotation processor writing the output? the j2cl maven plugin should explicitly ignore the target/generated-sources/annotations directory.

https://github.com/Vertispan/j2clmavenplugin/blob/main/j2cl-maven-plugin/src/main/java/com/vertispan/j2cl/mojo/AbstractBuildMojo.java#L286-L287

niloc132 commented 2 years ago

Dmitrii explained it in gitter - the linked code isn't sufficient, we need to also ignore generated-test-sources/test-annotations. I'll get that added, in the meantime his workaround will do the trick for now.

orielmaute commented 2 years ago

@niloc132 & @treblereel: thanks for your analysis!

Disabling apt in maven-compiler-plugin doesn't work for me, because i need the maven-compiler annotation processor to generate java classes for native js objects. My project runs the same test cases twice in phase test; once pure java tests with surefire plugin and other pure js tests with j2cl:test.

I created a pull request which fixed the issue for me.

niloc132 commented 2 years ago

Closing as fixed in #169