Skip to content

Commit

Permalink
Reject overrides of workspace packages (#4303)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdm authored Jun 10, 2024
1 parent 6171682 commit df3664d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/src/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ See $workspacesDocUrl for more information.
/// * The graph of the workspace rooted at [root] is not a tree.
/// * If a package name occurs twice.
/// * If two packages in the workspace override the same package name.
/// * A workspace package is overridden.
void validateWorkspace(Package root) {
if (root.workspaceChildren.isEmpty) return;

Expand Down Expand Up @@ -430,6 +431,7 @@ Workspace members must have unique names.
}

// Check that the workspace doesn't contain two overrides of the same package.
// Also check that workspace packages are not overridden.
final overridesSeen = <String, Package>{};
for (final package in root.transitiveWorkspace) {
for (final override in package.pubspec.dependencyOverrides.keys) {
Expand All @@ -443,6 +445,14 @@ Consider removing one of the overrides.
''');
}
overridesSeen[override] = package;

if (namesSeen[override] case final Package overriddenWorkspacePackage) {
fail('''
Cannot override workspace packages.
Package `$override` at `${overriddenWorkspacePackage.presentationDir}` is overridden in `${package.pubspecPath}`.
''');
}
}
}
}
31 changes: 31 additions & 0 deletions test/workspace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,37 @@ Consider removing one of the overrides.''',
output: contains('FOO'),
);
});

test('Cannot override workspace packages', () async {
await servePackages();
await dir(appPath, [
libPubspec(
'myapp',
'1.2.3',
extras: {
'workspace': ['pkgs/a'],
'dependency_overrides': {
'a': {'path': 'pkgs/a'},
},
},
sdk: '^3.5.0',
),
dir('pkgs', [
dir('a', [
libPubspec('a', '1.1.1', resolutionWorkspace: true),
]),
]),
]).create();
await pubGet(
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
error: allOf(
contains('Cannot override workspace packages'),
contains(
'Package `a` at `.${s}pkgs/a` is overridden in `pubspec.yaml`.',
),
),
);
});
}

final s = p.separator;

0 comments on commit df3664d

Please sign in to comment.