Skip to content

Make add-dependency idempotent #8534

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

Merged
merged 3 commits into from
Apr 30, 2025

Conversation

plemarquand
Copy link
Contributor

Motivation:

If the same dependency is added twice with different version specifiers the manifest is no longer valid. If the same dependency is added twice with the same version specifier it remains valid, but less maintainable.

Modifications:

If a manifest already contains the exact dependency and version that is being added via a add-dependency call, don't add it twice. Instead, do nothing.

If a manifest contains a dependency with the supplied URL but a different version qualifier then throw an error explaining that the supplied dependency has already been added.

Result:

A manifest that continues to parse correctly.

Issue: #8519

@plemarquand
Copy link
Contributor Author

@swift-ci please test

Copy link
Member

@cmcgee1024 cmcgee1024 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: This looks good to me. Having these operations be idempotent will be a really good thing to have to make it easier for code that automates adding capabilities to your package where it may or may not already be there.

/// If the exact same dependency already exists, `false` is returned to indicate
/// that no edits are needed. If a different dependency with the same id or url
/// with different arguments exists, an error is thrown.
private static func checkExistingDependency(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I know this is marked as a private function, but is there value is validate this function in isolation to ensure we can test all code paths to ensure the behaviour is as expected, as opposed to testing the implementation?

try await executeAddURLDependencyAndAssert(
packagePath: path,
initialManifest: manifest,
url: "https://github.com/swiftlang/swift-syntax.git",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: Does SwiftPM treat a package dependency of "https://github.com/swiftlang/swift-syntax.git" the same as "https://github.com/swiftlang/swift-syntax"? Is there value if documenting this specific case?

what about repositories that has the same name, but belong to different organizations/users?

@plemarquand
Copy link
Contributor Author

@swift-ci test windows platform

@plemarquand
Copy link
Contributor Author

@swift-ci test

If a manifest already contains the exact dependency is being
added via a `add-dependency` call, don't add it twice. Instead, do
nothing. This ensures we don't end up with an invalid manifest with two
of the same dependencies in it.

If a manifest contains a dependency with the supplied URL but a
different version qualifier then throw an error explaining that the
supplied dependency has already been added.

Issue: swiftlang#8519
@plemarquand plemarquand force-pushed the idempotent-add-dependency branch from 571350e to 518c227 Compare April 24, 2025 19:55
@plemarquand
Copy link
Contributor Author

@swift-ci test

@plemarquand
Copy link
Contributor Author

@swift-ci please test windows

@plemarquand
Copy link
Contributor Author

@swift-ci test windows

1 similar comment
@plemarquand
Copy link
Contributor Author

@swift-ci test windows

@plemarquand plemarquand merged commit 174cdc2 into swiftlang:main Apr 30, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants