Skip to content

Parse dart files with the SDK version from the pubspec - #186

Open
gmpassos wants to merge 1 commit into
Workiva:masterfrom
gmpassos:parse-with-pubspec-sdk-feature-set
Open

Parse dart files with the SDK version from the pubspec#186
gmpassos wants to merge 1 commit into
Workiva:masterfrom
gmpassos:parse-with-pubspec-sdk-feature-set

Conversation

@gmpassos

Copy link
Copy Markdown

Problem

getDartDirectivePackageNames parsed every dart file with parseString's default feature set, FeatureSet.latestLanguageVersion(). Syntax newer than the package's declared SDK constraint was therefore accepted silently, even though the analyzer would reject it in the package it lives in.

Change

checkPackage resolves a FeatureSet from the environment: sdk: lower bound of the pubspec it already parses (truncated to major.minor.0, since a language version has no patch component) and passes it down to getDartDirectivePackageNames:

final featureSet = featureSetForPubspec(pubspec);
...
getDartDirectivePackageNames(file, featureSet: featureSet)
  • featureSetForPubspec falls back to FeatureSet.latestLanguageVersion() when there is no SDK lower bound.
  • A bare pin (sdk: 3.0.0) works too — Version implements VersionRange with itself as the lower bound.
  • Workspace sub-packages get their own feature set, since checkPackage already recurses per sub-package.
  • // @dart=x.y overrides still apply: parseString hands the feature set to the scanner as featureSetForOverriding.

Behavior change

A file using syntax newer than its pubspec's SDK lower bound now hits the existing ArgumentError path in getDartDirectivePackageNames, which prints the diagnostic and exits 1. Such code is genuinely invalid under language versioning, but it is a harder failure than before.

Testing

  • dart analyze / dart format clean.
  • End-to-end against a scratch package containing sealed class: with sdk: ">=2.19.0 <4.0.0" it now reports experiment_not_enabled: This requires the 'sealed-class' language feature to be enabled; with sdk: ^3.0.0 it parses and validates normally.
  • dart test: 84 passing. The 4 failures in test/executable_test.dart are pre-existing and reproduce identically on unmodified master (their fixtures need network access for pub get).

🤖 Generated with Claude Code

https://claude.ai/code/session_01JUxxy3wafsbkdCCRqmZEDd

`getDartDirectivePackageNames` parsed every dart file with
`FeatureSet.latestLanguageVersion()`, so syntax newer than the package's
declared SDK constraint was silently accepted.

`checkPackage` now resolves a `FeatureSet` from the `environment: sdk:`
lower bound of the pubspec it already parsed, and passes it down to
`getDartDirectivePackageNames`. Workspace sub-packages get their own
feature set, since `checkPackage` recurses per sub-package.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JUxxy3wafsbkdCCRqmZEDd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant