Configuration.setExtendsFrom(X) makes X immutable

It seems that setExtendsFrom in configurations makes the passed in values immutable. This is causing premature resolution problems for me eventually causing an Exception - Caused by: org.gradle.api.InvalidUserDataException: Cannot change configuration ‘:X’ after it has been resolved.

I am trying to apply my own configurations for compile and testCompile (etc). Below is a code snippet:

p.configurations {
 Configuration myMaster = maybeCreate("master")
  .setVisible(true)
  .setDescription("the published artifact but no dependencies")
     System.out.println(System.identityHashCode(myMaster))
     Configuration myCompile = maybeCreate("compile")
  .setVisible(true)
  .setDescription("artifacts needed externally by users of this artifact to compile the application")
  .setExtendsFrom(myMaster);
          //myMaster.setVisible(false); - causes exception
     maybeCreate('testCompile')
  .setVisible(true)
  .setDescription("artifacts needed to compile unit tests")
  .setExtendsFrom(myCompile);
}
  // ...
apply plugin 'java'

The problem is that this configuration must come before the application of the java plugin do to ordering constraints. The java plugin attemps to maybeCreate and modify these configurations which leads to an Exception. Note I also have a related question open regarding plugins tampering with my configurations here: http://forums.gradle.org/gradle/topics/prevent-plugins-from-tampering-with-project-configurations.

I believe this is a bug. The problem appears to be with the following code in DefaultConfiguration:

public Configuration setExtendsFrom(Iterable<Configuration> extendsFrom) {
        validateMutation();
        for (Configuration configuration : this.extendsFrom) {
            inheritedArtifacts.removeCollection(configuration.getAllArtifacts());
            inheritedDependencies.removeCollection(configuration.getAllDependencies());
        }
        this.extendsFrom = new HashSet<Configuration>();
        for (Configuration configuration : extendsFrom) {
            extendsFrom(configuration);
        }
        return this;
    }
      public Configuration extendsFrom(Configuration... extendsFrom) {
        validateMutation();
        for (Configuration configuration : extendsFrom) {
            if (configuration.getHierarchy().contains(this)) {
                throw new InvalidUserDataException(String.format(
                        "Cyclic extendsFrom from %s and %s is not allowed. See existing hierarchy: %s", this,
                        configuration, configuration.getHierarchy()));
            }
            this.extendsFrom.add(configuration);
            inheritedArtifacts.addCollection(configuration.getAllArtifacts());
            inheritedDependencies.addCollection(configuration.getAllDependencies());
        }
        return this;
    }

The setExtendsFrom above is accidently calling the first method because DefaultConfiguration extends AbstractFileCollection which Implements FileCollection which implements Iterable. Not sure what is exactly going on with the compile but since java uses type erasure and groovy has a bunch of runtime features it may not have caught the type mismatch. No runetime errors occur because the iterator (which does not return a Configuration but instead a File) does not return any non-empty iterations.

I am able to temporarily work around this by explicitly forcing the second call by wrapping the configurations in a list.

p.configurations {
    Configuration myMaster = maybeCreate("master")
        .setVisible(true)
        .setDescription("the published artifact but no dependencies")
    System.out.println(System.identityHashCode(myMaster))
    Configuration myCompile = maybeCreate("compile")
        .setVisible(true)
        .setDescription("artifacts needed externally by users of this artifact to compile the application")
        .setExtendsFrom([myMaster]);
        //myMaster.setVisible(false); - causes exception
    maybeCreate('testCompile')
        .setVisible(true)
        .setDescription("artifacts needed to compile unit tests")
        .setExtendsFrom([myCompile]);
}
// ...
apply plugin 'java'