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()
P.S. Found my old post on the topic - Gradle messes up the classpath order in generated projects when there are mixed dependency types