Skip to content
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

Support for resolving all packages in workspace together #4154

Merged
merged 7 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 6 additions & 1 deletion lib/src/command/add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ Specify multiple sdk packages with descriptors.''');
solveResult = await resolveVersions(
SolveType.upgrade,
cache,
Package(resolutionPubspec, entrypoint.rootDir),
Package(
resolutionPubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
),
);
} on GitException {
final name = updates.first.ref.name;
Expand Down Expand Up @@ -356,6 +360,7 @@ Specify multiple sdk packages with descriptors.''');
dependencies: dependencies,
devDependencies: devDependencies,
dependencyOverrides: dependencyOverrides,
workspace: original.workspace,
);
}

Expand Down
38 changes: 32 additions & 6 deletions lib/src/command/dependency_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,21 @@ class DependencyServicesReportCommand extends PubCommand {
final breakingPubspec = stripVersionBounds(compatiblePubspec);

final compatiblePackagesResult = await _tryResolve(
Package(compatiblePubspec, entrypoint.rootDir),
Package(
compatiblePubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
),
cache,
additionalConstraints: additionalConstraints,
);

final breakingPackagesResult = await _tryResolve(
Package(breakingPubspec, entrypoint.rootDir),
Package(
breakingPubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
),
cache,
additionalConstraints: additionalConstraints,
);
Expand All @@ -101,6 +109,7 @@ class DependencyServicesReportCommand extends PubCommand {
sdkConstraints: compatiblePubspec.sdkConstraints,
dependencies: compatiblePubspec.dependencies.values,
devDependencies: compatiblePubspec.devDependencies.values,
workspace: compatiblePubspec.workspace,
);
final dependencySet =
_dependencySetOfPackage(singleBreakingPubspec, package);
Expand All @@ -111,7 +120,11 @@ class DependencyServicesReportCommand extends PubCommand {
.toRef()
.withConstraint(stripUpperBound(package.toRange().constraint));
final singleBreakingPackagesResult = await _tryResolve(
Package(singleBreakingPubspec, entrypoint.rootDir),
Package(
singleBreakingPubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
),
cache,
);
singleBreakingVersion = singleBreakingPackagesResult
Expand All @@ -128,7 +141,11 @@ class DependencyServicesReportCommand extends PubCommand {
);

final smallestUpgradeResult = await _tryResolve(
Package(atLeastCurrentPubspec, entrypoint.rootDir),
Package(
atLeastCurrentPubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
),
cache,
solveType: SolveType.downgrade,
additionalConstraints: additionalConstraints,
Expand Down Expand Up @@ -216,7 +233,14 @@ class DependencyServicesListCommand extends PubCommand {

final currentPackages = fileExists(entrypoint.lockFilePath)
? entrypoint.lockFile.packages.values.toList()
: (await _tryResolve(Package(pubspec, entrypoint.rootDir), cache) ??
: (await _tryResolve(
Package(
pubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
),
cache,
) ??
<PackageId>[]);

final dependencies = <Object>[];
Expand Down Expand Up @@ -428,6 +452,7 @@ class DependencyServicesApplyCommand extends PubCommand {
location: toUri(entrypoint.pubspecPath),
),
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
),
lockFile: updatedLockfile,
);
Expand Down Expand Up @@ -720,6 +745,7 @@ Future<List<Object>> _computeUpgradeSet(
dependencies: rootPubspec.dependencies.values,
devDependencies: rootPubspec.devDependencies.values,
sdkConstraints: rootPubspec.sdkConstraints,
workspace: rootPubspec.workspace,
);

final dependencySet = _dependencySetOfPackage(pubspec, package);
Expand All @@ -734,7 +760,7 @@ Future<List<Object>> _computeUpgradeSet(
? SolveType.downgrade
: SolveType.get,
cache,
Package(pubspec, entrypoint.rootDir),
Package(pubspec, entrypoint.rootDir, entrypoint.root.workspaceChildren),
sigurdm marked this conversation as resolved.
Show resolved Hide resolved
lockFile: lockFile,
additionalConstraints: additionalConstraints,
);
Expand Down
12 changes: 10 additions & 2 deletions lib/src/command/outdated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,23 @@ Consider using the Dart 2.19 sdk to migrate to null safety.''');
'Resolving',
() async {
final upgradablePackagesResult = await _tryResolve(
Package(upgradablePubspec, entrypoint.rootDir),
Package(
upgradablePubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
),
cache,
lockFile: entrypoint.lockFile,
);
hasUpgradableResolution = upgradablePackagesResult != null;
upgradablePackages = upgradablePackagesResult ?? [];

final resolvablePackagesResult = await _tryResolve(
Package(resolvablePubspec, entrypoint.rootDir),
Package(
resolvablePubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
),
cache,
lockFile: entrypoint.lockFile,
);
Expand Down
1 change: 1 addition & 0 deletions lib/src/command/remove.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ To remove a dependency override of a package prefix the package name with
dependencies: dependencies.values,
devDependencies: devDependencies.values,
dependencyOverrides: overrides.values,
workspace: original.workspace,
);
}

Expand Down
7 changes: 6 additions & 1 deletion lib/src/command/upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,11 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
return await resolveVersions(
SolveType.upgrade,
cache,
Package(resolvablePubspec, entrypoint.rootDir),
Package(
resolvablePubspec,
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
),
);
},
condition: _shouldShowSpinner,
Expand Down Expand Up @@ -331,6 +335,7 @@ be direct 'dependencies' or 'dev_dependencies', following packages are not:
Package(
_updatedPubspec(newPubspecText, entrypoint),
entrypoint.rootDir,
entrypoint.root.workspaceChildren,
),
);
changes = tighten(
Expand Down
34 changes: 23 additions & 11 deletions lib/src/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,10 @@ class Entrypoint {
Entrypoint(
this.rootDir,
this.cache, {
Pubspec? pubspec,
}) : _root = pubspec == null ? null : Package(pubspec, rootDir),
({Pubspec pubspec, List<Package> workspacePackages})? preloaded,
}) : _root = preloaded == null
? null
: Package(preloaded.pubspec, rootDir, preloaded.workspacePackages),
globalDir = null {
if (p.isWithin(cache.rootDir, rootDir)) {
fail('Cannot operate on packages inside the cache.');
Expand All @@ -251,7 +253,11 @@ class Entrypoint {
_example,
_packageGraph,
cache,
Package(pubspec, rootDir),
Package(
pubspec,
rootDir,
root.workspaceChildren,
),
globalDir,
);
}
Expand Down Expand Up @@ -330,14 +336,20 @@ class Entrypoint {
}

if (!isGlobal) {
entries.add(
PackageConfigEntry(
name: root.name,
rootUri: p.toUri('../'),
packageUri: p.toUri('lib/'),
languageVersion: root.pubspec.languageVersion,
),
);
/// Run through the entire workspace transitive closure and add an entry
/// for each package.
for (final package in root.transitiveWorkspace) {
entries.add(
PackageConfigEntry(
name: package.name,
rootUri: p.toUri(
p.relative(package.dir, from: p.join(rootDir, '.dart_tool')),
),
packageUri: p.toUri('lib/'),
languageVersion: package.pubspec.languageVersion,
),
);
}
}

final packageConfig = PackageConfig(
Expand Down
1 change: 1 addition & 0 deletions lib/src/global_packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class GlobalPackages {
sources: cache.sources,
),
_packageDir(name),
[],
);

// Resolve it and download its dependencies.
Expand Down
23 changes: 21 additions & 2 deletions lib/src/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ class Package {
/// The parsed pubspec associated with this package.
final Pubspec pubspec;

/// The (non-transitive) workspace packages.
final List<Package> workspaceChildren;

/// The transitive closure of [workspaceChildren] rooted at this package.
///
/// Includes this package.
Iterable<Package> get transitiveWorkspace sync* {
final stack = [this];

while (stack.isNotEmpty) {
final current = stack.removeLast();
yield current;
stack.addAll(current.workspaceChildren);
}
}

/// The immediate dependencies this package specifies in its pubspec.
Map<String, PackageRange> get dependencies => pubspec.dependencies;

Expand Down Expand Up @@ -119,14 +135,17 @@ class Package {
expectedName: name,
allowOverridesFile: withPubspecOverrides,
);
return Package(pubspec, dir);
final workspacePackages = pubspec.workspace
.map((e) => Package.load(null, p.join(dir, e), sources))
.toList();
return Package(pubspec, dir, workspacePackages);
}

/// Creates a package with [pubspec] associated with [dir].
///
/// For temporary resolution attempts [pubspec] does not have to correspond
/// to the one at disk.
Package(this.pubspec, this.dir);
Package(this.pubspec, this.dir, this.workspaceChildren);

/// Given a relative path within this package, returns its absolute path.
///
Expand Down
3 changes: 2 additions & 1 deletion lib/src/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ class PackageGraph {
) {
final packages = {
for (final id in result.packages)
id.name: id.name == entrypoint.root.name
id.name: id.isRoot
? entrypoint.root
: Package(
result.pubspecs[id.name]!,
entrypoint.cache.getDirectory(id),
[],
),
};

Expand Down
4 changes: 4 additions & 0 deletions lib/src/pubspec_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Pubspec stripDevDependencies(Pubspec original) {
dependencies: original.dependencies.values,
devDependencies: [], // explicitly give empty list, to prevent lazy parsing
dependencyOverrides: original.dependencyOverrides.values,
workspace: original.workspace,
);
}

Expand All @@ -36,6 +37,7 @@ Pubspec stripDependencyOverrides(Pubspec original) {
dependencies: original.dependencies.values,
devDependencies: original.devDependencies.values,
dependencyOverrides: [],
workspace: original.workspace,
);
}

Expand Down Expand Up @@ -85,6 +87,7 @@ Pubspec stripVersionBounds(
dependencies: stripBounds(original.dependencies),
devDependencies: stripBounds(original.devDependencies),
dependencyOverrides: original.dependencyOverrides.values,
workspace: original.workspace,
);
}

Expand Down Expand Up @@ -121,6 +124,7 @@ Pubspec atLeastCurrent(Pubspec original, List<PackageId> current) {
dependencies: fixBounds(original.dependencies),
devDependencies: fixBounds(original.devDependencies),
dependencyOverrides: original.dependencyOverrides.values,
workspace: original.workspace,
);
}

Expand Down
Loading