Skip to content

Add support for Gradle version catalog dependencies#4704

Open
Kaushik-Kumar-CEG wants to merge 1 commit intoaboutcode-org:developfrom
Kaushik-Kumar-CEG:fix-gradle-version-catalog-4521
Open

Add support for Gradle version catalog dependencies#4704
Kaushik-Kumar-CEG wants to merge 1 commit intoaboutcode-org:developfrom
Kaushik-Kumar-CEG:fix-gradle-version-catalog-4521

Conversation

@Kaushik-Kumar-CEG
Copy link
Copy Markdown

@Kaushik-Kumar-CEG Kaushik-Kumar-CEG commented Jan 26, 2026

fixes #4521

The Issue

The current Gradle parser reports incomplete Maven PURLs (e.g., pkg:maven/androidx) when scanning build.gradle files that use modern Gradle version catalog references (like libs.androidx.appcompat).

The Approach

This PR adds support for parsing gradle/libs.versions.toml to dynamically resolve version catalog aliases into full, accurate Maven coordinates.

  • Grammar Update: Added the DEPENDENCY-VERSION-CATALOG rule to match libs.x.y.z patterns in the existing pygmars parser
  • Catalog Parsing: Added parse_version_catalog() to locate, read, and parse TOML catalog files
  • Resolution: Updated get_dependencies() to intercept and resolve these catalog references during extraction to generate complete PURLs

Before Fix:
image

After Fix:
image

Verification

  • Ran full test suite for the gradle parser (all tests pass).
  • Verified existing Groovy and Kotlin tests are unbroken.
  • Tested edge cases: missing catalogs, unresolved references, and mixed dependencies.
  • Added new test fixtures (fdroid-version-catalog) including a sample libs.versions.toml and expected JSON output.

@Kaushik-Kumar-CEG Kaushik-Kumar-CEG deleted the fix-gradle-version-catalog-4521 branch April 6, 2026 19:05
@Kaushik-Kumar-CEG Kaushik-Kumar-CEG restored the fix-gradle-version-catalog-4521 branch April 16, 2026 20:10
@Kaushik-Kumar-CEG
Copy link
Copy Markdown
Author

@pombredanne @AyanSinhaMahapatra
reopened the PR! closed it briefly to double check something locally
Ready for review :)

Copy link
Copy Markdown
Member

@AyanSinhaMahapatra AyanSinhaMahapatra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kaushik-Kumar-CEG thanks, see comments for your consideration.

Comment thread src/packagedcode/build_gradle.py Outdated
'version': version
}
return alias_map
except Exception as e:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception handling is present upstream in the package plugin as generic catchalls for failure to parse so this is not required.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the try/except block, letting exceptions propagate to the upstream catchall

if os.path.exists(loc):
catalog_path = loc
break
if not catalog_path:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you have spaces between blocks of code, improves readability.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added blank lines between logical blocks and ensured proper spacing

Comment thread src/packagedcode/build_gradle.py Outdated
'scope': dep.get('scope', '')
})
# Skip if not found in catalog - prevents incomplete PURLs
continue
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I want to ignore these altogether, I still want to have these as before, shows something is wrong instead of ignoring silently.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed the logic so unresolved catalog references are kept as is instead of being silently dropped. Only resolved ones get replaced now

"is_optional": false,
"is_pinned": true,
"is_direct": true,
"resolved_package": {},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to create a resolved_package for pinned requirements.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added resolved_package for all pinned dependencies. This follows the same pattern used in the npm and nuget handlers.also this adds ~45 lines of PackageData per pinned dep in every expected JSON fixture, which is why the diff looks large (~2800 lines are just regenerated test fixtures)

@@ -0,0 +1,12 @@
[versions]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add the link to this file as a comment here?
We need to know the source

Could you also add another real example?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added source links to both the build.gradle and libs.versions.toml files. also added a second test case from nicbell/material-lists — a real Groovy project that uses version catalogs with the module syntax format. and added support for that TOML format as well

fixes aboutcode-org#4521

- Parse gradle/libs.versions.toml for version catalog dependencies
- Support both group/name dict and module string TOML formats
- Add resolved_package for pinned dependencies
- Keep unresolved catalog references visible instead of silently skipping
- Add test fixtures from F-Droid client and nicbell/material-lists
- Remove dead DEPENDENCY-5 handler code

Signed-off-by: Kaushik <kaushikrjpm10@gmail.com>
@Kaushik-Kumar-CEG Kaushik-Kumar-CEG force-pushed the fix-gradle-version-catalog-4521 branch from e94ee2e to c4279ac Compare April 21, 2026 20:28
@Kaushik-Kumar-CEG
Copy link
Copy Markdown
Author

@AyanSinhaMahapatra
all review comments have been addressed and force pushed. The large diff is mostly from regenerated test fixtures.adding resolved_package to pinned deps adds ~45 lines per dependency across all existing expected JSON files. actual code changes are around 390 lines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect Maven packages reported when scanning build.gradle for an Android app

2 participants