gradle / gradle

Adaptable, fast automation for all
https://gradle.org
Apache License 2.0
16.78k stars 4.7k forks source link

Component metadata rules do not work for file/generated dependencies #13044

Open LunNova opened 4 years ago

LunNova commented 4 years ago

Expected Behavior

Using component metadata rules to change attributes should work for any dependency

Current Behavior

Does not appear to work for a SelfResolvingDependency

Context

Have a few dependencies in implementation. Some are normal maven modules, one's a SelfResolvingDependency which gets generated by merging two downloaded jars. For legal reasons the generated jar can't be hosted as a maven module, it has to be created on-demand.

A gradle plugin which remaps jars (changing names in them based on a mapping file) is used to transform any jars which need mapped based on their dev.minco.mapped.net.minecraft attribute. The default value of this attribute is "UNKNOWN" and an AttributeCompatibilityRule is used to make that compatible with any target, ensuring that dependency without this metadata is not mapped.

The generated dependency actually should have a value for this attribute, so the component metadata rule is used to add it:

components { components ->
        components.withModule("net.minecraft:minecraft") { ComponentMetadataDetails rule ->
            rule.allVariants { variant ->
                variant.attributes {
                    container ->
                        println("set attr $mcMappedAttribute to obf")
                        container.attribute(mcMappedAttribute, "obf")
                }
            }
        }
    }

The rule appears to run, but the default value for the attribute of "UNKNOWN" still gets passed to the compatibility check resulting in no transform being ran:

Compatibility check ran, values: UNKNOWN -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: UNKNOWN -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: UNKNOWN -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: UNKNOWN -> net.fabricmc:yarn:1.15.2+build.9:v2:named
set attr dev.minco.mapped.net.minecraft to obf
Compatibility check ran, values: UNKNOWN -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: UNKNOWN -> net.fabricmc:yarn:1.15.2+build.9:v2:named

Using the same rule to target a module from a maven dependency does set the attribute, causing a build failure (expected, proves it's working, haven't actually added the transformer yet):

> Task :compileJava FAILED
set attr dev.minco.mapped.net.minecraft to obf
set attr dev.minco.mapped.net.minecraft to obf
set attr dev.minco.mapped.net.minecraft to obf
set attr dev.minco.mapped.net.minecraft to obf
set attr dev.minco.mapped.net.minecraft to obf
set attr dev.minco.mapped.net.minecraft to obf
Compatibility check ran, values: obf -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: obf -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: obf -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: obf -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: obf -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: obf -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: obf -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: obf -> net.fabricmc:yarn:1.15.2+build.9:v2:named
Compatibility check ran, values: UNKNOWN -> net.fabricmc:yarn:1.15.2+build.9:v2:named

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
   > Could not resolve com.google.code.gson:gson:2.8.6.
     Required by:
         project :
      > Unable to find a matching variant of com.google.code.gson:gson:2.8.6:
          - Variant 'compile' capability com.google.code.gson:gson:2.8.6:
              - Incompatible attribute:
                  - Required dev.minco.mapped.net.minecraft 'net.fabricmc:yarn:1.15.2+build.9:v2:named' and found incompatible value 'obf'.
              - Other attributes:

Steps to Reproduce

Use the MappingTest subfolder at https://github.com/MinimallyCorrect/Mapping/tree/gradle-13044

Your Environment

Build scan URL: https://scans.gradle.com/s/d3gx3fejgdqbw

Guess the suggested resolution here might involve not using SelfResolvingDependency, but that seems to leave us with no way to generate a dependency on demand. A FilesCollection as a dependency uses DefaultSelfResolvingDependency internally, although you're also left with group/module/version unset, so that doesn't help at all.

LunNova commented 3 years ago

Still broken under 6.7.1, moved the repro to this tag https://github.com/MinimallyCorrect/Mapping/tree/gradle-13044, main now has a workaround for this.

(Worked around this issue by making a temporary local maven repo for the dependency, immediately ran into another issue with chained transformers not seeming to work which I'm still trying to make an MVCE for. :/)