From df3664d17fd146ccd19b32820a022404da91f6d3 Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Mon, 10 Jun 2024 16:16:31 +0200 Subject: [PATCH] Reject overrides of workspace packages (#4303) --- lib/src/package.dart | 10 ++++++++++ test/workspace_test.dart | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/src/package.dart b/lib/src/package.dart index 7a2178d4d..4ccab8f58 100644 --- a/lib/src/package.dart +++ b/lib/src/package.dart @@ -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; @@ -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 = {}; for (final package in root.transitiveWorkspace) { for (final override in package.pubspec.dependencyOverrides.keys) { @@ -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}`. +'''); + } } } } diff --git a/test/workspace_test.dart b/test/workspace_test.dart index 63cd9cd5b..8cbd0af7a 100644 --- a/test/workspace_test.dart +++ b/test/workspace_test.dart @@ -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;