# Gradle IDEA plugin appears to shuffle the modules and libraries

**URL:** <https://discuss.gradle.org/t/gradle-idea-plugin-appears-to-shuffle-the-modules-and-libraries/14815>\
**Category:** Bugs\
**Tags:** plugins\
**Created:** [February 29, 2016, 3:06am UTC](https://discuss.gradle.org/t/gradle-idea-plugin-appears-to-shuffle-the-modules-and-libraries/14815 "2016-02-29T03:06:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![trejkaz](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/trejkaz/32/3338_2.png) [@trejkaz](https://discuss.gradle.org/u/trejkaz)\
**Post date:** [February 29, 2016, 3:06am UTC](https://discuss.gradle.org/t/gradle-idea-plugin-appears-to-shuffle-the-modules-and-libraries/14815/1 "2016-02-29T03:06:42Z")

</div>

Pretty much everyone on our team has been complaining about .iml files constantly showing changes even though they haven’t changed anything in the build.

Today we performed an experiment. One person confirms that everything is clean in their project and checks in all the .iml files. Another person then updates, runs `gradle idea` and then looks for differences. If something has changed, then `gradle idea` is the culprit, otherwise it is some other thing (it could have been IDEA itself.)

It turns out that `gradle idea` is indeed the culprit. What I’m seeing here appears to be some kind of random (or at least, I can’t see the pattern) shuffling of the contents of the .iml file.

Typically, this sort of issue means that somebody thought it was a good idea to use `HashMap` instead of `LinkedHashMap` (which maintains insertion order) or `TreeMap` (which sorts), and indeed, if I do search through Gradle’s source repo, I do see usages of `HashMap`. For example:

> <https://github.com/gradle/gradle/blob/1e02b1f5da35bde98dd2a0adec2c6622546a7b79/subprojects/ide/src/main/groovy/org/gradle/plugins/ide/internal/tooling/IdeaModelBuilder.java#L76>

So seemingly the order you get things will depend on which order the modules happened to be for each person, and I can see that in the same .iml files, the list of `ModuleDependency` varies from person to person.

I figure I can probably do hack something together in a `whenMerged` callback to dodge this issue, programmatically re-sorting dependencies back into some kind of stable order, but it isn’t going to be easy because the plugin has added all the libraries as raw jar files. Had it at least used project libraries, the name of the library would have provided a convenient key to sort by.

So I have gathered two issues here:

1. The order itself shouldn’t matter (though it can, in extreme situations), but it should at least be stable, and isn’t.
2. It would be nice if project libraries were inserted into the project as project libraries, if only to cause less bleeding to the eyes when you do go into the project structure to have a look. (It would be even better if it would insert them as proper Maven dependencies so that IDEA could pick up the changes and auto-download them.)

Why do we still check in our .iml files? Because there is another issue (considered a feature, supposedly) where Gradle will successfully create incorrect .iml files when things go wrong. Being able to revert to working project files allowed us to continue to work a few times now, so we’ll continue to use the checked-in .iml files as an escape hatch until we can trust the tool.

---

<div class="post-metadata">

**Author:** ![ddimitrov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ddimitrov/32/141_2.png) [@ddimitrov](https://discuss.gradle.org/u/ddimitrov)\
**Post date:** [February 29, 2016, 5:59am UTC](https://discuss.gradle.org/t/gradle-idea-plugin-appears-to-shuffle-the-modules-and-libraries/14815/2 "2016-02-29T05:59:59Z")

</div>

Reported before as:

> [@Generation of IntelliJ project files reorders dependencies](https://discuss.gradle.org/t/generation-of-intellij-project-files-reorders-dependencies/13917/2):
>
> If anybody is interested in fixing this, the offending code is the design of the IdeDependenciesExtractor class - it provides the repo dependencies and local file dependencies in separate API calls, so there is no way for the IDE plugins to keep the relative ordering. To get a better idea of the issue, see the usages of dependenciesExtractor [IdeaDependenciesProvider#provideFromScopeRuleMappings()](https://github.com/gradle/gradle/blob/e5699fa5245564a88a5e1afcf7dbe236e9fb76ad/subprojects/ide/src/main/groovy/org/gradle/plugins/ide/idea/model/internal/IdeaDependenciesProvider.java#L132) P.S. Found my old post on the topic - [Gradle messes up the classpath order in generated projects …](https://discuss.gradle.org/t/gradle-messes-up-the-classpath-order-in-generated-projects-when-there-are-mixed-dependency-types/13130)

> [@Gradle messes up the classpath order in generated projects when there are mixed dependency types](https://discuss.gradle.org/t/gradle-messes-up-the-classpath-order-in-generated-projects-when-there-are-mixed-dependency-types/13130):
>
> Consider the following build: apply plugin: 'java' apply plugin: 'idea' apply plugin: 'eclipse' repositories { jcenter() } dependencies { compile files('lib/foo.jar') compile 'org.slf4j:slf4j-api:1.7.13' } task dumpClassPath(dependsOn: ['idea', 'eclipse']) { doLast { println "Gradle classpath:\n- " + configurations.compile.files\*.name.join("\n- ") println "-------\n" println "IDEA classpath: \n- " + file(project.name + ".iml").readLines() …

No tracking JIRA as of now.

---

<div class="post-metadata">

**Author:** ![trejkaz](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/trejkaz/32/3338_2.png) [@trejkaz](https://discuss.gradle.org/u/trejkaz)\
**Post date:** [March 1, 2016, 2:54am UTC](https://discuss.gradle.org/t/gradle-idea-plugin-appears-to-shuffle-the-modules-and-libraries/14815/3 "2016-03-01T02:54:13Z")

</div>

Both of those issues appear to indicate that it’s reordering the dependencies in some stable way, but what I’m seeing is more like random insertion of one or two dependencies completely in the wrong position in the list, versus what another user on the same project sees when they run the exact same command.

Is it really the same issue?

---

<div class="post-metadata">

**Author:** ![ddimitrov](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ddimitrov/32/141_2.png) [@ddimitrov](https://discuss.gradle.org/u/ddimitrov)\
**Post date:** [March 1, 2016, 3:13am UTC](https://discuss.gradle.org/t/gradle-idea-plugin-appears-to-shuffle-the-modules-and-libraries/14815/4 "2016-03-01T03:13:02Z")

</div>

Indeed, it is a separate issue, still both can be addressed by the same fix.

---

<div class="post-metadata">

**Author:** ![trejkaz](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/trejkaz/32/3338_2.png) [@trejkaz](https://discuss.gradle.org/u/trejkaz)\
**Post date:** [March 1, 2016, 4:31am UTC](https://discuss.gradle.org/t/gradle-idea-plugin-appears-to-shuffle-the-modules-and-libraries/14815/5 "2016-03-01T04:31:59Z")

</div>

I guess that’s true, but I don’t particularly care whether it reorders the dependencies, as long as it does it the same way every time, irrespective of the phase of the moon. So the minimum fix for my issue is probably quite a bit simpler than whatever the proper fix would involve.
