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

Incremental Java compile fails when empty source folders are removed #8197

Closed
hungvietnguyen opened this issue Jan 9, 2019 · 7 comments
Closed

Comments

@hungvietnguyen
Copy link

Environment

Gradle 5.1

Steps to Reproduce

  1. Use this sample Java library project: GradleBug.tar.gz

The project has this custom Java compile task:

class CustomJavaCompileTask extends JavaCompile {

    @InputFiles
    @SkipWhenEmpty
    public FileTree getSource() {
	return project.files(getSourceList()).getAsFileTree()
    }

    private List<FileTree> getSourceList() {
        ArrayList<FileTree> sources = new ArrayList<FileTree>();

	FileTree source1 = project.fileTree("src/main/java")
	FileTree source2 = project.fileTree("src/main/java2")

	sources.add(source1)
	if (!source2.isEmpty()) {
		sources.add(source2)
	}

	return sources
    }

    @Override
    void compile(IncrementalTaskInputs inputs) {
	for (FileTree source : getSourceList()) {
		super.source(source)
	}
        super.compile(inputs)
    }
}

task customJavaCompileTask(type: CustomJavaCompileTask) {
	destinationDir = file("out")
	classpath = files()
}
  1. Run the first build: ./gradlew customJavaCompileTask, it should succeed.
  2. Delete src/main/java2/GradleBug/Library2.java.
  3. Run the second build: ./gradlew customJavaCompileTask -s, it fails with:
java.lang.IllegalArgumentException: Unable to find source java class: 'GradleBug/src/main/java2/GradleBug/Library2.java' because it does not belong to any of the source dirs: '[GradleBug/src/main/java]'
        at org.gradle.api.internal.tasks.compile.incremental.recomp.SourceToNameConverter.getClassName(SourceToNameConverter.java:44)
        at org.gradle.api.internal.tasks.compile.incremental.recomp.JavaChangeProcessor.processChange(JavaChangeProcessor.java:34)
        at org.gradle.api.internal.tasks.compile.incremental.recomp.InputChangeAction.execute(InputChangeAction.java:47)
        at org.gradle.api.internal.tasks.compile.incremental.recomp.InputChangeAction.execute(InputChangeAction.java:26)
        at org.gradle.api.internal.changedetection.changes.ChangesOnlyIncrementalTaskInputs.doRemoved(ChangesOnlyIncrementalTaskInputs.java:53)
        ...

Expected Behavior

Incremental build should succeed.

Context

To work around #8194, in the Android Gradle plugin, we had to remove empty source folders from the inputs of the Java compile task. However, we now run into the above issue.

One way to work around both issues for us is to still register all source folders with Gradle, but create directories at non-existent paths before registering.

@wolfs
Copy link
Member

wolfs commented Jan 9, 2019

It seems like this issue is already in 4.10.1 and 5.0.

Allowing to remove the underlying directory without changing the configuration should be fixed by #8200.

Do you need to be able to do such a workaround if the original issue is fixed?

@hungvietnguyen
Copy link
Author

One way to work around both issues for us is to still register all source folders with Gradle, but create directories at non-existent paths before registering.

I was hoping that the above quick fix would work but I'm running into yet another issue: Gradle sees an added empty directory (without any source files) and assumes that the directory is a resource and then disables incremental compile with this message: "Full recompilation is required because A resource changed." (I'm working on a reproducible project, but it's not too easy...)

I will see if we can work around that issue too, otherwise we'll need to wait for 5.1.1. When do you think 5.1.1 will be available?

@wolfs
Copy link
Member

wolfs commented Jan 9, 2019

5.1.1 should go out this week.

@hungvietnguyen
Copy link
Author

Great, it may be better to wait for it then.

@hungvietnguyen
Copy link
Author

FYI, I managed to reproduce the issue with Full recompilation is required because A resource changed. here: #8203

@wolfs
Copy link
Member

wolfs commented Jan 10, 2019

I'll close this issue, given that the original problem has been fixed and the code you show in the plugin above should not be something we require users to do.

@wolfs wolfs closed this as completed Jan 10, 2019
@wolfs wolfs removed this from the 5.1.1 milestone Jan 10, 2019
@hungvietnguyen
Copy link
Author

The fact that Gradle looks for a previously deleted file in the current source folders looks incorrect to me and I feel it may hide some latent issues. But I agree that the setup in the sample project is not common.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants