Skip to content

Commit 8ba7789

Browse files
committed
Throw error for mixed targets when tool version is insufficient
1 parent 44377cf commit 8ba7789

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Sources/PackageLoading/PackageBuilder.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ extension Target {
153153
case emptyName
154154
}
155155

156+
/// The target contains an invalid mix of languages (e.g. both Swift and C).
157+
case mixedSources(AbsolutePath)
158+
156159
}
157160
}
158161

@@ -161,6 +164,10 @@ extension Target.Error: CustomStringConvertible {
161164
switch self {
162165
case .invalidName(let path, let problem):
163166
return "invalid target name at '\(path)'; \(problem)"
167+
case .mixedSources(let path):
168+
// TODO(ncooke3): Update error message with support version.
169+
return "target at '\(path)' contains mixed language source " +
170+
"files; feature not supported until tools version XX"
164171
}
165172
}
166173
}
@@ -1418,6 +1425,10 @@ extension Sources {
14181425
}
14191426
}
14201427

1428+
var containsMixedLanguage: Bool {
1429+
return hasSwiftSources && hasClangSources
1430+
}
1431+
14211432
/// Determine target type based on the sources.
14221433
fileprivate func computeTargetType() -> Target.Kind {
14231434
let isLibrary = !relativePaths.contains { path in

Sources/PackageLoading/TargetSourcesBuilder.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ public struct TargetSourcesBuilder {
180180
diagnoseInfoPlistConflicts(in: resources)
181181
diagnoseInvalidResource(in: target.resources)
182182

183+
// It's an error to contain mixed language source files.
184+
// TODO(ncooke3): Update `toolsVersion` when the PR merges.
185+
if sources.containsMixedLanguage, toolsVersion < .v4 {
186+
throw Target.Error.mixedSources(targetPath)
187+
}
188+
183189
return (sources, resources, headers, ignored, others)
184190
}
185191

Tests/PackageLoadingTests/PackageBuilderTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ class PackageBuilderTests: XCTestCase {
4040
}
4141
}
4242

43+
func testMixedSourcesWhenUnsupportedToolsVersion() throws {
44+
let foo: AbsolutePath = AbsolutePath(path: "/Sources/foo")
45+
46+
let fs = InMemoryFileSystem(emptyFiles:
47+
foo.appending(components: "Foo.swift").pathString,
48+
foo.appending(components: "bar.c").pathString
49+
)
50+
51+
let manifest = Manifest.createRootManifest(
52+
name: "pkg",
53+
path: .root,
54+
toolsVersion: .v3,
55+
targets: [
56+
try TargetDescription(name: "foo"),
57+
]
58+
)
59+
PackageBuilderTester(manifest, in: fs) { _, diagnostics in
60+
// TODO(ncooke3): Update error message with support version.
61+
diagnostics.check(diagnostic: "target at '\(foo)' contains mixed language source files; feature not supported until tools version XX", severity: .error)
62+
}
63+
}
64+
4365
func testMixedSources() throws {
4466
let foo: AbsolutePath = AbsolutePath(path: "/Sources/foo")
4567

0 commit comments

Comments
 (0)