Skip to content

Move hasMultipleVersions to Description #4576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/command/add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ Specify multiple sdk packages with descriptors.''');
final description = pubspecDescription(
ref.withConstraint(
constraint ??
(ref.source is HostedSource
(ref.description.hasMultipleVersions
? VersionConstraint.compatibleWith(resultId.version)
: VersionConstraint.any),
),
Expand Down
7 changes: 3 additions & 4 deletions lib/src/command/upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import '../package_name.dart';
import '../pubspec.dart';
import '../pubspec_utils.dart';
import '../solver.dart';
import '../source/hosted.dart';
import '../utils.dart';

/// Handles the `upgrade` pub command.
Expand Down Expand Up @@ -248,11 +247,11 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
// Mapping from original to changed value.
var changes = <Package, Map<PackageRange, PackageRange>>{};
for (final package in entrypoint.workspaceRoot.transitiveWorkspace) {
final declaredHostedDependencies = [
final declaredUpgradableDependencies = [
...package.dependencies.values,
...package.devDependencies.values,
].where((dep) => dep.source is HostedSource);
for (final dep in declaredHostedDependencies) {
].where((dep) => dep.description.hasMultipleVersions);
for (final dep in declaredUpgradableDependencies) {
final resolvedPackage = resolvedPackages[dep.name]!;
if (!toUpgrade.contains(dep.name)) {
// If we're not trying to upgrade this package, or it wasn't in the
Expand Down
2 changes: 1 addition & 1 deletion lib/src/package_name.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class PackageRange {
bool get _showVersionConstraint {
if (isRoot) return false;
if (!constraint.isAny) return true;
return description.source.hasMultipleVersions;
return description.hasMultipleVersions;
}

/// Returns a copy of `this` with the same semantics, but with a `^`-style
Expand Down
5 changes: 4 additions & 1 deletion lib/src/solver/version_solver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,10 @@ class VersionSolver {
// can't be downgraded.
if (_type == SolveType.downgrade) {
final locked = _lockFile.packages[package];
if (locked != null && !locked.source.hasMultipleVersions) return locked;
if (locked != null &&
!locked.description.description.hasMultipleVersions) {
return locked;
}
}

if (_unlock.isEmpty || _unlock.contains(package)) return null;
Expand Down
11 changes: 5 additions & 6 deletions lib/src/source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ abstract class Source {
/// all sources.
String get name;

/// Whether this source can choose between multiple versions of the same
/// package during version solving.
///
/// Defaults to `false`.
bool get hasMultipleVersions => false;

/// Parses a [PackageRef] from a name and a user-provided [description].
///
/// When a [Pubspec] is parsed, it reads in the description for each
Expand Down Expand Up @@ -190,6 +184,11 @@ abstract class Source {
/// with a version constraint.
abstract class Description {
Source get source;

/// Whether the source can choose between multiple versions of this
/// package during version solving.
bool get hasMultipleVersions;

Object? serializeForPubspec({
required String? containingDir,
required LanguageVersion languageVersion,
Expand Down
3 changes: 3 additions & 0 deletions lib/src/source/git.dart
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,9 @@ class GitDescription extends Description {
}
return p.prettyUri(url);
}

@override
bool get hasMultipleVersions => false;
}

class ResolvedGitDescription extends ResolvedDescription {
Expand Down
5 changes: 3 additions & 2 deletions lib/src/source/hosted.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ class HostedSource extends CachedSource {

@override
final name = 'hosted';
@override
final hasMultipleVersions = true;

static String pubDevUrl = 'https://pub.dev';
static String pubDartlangUrl = 'https://pub.dartlang.org';
Expand Down Expand Up @@ -1818,6 +1816,9 @@ class HostedDescription extends Description {

@override
HostedSource get source => HostedSource.instance;

@override
bool get hasMultipleVersions => true;
}

class ResolvedHostedDescription extends ResolvedDescription {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/source/path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ class PathDescription extends Description {

@override
int get hashCode => _canonicalizedPath.hashCode;

@override
bool get hasMultipleVersions => false;
}

class ResolvedPathDescription extends ResolvedDescription {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/source/root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,7 @@ class RootDescription extends Description {

@override
int get hashCode => 'root'.hashCode;

@override
bool get hasMultipleVersions => false;
}
3 changes: 3 additions & 0 deletions lib/src/source/sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ class SdkDescription extends Description {
bool operator ==(Object other) {
return other is SdkDescription && other.sdk == sdk;
}

@override
bool get hasMultipleVersions => false;
}

class ResolvedSdkDescription extends ResolvedDescription {
Expand Down
3 changes: 3 additions & 0 deletions lib/src/source/unknown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class UnknownDescription extends Description {

@override
int get hashCode => Object.hash(source.name, json.encode(description));

@override
bool get hasMultipleVersions => false;
}

class ResolvedUnknownDescription extends ResolvedDescription {
Expand Down