[BUG] build_dep in pubspec.py crashes with AttributeError on path, git, or unversioned dependencies
维护者通常 2 天内回复
@Adnan9-63 已经在做这个了。
开始于 2026年11月17日。
评估
调研方向
Read src/packagedcode/pubspec.py, especially build_dep and collect_deps, then reproduce with the pubspec.yaml shown in the issue. Check whether path, git, and unversioned dependencies parse without crashing and retain their dependency data. Done means the scan returns dependencies instead of an empty package list; linked pull request #5492 is already open.
由索引模型根据 Issue 内容生成。
描述
Problem Description
In src/packagedcode/pubspec.py, the build_dep function assumes version is always a string containing a version number.
When a pubspec.yaml contains:
- Path dependencies (e.g.
path: ../local_pkg) - Git dependencies (e.g.
git: https://github.com/...) - Unversioned dependencies (e.g.
collection:where YAML parses the value asNone)
build_dep crashes with an AttributeError because it calls .replace('.', '') directly on version without verifying that it is a string.
During a scan (scancode --package), this unhandled exception causes ScanCode to fail silently on pubspec.yaml, returning 0 packages and missing all dependencies.
Steps to Reproduce
- Create a
pubspec.yamlcontaining a path dependency and an unversioned dependency:
name: my_flutter_app
version: 1.0.0
dependencies:
flutter:
sdk: flutter
my_local_package:
path: ../my_local_package
collection:
- Run a package scan:
scancode --package --json-pp scan.json pubspec.yaml
-
scan.jsonreportspackages: [](dependencies are completely lost). -
Directly calling
DartPubspecYamlHandler.parsereveals the traceback:
Traceback (most recent call last):
File "src/packagedcode/pubspec.py", line 288, in build_package
package_dependencies.extend(dependencies)
File "src/packagedcode/pubspec.py", line 193, in collect_deps
dep = build_dep(...)
File "src/packagedcode/pubspec.py", line 216, in build_dep
if version.replace('.', '').isdigit():
AttributeError: 'dict' object has no attribute 'replace'
(Or AttributeError: 'NoneType' object has no attribute 'replace' if version is None).
Root Cause
In src/packagedcode/pubspec.py, line 216:
def build_dep(name, version, scope, is_runtime=True, is_optional=False):
if isinstance(version, dict) and 'sdk' in version:
version = ', '.join(': '.join([k, str(v)]) for k, v in version.items())
if version.replace('.', '').isdigit():
purl = PackageURL(
type='pubspec',
name=name,
version=version,
)
is_pinned = True
- If
versionisNone,version.replace(...)raisesAttributeError: 'NoneType' object has no attribute 'replace'. - If
versionis adict(such as{'path': '...'}or{'git': '...'}),version.replace(...)raisesAttributeError: 'dict' object has no attribute 'replace'.
Expected Behavior
- Path and git dependencies should not crash the parser; their mapping should be safely preserved in
extra_data. - Unversioned dependencies should result in a valid
DependentPackagewithextracted_requirement=Noneandis_pinned=False.
Proposed Solution
Safely check types and extract version/extra_data in build_dep:
def build_dep(name, version, scope, is_runtime=True, is_optional=False):
extra_data = {}
extracted_requirement = None
is_pinned = False
if isinstance(version, dict):
if 'sdk' in version:
extracted_requirement = ', '.join(': '.join([k, str(v)]) for k, v in version.items())
else:
extra_data = version
if 'version' in version and isinstance(version['version'], str):
extracted_requirement = version['version']
elif isinstance(version, str):
extracted_requirement = version
if extracted_requirement and isinstance(extracted_requirement, str) and extracted_requirement.replace('.', '').isdigit():
purl = PackageURL(
type='pubspec',
name=name,
version=extracted_requirement,
)
is_pinned = True
else:
purl = PackageURL(
type='pubspec',
name=name,
)
dep = models.DependentPackage(
purl=purl.to_string(),
extracted_requirement=extracted_requirement,
scope=scope,
is_runtime=is_runtime,
is_optional=is_optional,
is_pinned=is_pinned,
extra_data=extra_data,
)
return dep
Environment
- ScanCode version: 33.0.0rc1 / develop branch
- OS: All
- 主要语言
- Python
- 星标
- 2.7k
- 派生
- 871
- 平均合并
- 1 天 10 小时
- 30 天内合并 PR
- 3
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
aboutcode-org/scancode-toolkit 的其他 Issue
-
yarn.lock v1 with a scoped alias of an unscoped package yields no packages and no error可能已有人在做 @Shubham-dev-stack 于 6 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 74/100
aboutcode-org/scancode-toolkit#5466 ·
维护者通常 2 天内回复
-
package-lock.json dependencies report file_references as a list inside a list可能已有人在做 @Anbarasu2410 于 9 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 82/100
aboutcode-org/scancode-toolkit#5449 · 4 条评论 ·
维护者通常 2 天内回复
-
scancode aborts when encountering a broken RPM archive可能已有人在做 @Ashutosh-Panda2004 于 14 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 62/100
aboutcode-org/scancode-toolkit#5407 · 3 条评论 ·
维护者通常 2 天内回复
-
Four silent bugs: IsRequiredPhrase.license_expression returns None, find_in_path ignores later PATH changes, duplicate dict keys override license mappings可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭bug
难度 2/5 半天 新手友好度 65/100
aboutcode-org/scancode-toolkit#5405 · 1 条评论 ·
维护者通常 2 天内回复
-
parse_maintainer_name_email returns empty name and wrong email when string has "@" but no angle brackets可能已有人在做 @RohitSutar1823 于 15 天前认领。 未关闭bug
难度 2/5 1-3 小时 新手友好度 84/100
aboutcode-org/scancode-toolkit#5402 ·
维护者通常 2 天内回复
查看 aboutcode-org/scancode-toolkit 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
维护者通常 4 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
mohanraj00/entelos#200 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 68/100
-
sweep
难度 2/5 1-3 小时 新手友好度 85/100
bketelsen/truenas-mcp#77 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 86/100
sandbox-quantum/switch#2163 ·
维护者通常 1 天内回复