How to invalidate configuration cache when file changes

I would like my configuration cache for a task to invalidate if a specific file changes.

I tried the following:

kotlinCompileTasks is a collection of all of my kotlin compile Task objects.

  kotlinCompileTasks.all {   
	it.inputs.file(projectDir.resolve("build.json"))
  }

build.json is a custom file that I use during configuration. If build.json changes, I want the configuration cache to be invalidated.

The code above is not working. I expect that if I edit build.json, the configuration cache would invalidate. However, it does not invalidate but instead is reused even when build.json is edited.

I think I understand why. In the docs, it says:

Build configuration inputs include:

  • Init scripts, settings scripts, build scripts.
  • System properties, Gradle properties, environment variables used during the configuration phase
  • Configuration files accessed using value suppliers such as providers
  • buildSrc build configuration inputs and source files.

And I guess setting a task input file doesn’t belong to any of the above categories. But even with the list above, I do not know how I can achieve my goal. I’m guessing I should use a “value supplier”? But, I do not know how.

Read the file using one of the providers.fileContents methods.
https://docs.gradle.org/current/userguide/configuration_cache.html#config_cache:requirements:undeclared_file_read

1 Like

And better do not use .all, as that will work against task configuration avoidance and realize all those tasks even if they are not going to be executed. Better use .configureEach. :slight_smile:

2 Likes

I never knew this. Interesting!

There are more APIs you shouldn’t use.
See Task Configuration Avoidance

1 Like

i need help with thing with Mcreator pls help me pls that all

At least to me it is totally unclear what you are asking about.
If you have a question related to this thread, you should provide more information.
If it is a separate topic you need help with, you should open a separate thread with your question.

ran into RegularFile is treated as functional interface and fails with cryptic exception at runtime · Issue #15808 · gradle/gradle · GitHub, which was reported by you yourself @Vamprire