Repository navigation
Handle mapping versions for Dart pubspec dependencies #5482 - #5487
Open
Raman-Katiyar wants to merge 2 commits into
Open
Raman-Katiyar wants to merge 2 commits into
Raman-Katiyar wants to merge 2 commits into
Conversation
Dart dependencies can be mappings, not only version strings, e.g. a path dependency such as: dev_dependencies: _json_serial_shared_test: path: ../shared_test build_dep() only handled the {'sdk': ...} mapping form and called version.replace() on every other mapping, crashing with AttributeError: 'dict' object has no attribute 'replace' and aborting the whole pubspec parse, so scanning such a package returned no packages and no dependencies. Flatten any mapping version into a requirement string that keeps the source information (e.g. 'path: ../shared_test', 'git: url: ..., ref: main', 'hosted: ..., version: ^1.0.0'). The 'sdk:' form keeps its previous rendering ('sdk: flutter'). Add data-driven regression tests with the real json_serializable 6.15.2 pubspec.yaml from the issue and a minimal fixture covering path, git, hosted and sdk mapping forms. Signed-off-by: Raman Katiyar <ramankatiyar273@gmail.com>
Author
|
@AyanSinhaMahapatra @pombredanne — could you please review this when you have a chance? It fixes #5482, where a Dart |
Closed
3 tasks done
Defensive hardening on top of the mapping-version fix: a non-string scalar version (None for an unversioned dependency, or a bare number) no longer crashes the whole parse with AttributeError at version.replace(). It is coerced to str first, keeping the dependency in the results. The real YAML parse path yields strings, so this is robustness for unusual inputs, not a second reproduced defect. Adds three unit tests for build_dep() with None/int/float versions (fail-before: AttributeError at pubspec.py:234; pass-after: 17/17 in tests/packagedcode/test_pubspec.py). End-to-end CLI check on the real json_serializable 6.15.2 pubspec still yields 1 package and 24 dependencies. Signed-off-by: Raman Katiyar <ramankatiyar273@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5482
Problem
Scanning a Dart package whose
pubspec.yamldeclares a dependencywith a mapping version — for example a
path:dependency:crashes the pubspec parser:
The exception aborts the whole parse, so the scan returns no packages
and no dependencies for that package. Reproduced with the real
json_serializable6.15.2 package from the issue(https://pub.dev/api/archives/json_serializable-6.15.2.tar.gz).
Root cause
build_dep()insrc/packagedcode/pubspec.pyassumes the dependencyversion is always a string. Dart dependencies can also be mappings
(dependency sources):
path:,git:,hosted:(with a nestedversion:), andsdk:. Only the{'sdk': ...}mapping form washandled; every other mapping reached
version.replace(...)andcrashed.
Fix
Generalize the mapping handling in
build_dep()so any mappingversion is flattened into a requirement string that keeps the source
information, e.g.
path: ../shared_test,git: url: https://github.com/example/git_dep.git, ref: main,hosted: https://example.com, version: ^1.0.0. Thesdk:form keepsits previous rendering (
sdk: flutter), so existing behavior isunchanged.
Tests
Two new data-driven regression tests in
tests/packagedcode/test_pubspec.py(no test code change needed —fixtures are picked up automatically):
tests/packagedcode/data/pubspec/specs/json-serializable-pubspec.yaml(+ expected JSON): the real
pubspec.yamlfrom the issue.tests/packagedcode/data/pubspec/specs/dependency-sources-pubspec.yaml(+ expected JSON): a minimal fixture covering
path:,git:(stringand mapping forms),
hosted:with a nestedversion:, andsdk:mapping forms.
Fail-before (unmodified code): both new tests fail with
AttributeError: 'dict' object has no attribute 'replace'atpubspec.py:216, and parsing the realpubspec.yamlraises the sameerror.
Pass-after:
tests/packagedcode/test_pubspec.py: 14 passed (12 existing +2 new), run both with the vendored test dependencies and in a fresh
venvwithpip install -e .(Python 3.12.3).(
scancode --packageon the extractedjson_serializable6.15.2package): 1 package (
json_serializable6.15.2) and 24dependencies, including
pkg:pubspec/_json_serial_shared_testwith requirementpath: ../shared_test. Before the fix the same scan returned nopackage at all.
ruff check src/packagedcode/pubspec.py: all checks passed.(
ruff formatwould reformat the whole file, including untouchedcode, so it was not applied — the new code follows the existing
single-quote style of the file.)
Also updated
CHANGELOG.rst(Next release section).Tasks
Signed-off-by: Raman Katiyar ramankatiyar273@gmail.com