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

未关闭
#5,493 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@patelshivani2283-lab 已经在做这个了。

开始于 2026年11月17日。

  • #5494 来自 @patelshivani2283-lab —— 未关闭

评估

难度
4/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
backend

调研方向

Start with get_dependencies() in src/packagedcode/nuget.py and inspect the existing NuGet package tests for .nuspec dependency groups and license parsing. The issue also reports that packages.lock.json project references of type Project are rejected; check the related lockfile handling and tests. Reproduce the fallback group crash, then confirm all three reported cases are covered and scans complete without errors.

由索引模型根据 Issue 内容生成。

描述

Description

In NuGet .nuspec manifests, dependency <group> elements can specify target frameworks. According to Microsoft's NuGet nuspec reference documentation:

"A <dependencies> element can contain one <group> tag without a targetFramework attribute to specify a fallback group that is applied when no specific targetFramework matches the project."

Currently, scancode-toolkit's get_dependencies() in src/packagedcode/nuget.py accesses group['@targetFramework'] directly:

for group in groups:
    extra_data = dict(framework=group['@targetFramework'])
    deps = group.get('dependency') or []
    yield from _get_dep_packs(deps, extra_data)

When scanning any .nuspec that includes a fallback <group> without targetFramework, this raises an unhandled KeyError: '@targetFramework' and aborts the scan.

Additionally:

  • When <license type="expression">...</license> is used in .nuspec, xmltodict parses the element as a dictionary ({'@type': 'expression', '#text': '...'}). Because nuspec.get('license') was passed directly, extracted_license_statement received the dictionary, resulting in a stringified mapping rather than extracting the license expression string.
  • In packages.lock.json, project references with "type": "Project" were not recognized, raising Exception: Unknown package type: Project.
How To Reproduce
  1. Create a test.nuspec with a fallback group:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>SamplePackage</id>
    <version>1.0.0</version>
    <dependencies>
      <group>
        <dependency id="FallbackDep" version="1.0.0" />
      </group>
      <group targetFramework="net6.0">
        <dependency id="Net6Dep" version="2.0.0" />
      </group>
    </dependencies>
  </metadata>
</package>
  1. Scan the file:
./scancode --package --json - test.nuspec
  1. Observe traceback:
KeyError: '@targetFramework'
Expected Behavior
  • Dependencies in a fallback <group> without targetFramework should be extracted without crashing.
  • extra_data['framework'] should be set when targetFramework is present, or omitted when absent.
  • The <license> element text should be cleanly extracted even when attributes like type="expression" are present.
System configuration
  • OS: macOS / Linux / Windows
  • ScanCode version: 33.0.0rc1 / develop branch
  • Python version: 3.10+
主要语言
Python
星标
2.7k
派生
871
平均合并
1 天 10 小时
30 天内合并 PR
3

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

aboutcode-org/scancode-toolkit 的其他 Issue

查看 aboutcode-org/scancode-toolkit 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。