[BUG] build_dep in pubspec.py crashes with AttributeError on path, git, or unversioned dependencies

オープン
#5,491 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@Adnan9-63 がすでに取り組んでいます。

2026年11月17日 から。

  • #5492 @muskan-creates352 による — マージされずにクローズ
  • #5497 @Adnan9-63 による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
backend, databases

調査の方向性

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:

  1. Path dependencies (e.g. path: ../local_pkg)
  2. Git dependencies (e.g. git: https://github.com/...)
  3. Unversioned dependencies (e.g. collection: where YAML parses the value as None)

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
  1. Create a pubspec.yaml containing 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:
  1. Run a package scan:
scancode --package --json-pp scan.json pubspec.yaml
  1. scan.json reports packages: [] (dependencies are completely lost).

  2. Directly calling DartPubspecYamlHandler.parse reveals 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 version is None, version.replace(...) raises AttributeError: 'NoneType' object has no attribute 'replace'.
  • If version is a dict (such as {'path': '...'} or {'git': '...'}), version.replace(...) raises AttributeError: '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 DependentPackage with extracted_requirement=None and is_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時間
マージ済み PR(30日)
3

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

aboutcode-org/scancode-toolkit のほかの issue

aboutcode-org/scancode-toolkit の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。