Skip to content

Commit

Permalink
fix: only include normal dependencies of transitive dependencies (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
blaugold committed Oct 4, 2022
1 parent b620136 commit e0659e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/melos/lib/src/package.dart
Expand Up @@ -755,17 +755,17 @@ class Package {
};

/// The dependencies listed in `dependencies:` inside the package's
/// `pubspec.yaml` that are part of the melos workspace
/// `pubspec.yaml` that are part of the melos workspace.
late final Map<String, Package> dependenciesInWorkspace =
_packagesInWorkspaceForNames(dependencies);

/// The dependencies listed in `dev_dependencies:` inside the package's
/// `pubspec.yaml` that are part of the melos workspace
/// `pubspec.yaml` that are part of the melos workspace.
late final Map<String, Package> devDependenciesInWorkspace =
_packagesInWorkspaceForNames(devDependencies);

/// The dependencies listed in `dependency_overrides:` inside the package's
/// `pubspec.yaml` that are part of the melos workspace
/// `pubspec.yaml` that are part of the melos workspace.
late final Map<String, Package> dependencyOverridesInWorkspace =
_packagesInWorkspaceForNames(dependencyOverrides);

Expand All @@ -776,7 +776,7 @@ class Package {
entry.key: entry.value,
};

/// The packages that depend on this package as a dev dependency..
/// The packages that depend on this package as a dev dependency.
late final Map<String, Package> devDependentsInWorkspace = {
for (final entry in _packageMap.entries)
if (entry.value.devDependenciesInWorkspace.containsKey(name))
Expand All @@ -793,13 +793,15 @@ class Package {
late final Map<String, Package> allTransitiveDependenciesInWorkspace =
_transitivelyRelatedPackages(
root: this,
directlyRelatedPackages: (package) => package.allDependenciesInWorkspace,
directlyRelatedPackages: (package, isRoot) => isRoot
? package.allDependenciesInWorkspace
: package.dependenciesInWorkspace,
);

late final Map<String, Package> allTransitiveDependentsInWorkspace =
_transitivelyRelatedPackages(
root: this,
directlyRelatedPackages: (package) => package.allDependentsInWorkspace,
directlyRelatedPackages: (package, _) => package.allDependentsInWorkspace,
);

Map<String, Package> _packagesInWorkspaceForNames(List<String> names) {
Expand Down Expand Up @@ -1103,10 +1105,11 @@ class Package {
/// related to it.
Map<String, Package> _transitivelyRelatedPackages({
required Package root,
required Map<String, Package> Function(Package) directlyRelatedPackages,
required Map<String, Package> Function(Package, bool isRoot)
directlyRelatedPackages,
}) {
final result = <String, Package>{};
final workingSet = directlyRelatedPackages(root).values.toList();
final workingSet = directlyRelatedPackages(root, true).values.toList();

while (workingSet.isNotEmpty) {
final current = workingSet.removeLast();
Expand All @@ -1120,7 +1123,7 @@ Map<String, Package> _transitivelyRelatedPackages({
// Since `current` is a package that was not in the result, we are
// seeing it for the first time and still need to traverse its related
// packages.
workingSet.insertAll(0, directlyRelatedPackages(current).values);
workingSet.insertAll(0, directlyRelatedPackages(current, false).values);

return current;
});
Expand Down
24 changes: 24 additions & 0 deletions packages/melos/test/package_test.dart
Expand Up @@ -77,6 +77,30 @@ void main() {
);
});

group('Package', () {
group('allTransitiveDependenciesInWorkspace', () {
test('does not included transitive dev dependencies', () {
final workspaceBuilder = VirtualWorkspaceBuilder('name: test');
workspaceBuilder.addPackage('''
name: a
''');
workspaceBuilder.addPackage('''
name: b
dev_dependencies:
a: any
''');
workspaceBuilder.addPackage('''
name: c
dependencies:
b: any
''');
final workspace = workspaceBuilder.build();
final cPackage = workspace.allPackages['c']!;
expect(cPackage.allTransitiveDependenciesInWorkspace.keys, ['b']);
});
});
});

group('PackageFilter', () {
test('default', () {
final filter = PackageFilter();
Expand Down

0 comments on commit e0659e9

Please sign in to comment.