Skip to content

Fix KeyError in NuGet .nuspec parsing for groups without targetFramework - #5494

Open
patelshivani2283-lab wants to merge 1 commit into
aboutcode-org:developfrom
patelshivani2283-lab:fix-nuget-nuspec-fallback-groups-and-license
Open

patelshivani2283-lab wants to merge 1 commit into
aboutcode-org:developfrom
patelshivani2283-lab:fix-nuget-nuspec-fallback-groups-and-license

Conversation

@patelshivani2283-lab

Copy link
Copy Markdown

Fixes #5493

Tasks

  • Reviewed contribution guidelines
  • PR is descriptively titled 📑 and links the original issue above 🔗
  • Tests pass -- look for a green checkbox ✔️ a few minutes after opening your PR
  • Commits are in uniquely-named feature branch and has no merge conflicts 📁
  • Updated documentation pages (if applicable)
  • Updated CHANGELOG.rst (if applicable)

Summary of Changes

  • In src/packagedcode/nuget.py, used group.get('@targetFramework') instead of direct key lookup group['@targetFramework'], preventing KeyError when parsing .nuspec files containing fallback <group> tags without targetFramework attribute.
  • Extracted license string from the #text field when <license> has XML attributes (such as type="expression").
  • Supported "type": "Project" in packages.lock.json as direct dependencies (is_direct = True).
  • Added 3 unit tests in tests/packagedcode/test_nuget.py:
    • test_parse_nuspec_with_fallback_group_without_target_framework
    • test_parse_nuspec_with_expression_license
    • test_parse_packages_lock_with_project_dependency
  • Updated CHANGELOG.rst and AUTHORS.rst.

Signed-off-by: patelshivani2283-lab patelshivani2283@gmail.com

In NuGet .nuspec manifests, Microsoft's specification allows dependency
groups to omit the targetFramework attribute to specify a fallback group.
Previously, get_dependencies accessed group['@TARGETframework'] directly,
causing a KeyError when encountering fallback groups.

Additionally:
- Extracted license text from the '#text' field when the nuspec <license>
  element contains XML attributes (e.g., type='expression').
- Supported 'Project' dependency types in packages.lock.json as direct
  dependencies.

Signed-off-by: Shivani Patel <patelshivani2283@gmail.com>

@kmlyz kmlyz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the fix, @patelshivani2283-lab. Using group.get('@targetFramework') avoids the KeyError on fallback groups, and all 13 tests in tests/packagedcode/test_nuget.py pass. One note on src/packagedcode/nuget.py: when <license> has type="file", license_val.get('#text') extracts a filename such as LICENSE.txt as extracted_license_statement. Consider checking license_val.get('@type') == 'expression' or adding a type="file" test case.

This branch has not been deployed

No deployments
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.

[BUG] get_dependencies in nuget.py crashes with KeyError: '@targetFramework' on dependency groups without targetFramework

2 participants