Setting intellij plugin configs programmatically

I am a contributor of a gradle plugin. I would like my plugin to communicate with the Intellij IDEA plugin so that I can do the equivalent of this:

idea {
  module {
    sourceDirs += file('some_dir')
  }
}

I have code that does this in one of my plugin task’s configuration steps:

IdeaPlugin idea = (IdeaPlugin) project.getPlugins().findPlugin("idea")
if (idea == null) {
  return
}
idea.model.module.sourceDirs += f

This works when I load the project into intellij as a gradle project. However things are a bit strange when running on the CLI. Running ./gradlew build and then ./gradlew idea produces an iml file with the expected directories. If I run ./gradlew clean ; ./gradlew idea my custom directories are not found in the *.iml file that is produced. This is surprising because the sourceDir update happens in the configuration step, which should be before the idea plugin tasks. What state information could have been updated between ./gradlew build and ./gradlew idea that makes it work?

I thought perhaps this is because the idea configuration already copied idea.model.module.sourceDirs into an internal data structure before my configuration code has a chance to update it, but ruled this out:
I created a unit test and used a GradleRunner with debugging enabled so I can set break points in the Idea plugin classes. I set a breakpoint in GenerateIdeaModule in the method protected void configure(Module xmlModule) { and see that xmlModule.sourceFolders contains the directories I expected.

Anyone have any ideas on what I missed?

Looks like I found the solution, the idea plugin only allows adding paths that exist on the file system. At config time, the paths do not exist yet.