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

Detect duplicate names in workspace packages #4232

Merged
merged 5 commits into from
Apr 23, 2024
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/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class Entrypoint {
);
for (final package in root.transitiveWorkspace) {
if (identical(pubspecsMet.entries.first.value, package.pubspec)) {
validateWorkspaceGraph(root);
validateWorkspace(root);
return (root: root, work: package);
}
}
Expand Down
19 changes: 17 additions & 2 deletions lib/src/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,10 @@ See $workspacesDocUrl for more information.
}

/// Reports an error if the graph of the workspace rooted at [root] is not a
/// tree.
void validateWorkspaceGraph(Package root) {
/// tree. Or if a package name occurs twice.
void validateWorkspace(Package root) {
if (root.workspaceChildren.isEmpty) return;

final includedFrom = <String, String>{};
final stack = [root];

Expand All @@ -382,4 +384,17 @@ Packages can only be included in the workspace once.
}
stack.addAll(current.workspaceChildren);
}

// Check that the workspace doesn't contain two packages with the same name!
final namesSeen = <String, Package>{};
sigurdm marked this conversation as resolved.
Show resolved Hide resolved
for (final package in root.transitiveWorkspace) {
final collision = namesSeen[package.name];
if (collision != null) {
fail('''
Workspace members must have unique names.
`${collision.pubspecPath}` and `${package.pubspecPath}` are both called "${package.name}".
''');
}
namesSeen[package.name] = package;
}
}
12 changes: 11 additions & 1 deletion lib/src/package_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,17 @@ class PackageConfig {
this.generator,
this.generatorVersion,
Map<String, dynamic>? additionalProperties,
}) : additionalProperties = additionalProperties ?? {};
}) : additionalProperties = additionalProperties ?? {} {
final names = <String>{};
// Sanity check:
for (final p in packages) {
if (!names.add(p.name)) {
throw ArgumentError(
'Duplicate name ${p.name} in generated package config',
);
}
}
}

/// Create [PackageConfig] from JSON [data].
///
Expand Down
35 changes: 35 additions & 0 deletions test/workspace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,41 @@ Changed 1 constraint in b${s}pubspec.yaml:
'Because myapp depends on both a 2.0.0 and a, version solving failed.',
);
});

test('Reports error if two members of workspace has same name', () async {
final server = await servePackages();
server.serve('dev_dep', '1.0.0');
await dir(appPath, [
libPubspec(
'myapp',
'1.2.3',
extras: {
'workspace': ['a', 'b'],
},
sdk: '^3.5.0',
),
dir('a', [
libPubspec(
'a',
'1.0.0',
resolutionWorkspace: true,
),
]),
dir('b', [
libPubspec(
'a', // Has same name as sibling.
'1.0.0',
resolutionWorkspace: true,
),
]),
]).create();
await pubGet(
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
error: '''
Workspace members must have unique names.
`a${s}pubspec.yaml` and `b${s}pubspec.yaml` are both called "a".''',
);
});
}

final s = p.separator;