Skip to content

Prevent non-targets from depending on test targets #8513

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

johnbute
Copy link
Contributor

@johnbute johnbute commented Apr 16, 2025

Prevent non-targets from depending on test targets

Motivation:

Fix for rdar://149007214
Currently, only test targets are allowed to have dependencies on other test targets.

Modifications:

Simply checked for each non-test target, if their dependency is of type test. If it is, throw an error.

Result:

Swift package manager will give an error explaining that only testTargets can depend on other testTargets

Fixes #8478

for dependency in dependencies {
if let depTarget = dependency.module, depTarget.type == .test {
self.observabilityScope.emit(.invalidDependencyOnTestTarget(
dependency: dependency.name,
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (non-blocking): consider passing the entire structure to the dependency and targetName argument to allow the invalidDependencyOnTestTarget to have additional control on possibly using "other" information.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done partially, I cannot pass the entire target structure since the target is of type PotentialModule and not Module

@johnbute johnbute force-pushed the non-target-dependency-fix branch from 067c46c to c21461c Compare April 17, 2025 18:43
@@ -309,6 +309,12 @@ extension Basics.Diagnostic {
return .error("\(messagePrefix) in dependencies of target '\(targetName)'; valid packages are: \(validPackages.map{ "\($0.descriptionForValidation)" }.joined(separator: ", "))")
}

static func invalidDependencyOnTestTarget(dependency: Module.Dependency, targetName: String) -> Self {
Copy link
Member

Choose a reason for hiding this comment

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

question: How best to manage packages out there that might be doing this already? This might need to be an opt-in feature either by the swift-tools comment at the top of the package so that packages can adopt the stricter checking.

Copy link
Contributor Author

@johnbute johnbute Apr 21, 2025

Choose a reason for hiding this comment

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

Currently, if you do this, you get the following error and your package does not build:

❯ swift build --package-path Fixtures/Miscellaneous/DependencyTargetDependsOnTestTarget
[1/1] Planning build
Building for debugging...
error: emit-module command failed with exit code 1 (use -v to see invocation)
/Users/bkhouri/Documents/git/public/swiftlang/swift-package-manager/Fixtures/Miscellaneous/DependencyTargetDependsOnTestTarget/Sources/DependencyTargetDependsOnTestTarget/DependencyTargetDependsOnTestTarget.swift:4:8: error: compiling for macOS 10.13, but module 'DependencyTargetDependsOnTestTargetTests' has a minimum deployment target of macOS 14.0: /Users/bkhouri/Documents/git/public/swiftlang/swift-package-manager/Fixtures/Miscellaneous/DependencyTargetDependsOnTestTarget/.build/arm64-apple-macosx/debug/Modules/DependencyTargetDependsOnTestTargetTests.swiftmodule
2 | // https://docs.swift.org/swift-book
3 |
4 | import DependencyTargetDependsOnTestTargetTests
  |        `- error: compiling for macOS 10.13, but module 'DependencyTargetDependsOnTestTargetTests' has a minimum deployment target of macOS 14.0: /Users/bkhouri/Documents/git/public/swiftlang/swift-package-manager/Fixtures/Miscellaneous/DependencyTargetDependsOnTestTarget/.build/arm64-apple-macosx/debug/Modules/DependencyTargetDependsOnTestTargetTests.swiftmodule
5 |
/Users/bkhouri/Documents/git/public/swiftlang/swift-package-manager/Fixtures/Miscellaneous/DependencyTargetDependsOnTestTarget/Sources/DependencyTargetDependsOnTestTarget/DependencyTargetDependsOnTestTarget.swift:4:8: error: compiling for macOS 10.13, but module 'DependencyTargetDependsOnTestTargetTests' has a minimum deployment target of macOS 14.0: /Users/bkhouri/Documents/git/public/swiftlang/swift-package-manager/Fixtures/Miscellaneous/DependencyTargetDependsOnTestTarget/.build/arm64-apple-macosx/debug/Modules/DependencyTargetDependsOnTestTargetTests.swiftmodule
2 | // https://docs.swift.org/swift-book
3 |
4 | import DependencyTargetDependsOnTestTargetTests
  |        `- error: compiling for macOS 10.13, but module 'DependencyTargetDependsOnTestTargetTests' has a minimum deployment target of macOS 14.0: /Users/bkhouri/Documents/git/public/swiftlang/swift-package-manager/Fixtures/Miscellaneous/DependencyTargetDependsOnTestTarget/.build/arm64-apple-macosx/debug/Modules/DependencyTargetDependsOnTestTargetTests.swiftmodule
5 |

Copy link
Contributor

@bkhouri bkhouri Apr 22, 2025

Choose a reason for hiding this comment

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

If packages are already doing this, they should be updated as it does not make sense, to me, having a non-test target depend on a test target.

Maybe we should add something to the "change log" as a possibly breaking change.

@johnbute
Copy link
Contributor Author

@swift-ci please test

@johnbute
Copy link
Contributor Author

@swift-ci test macOS

@johnbute
Copy link
Contributor Author

@swift-ci test windows

@johnbute
Copy link
Contributor Author

@swift-ci test windows

@johnbute
Copy link
Contributor Author

@swift-ci Please smoke test macOS platform

@johnbute
Copy link
Contributor Author

@swift-ci please test macos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prevent non-test targets from depending on test targets
3 participants