google/dagger

Could not generate unknown file: Source file already created: ... (Eclipse only?)

vorburger opened this issue · 12 comments

When I refactor (e.g. change a @singleton's name) then I regularly see error messages like this during development:

Could not generate unknown file: Source file already created: /ch.vorburger.inject.examples.dagger/.apt_generated/ch/vorburger/inject/examples/dagger/tests/DaggerThirdServiceIntegrationTest_TestRegistry.java.

in the Eclipse IDE as a red hover over e.g. the @singleton interface (with nothing in the Error Log view in Eclipse). This is on Linux. With a Java project in Eclipse configured by Gradle with net.ltgt.gradle:gradle-apt-plugin (not that this should matter in any way).

It goes away if I manually wipe the .apt_generated/ folder and Project > Clean.

Perhaps wherever that message is coming from could handle this condition somehow more gracefully? If the "source file is already created", then ... just perhaps overwrite it, instead of throwing an exception to the end user? Just a thought.

This is an eclipse issue, probably with relation to their Filer implementation. Not much Dagger can do here, if we try to write the file and Eclipse refuses, our hands are tied.

@ronshapiro ah, so the message above doesn't actually come from Dagger, but from Eclipse JDT's APT processor... indeed I've found it on https://github.com/eclipse/eclipse.jdt.core/search?utf8=%E2%9C%93&q=%22Source+file+already+created%3A+%22 !

The check causing this message appears to have been intentionally introduced in https://bugs.eclipse.org/bugs/show_bug.cgi?id=320195. You don't think that Dagger could really have a bug and, under some circumstances, "asks for the same file to be generated twice", as per Comment 3 in that Eclipse bug 320195 ?

We haven't heard any other related issues using javac so I imagine this is an eclipse bug. Does this only come up when you use eclipse refactoring tools? That would also lead me to believe this is a uniquely Eclipse bug.

Yeah could be an Eclipse bug, OR it's Eclipse does some more strict checking and rightfully points out a a corner case bug in Dagger, which only shows in Eclipse.. I'm saying that because an IDE like Eclipse is incremental, which normal CLI javac isn't, so the way it calls into Dagger via APT is probably a little different from javac. We'd have to fully debug this more in-depth to be sure. If I see it happen a lot perhaps I will some time. It's not blocking of course, just a little annoying when it happens.

Is this still an issue for you?

No but more likely not because it's fixed but because not using Dagger after all for the moment.. ;)

Feel free to close though of course. Re-open if anyone else finds this by web search.

I am experiencing this problem in Eclipse 4.7 on a Java / Groovy Eclipse Project that uses Dagger2. It happens really often and I have to clean the project a lot of times. If I could provide some information to get this fixed I would be more than happy.

foal commented

I also have such problem with eclipse. The removing generated classes works as a workaround, sometimes.
See also https://bugs.eclipse.org/bugs/show_bug.cgi?id=367599

groz commented

I see the same problem in IntelliJ

I also have this problem with my maven project using dagger. mvn clean compile works everytime. mvn compile only works the first time, the this error occurs 100% of the time.

This may be another documented case: agrosner/DBFlow#176

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project streams-main-dagger: Compilation failure: Compilation failure:
xxx Could not generate unknown file: Attempt to recreate a file for type xxx

The maven-compiler-plugin adds the generated sources to the sources passed to the compiler, so compilation can only run once without error; afterwards, a file generated by Dagger will be fed as a source (like all your src/main/java) to JavaC, and compilation will fail when Dagger will try to regenerate the file as JavaC will refuse to overwrite a source file.

I am seeing this problem with jdk15 now. I didn't see the problem with jdk11, with nothing else changed.

$ mvn clean install
...
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ halo-schema ---
[INFO] Toolchain in maven-compiler-plugin: JDK[/opt/java/jdk-15]
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 96 source files to /home/bentatham/git/products_halo/halo-schema/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /home/bentatham/git/products_halo/halo-schema/src/main/java/ca/nanometrics/halo/graphql/schema/HaloGraphQLSchemaModule.java:[17,9] error: Could not generate unknown file: Attempt to recreate a file for type ca.nanometrics.halo.graphql.schema.HaloGraphQLSchemaModule_ProvideGraphQLSFactory.

I am running the maven-processor-plugin before this in the build and setting <proc>none</proc> on maven-compiler-plugin. (I do this to work around eclipse issues as well.

I have disabled annotation processing in Eclipse for years to avoid similar issues (and run mvn when I need to generate sources), but now I'm seeing this 100% time directly from mvn outside of Eclipse.

Is there anyway to work around this issue?