Skip to content

Commit 187a29d

Browse files
authored
.docc bundles should be included in the list of target files provided to plugins (#4000) (#4001)
There was a special rule added in 5.5 to mark .docc bundles as ignored, so that there wouldn't be warnings about packages that contained them (since Xcode could deal with them but SwiftPM couldn't). But that also caused .docc files to become filtered out of the source file lists given to plugins. This change keeps ignored files from causing unhandled-files warnings, but it passes them along to plugins as "unknown" role, just other general files. rdar://86785502 (cherry picked from commit 333c9f3)
1 parent f7aa5f8 commit 187a29d

File tree

6 files changed

+68
-14
lines changed

6 files changed

+68
-14
lines changed

Sources/PackageLoading/PackageBuilder.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ public final class PackageBuilder {
810810
fileSystem: self.fileSystem,
811811
observabilityScope: self.observabilityScope
812812
)
813-
let (sources, resources, headers, others) = try sourcesBuilder.run()
813+
let (sources, resources, headers, ignored, others) = try sourcesBuilder.run()
814814

815815
// Make sure defaultLocalization is set if the target has localized resources.
816816
let hasLocalizedResources = resources.contains(where: { $0.localization != nil })
@@ -868,6 +868,7 @@ public final class PackageBuilder {
868868
type: targetType,
869869
sources: sources,
870870
resources: resources,
871+
ignored: ignored,
871872
others: others,
872873
dependencies: dependencies,
873874
swiftVersion: try swiftVersion(),
@@ -902,6 +903,7 @@ public final class PackageBuilder {
902903
type: targetType,
903904
sources: sources,
904905
resources: resources,
906+
ignored: ignored,
905907
dependencies: dependencies,
906908
buildSettings: buildSettings
907909
)

Sources/PackageLoading/TargetSourcesBuilder.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public struct TargetSourcesBuilder {
154154
}
155155

156156
/// Run the builder to produce the sources of the target.
157-
public func run() throws -> (sources: Sources, resources: [Resource], headers: [AbsolutePath], others: [AbsolutePath]) {
157+
public func run() throws -> (sources: Sources, resources: [Resource], headers: [AbsolutePath], ignored: [AbsolutePath], others: [AbsolutePath]) {
158158
let contents = self.computeContents()
159159
var pathToRule: [AbsolutePath: Rule] = [:]
160160

@@ -166,6 +166,7 @@ public struct TargetSourcesBuilder {
166166
let compilePaths = pathToRule.lazy.filter { $0.value.rule == .compile }.map { $0.key }
167167
let sources = Sources(paths: Array(compilePaths), root: targetPath)
168168
let resources: [Resource] = pathToRule.compactMap { resource(for: $0.key, with: $0.value) }
169+
let ignored = pathToRule.filter { $0.value.rule == .ignored }.map { $0.key }
169170
let others = pathToRule.filter { $0.value.rule == .none }.map { $0.key }
170171

171172
diagnoseConflictingResources(in: resources)
@@ -180,7 +181,7 @@ public struct TargetSourcesBuilder {
180181
throw Target.Error.mixedSources(targetPath)
181182
}
182183

183-
return (sources, resources, headers, others)
184+
return (sources, resources, headers, ignored, others)
184185
}
185186

186187
private struct Rule {

Sources/PackageModel/Target.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ public class Target: PolymorphicCodableProtocol {
130130
/// The resource files in the target.
131131
public let resources: [Resource]
132132

133+
/// Files in the target that were marked as ignored.
134+
public let ignored: [AbsolutePath]
135+
133136
/// Other kinds of files in the target.
134137
public let others: [AbsolutePath]
135138

@@ -155,6 +158,7 @@ public class Target: PolymorphicCodableProtocol {
155158
type: Kind,
156159
sources: Sources,
157160
resources: [Resource] = [],
161+
ignored: [AbsolutePath] = [],
158162
others: [AbsolutePath] = [],
159163
dependencies: [Target.Dependency],
160164
buildSettings: BuildSettings.AssignmentTable,
@@ -167,6 +171,7 @@ public class Target: PolymorphicCodableProtocol {
167171
self.type = type
168172
self.sources = sources
169173
self.resources = resources
174+
self.ignored = ignored
170175
self.others = others
171176
self.dependencies = dependencies
172177
self.c99name = self.name.spm_mangledToC99ExtendedIdentifier()
@@ -175,7 +180,7 @@ public class Target: PolymorphicCodableProtocol {
175180
}
176181

177182
private enum CodingKeys: String, CodingKey {
178-
case name, bundleName, defaultLocalization, platforms, type, sources, resources, others, buildSettings, pluginUsages
183+
case name, bundleName, defaultLocalization, platforms, type, sources, resources, ignored, others, buildSettings, pluginUsages
179184
}
180185

181186
public func encode(to encoder: Encoder) throws {
@@ -190,6 +195,7 @@ public class Target: PolymorphicCodableProtocol {
190195
try container.encode(type, forKey: .type)
191196
try container.encode(sources, forKey: .sources)
192197
try container.encode(resources, forKey: .resources)
198+
try container.encode(ignored, forKey: .ignored)
193199
try container.encode(others, forKey: .others)
194200
try container.encode(buildSettings, forKey: .buildSettings)
195201
// FIXME: pluginUsages property is skipped on purpose as it points to
@@ -205,6 +211,7 @@ public class Target: PolymorphicCodableProtocol {
205211
self.type = try container.decode(Kind.self, forKey: .type)
206212
self.sources = try container.decode(Sources.self, forKey: .sources)
207213
self.resources = try container.decode([Resource].self, forKey: .resources)
214+
self.ignored = try container.decode([AbsolutePath].self, forKey: .ignored)
208215
self.others = try container.decode([AbsolutePath].self, forKey: .others)
209216
// FIXME: dependencies property is skipped on purpose as it points to
210217
// the actual target dependency object.
@@ -264,6 +271,7 @@ public final class SwiftTarget: Target {
264271
type: Kind,
265272
sources: Sources,
266273
resources: [Resource] = [],
274+
ignored: [AbsolutePath] = [],
267275
others: [AbsolutePath] = [],
268276
dependencies: [Target.Dependency] = [],
269277
swiftVersion: SwiftLanguageVersion,
@@ -279,6 +287,7 @@ public final class SwiftTarget: Target {
279287
type: type,
280288
sources: sources,
281289
resources: resources,
290+
ignored: ignored,
282291
others: others,
283292
dependencies: dependencies,
284293
buildSettings: buildSettings,
@@ -434,6 +443,7 @@ public final class ClangTarget: Target {
434443
type: Kind,
435444
sources: Sources,
436445
resources: [Resource] = [],
446+
ignored: [AbsolutePath] = [],
437447
others: [AbsolutePath] = [],
438448
dependencies: [Target.Dependency] = [],
439449
buildSettings: BuildSettings.AssignmentTable = .init()
@@ -453,6 +463,7 @@ public final class ClangTarget: Target {
453463
type: type,
454464
sources: sources,
455465
resources: resources,
466+
ignored: ignored,
456467
others: others,
457468
dependencies: dependencies,
458469
buildSettings: buildSettings,

Sources/SPMBuildCore/PluginInvocation.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,9 @@ struct PluginScriptRunnerInputSerializer {
824824
targetFiles.append(contentsOf: try target.underlyingTarget.resources.map {
825825
.init(basePathId: try serialize(path: $0.path.parentDirectory), name: $0.path.basename, type: .resource)
826826
})
827+
targetFiles.append(contentsOf: try target.underlyingTarget.ignored.map {
828+
.init(basePathId: try serialize(path: $0.parentDirectory), name: $0.basename, type: .unknown)
829+
})
827830
targetFiles.append(contentsOf: try target.underlyingTarget.others.map {
828831
.init(basePathId: try serialize(path: $0.parentDirectory), name: $0.basename, type: .unknown)
829832
})

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,17 @@ final class PackageToolTests: CommandsTestCase {
12321232
public func Foo() { }
12331233
"""
12341234
}
1235+
try localFileSystem.writeFileContents(packageDir.appending(components: "Sources", "MyLibrary", "test.docc")) {
1236+
$0 <<< """
1237+
<?xml version="1.0" encoding="UTF-8"?>
1238+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">
1239+
<plist version="1.0">
1240+
<dict>
1241+
<key>CFBundleName</key>
1242+
<string>sample</string>
1243+
</dict>
1244+
"""
1245+
}
12351246
let hostTriple = try UserToolchain(destination: .hostDestination()).triple
12361247
let hostTripleString = hostTriple.isDarwin() ? hostTriple.tripleString(forPlatformVersion: "") : hostTriple.tripleString
12371248
try localFileSystem.writeFileContents(packageDir.appending(components: "Binaries", "LocalBinaryTool.artifactbundle", "info.json")) {
@@ -1293,6 +1304,13 @@ final class PackageToolTests: CommandsTestCase {
12931304
print("Looking for sed...")
12941305
let sed = try context.tool(named: "sed")
12951306
print("... found it at \\(sed.path)")
1307+
1308+
// Print out the source files so that we can check them.
1309+
if let sourceFiles = (targets.first{ $0.name == "MyLibrary" } as? SourceModuleTarget)?.sourceFiles {
1310+
for file in sourceFiles {
1311+
print(" \\(file.path): \\(file.type)")
1312+
}
1313+
}
12961314
}
12971315
}
12981316
"""
@@ -1340,29 +1358,42 @@ final class PackageToolTests: CommandsTestCase {
13401358
// Check that we can invoke the plugin with the "plugin" subcommand.
13411359
do {
13421360
let result = try SwiftPMProduct.SwiftPackage.executeProcess(["plugin", "mycmd"], packagePath: packageDir)
1343-
XCTAssertEqual(result.exitStatus, .terminated(code: 0))
1344-
XCTAssertMatch(try result.utf8Output(), .contains("This is MyCommandPlugin."))
1361+
let output = try result.utf8Output() + result.utf8stderrOutput()
1362+
XCTAssertEqual(result.exitStatus, .terminated(code: 0), "output: \(output)")
1363+
XCTAssertMatch(output, .contains("This is MyCommandPlugin."))
13451364
}
13461365

13471366
// Check that we can also invoke it without the "plugin" subcommand.
13481367
do {
13491368
let result = try SwiftPMProduct.SwiftPackage.executeProcess(["mycmd"], packagePath: packageDir)
1350-
XCTAssertEqual(result.exitStatus, .terminated(code: 0))
1351-
XCTAssertMatch(try result.utf8Output(), .contains("This is MyCommandPlugin."))
1369+
let output = try result.utf8Output() + result.utf8stderrOutput()
1370+
XCTAssertEqual(result.exitStatus, .terminated(code: 0), "output: \(output)")
1371+
XCTAssertMatch(output, .contains("This is MyCommandPlugin."))
13521372
}
13531373

13541374
// Testing listing the available command plugins.
13551375
do {
13561376
let result = try SwiftPMProduct.SwiftPackage.executeProcess(["plugin", "--list"], packagePath: packageDir)
1357-
XCTAssertEqual(result.exitStatus, .terminated(code: 0))
1358-
XCTAssertMatch(try result.utf8Output(), .contains("‘mycmd’ (plugin ‘MyPlugin’ in package ‘MyPackage’)"))
1377+
let output = try result.utf8Output() + result.utf8stderrOutput()
1378+
XCTAssertEqual(result.exitStatus, .terminated(code: 0), "output: \(output)")
1379+
XCTAssertMatch(output, .contains("‘mycmd’ (plugin ‘MyPlugin’ in package ‘MyPackage’)"))
13591380
}
13601381

13611382
// Check that we get the expected error if trying to invoke a plugin with the wrong name.
13621383
do {
13631384
let result = try SwiftPMProduct.SwiftPackage.executeProcess(["my-nonexistent-cmd"], packagePath: packageDir)
1364-
XCTAssertNotEqual(result.exitStatus, .terminated(code: 0))
1365-
XCTAssertMatch(try result.utf8stderrOutput(), .contains("No command plugins found for ‘my-nonexistent-cmd’"))
1385+
let output = try result.utf8Output() + result.utf8stderrOutput()
1386+
XCTAssertNotEqual(result.exitStatus, .terminated(code: 0), "output: \(output)")
1387+
XCTAssertMatch(output, .contains("No command plugins found for ‘my-nonexistent-cmd’"))
1388+
}
1389+
1390+
// Check that the .docc file was properly vended to the plugin.
1391+
do {
1392+
let result = try SwiftPMProduct.SwiftPackage.executeProcess(["mycmd"], packagePath: packageDir)
1393+
let output = try result.utf8Output() + result.utf8stderrOutput()
1394+
XCTAssertEqual(result.exitStatus, .terminated(code: 0), "output: \(output)")
1395+
XCTAssertMatch(output, .contains("Sources/MyLibrary/library.swift: source"))
1396+
XCTAssertMatch(output, .contains("Sources/MyLibrary/test.docc: unknown"))
13661397
}
13671398
}
13681399
}

Tests/PackageLoadingTests/TargetSourcesBuilderTests.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ class TargetSourcesBuilderTests: XCTestCase {
858858
let outputs = try builder.run()
859859
XCTAssertEqual(outputs.sources.paths, [AbsolutePath("/File.swift")])
860860
XCTAssertEqual(outputs.resources, [])
861+
XCTAssertEqual(outputs.ignored, [])
861862
XCTAssertEqual(outputs.others, [AbsolutePath("/Foo.xcdatamodel")])
862863

863864
XCTAssertFalse(observability.hasWarningDiagnostics)
@@ -898,6 +899,7 @@ class TargetSourcesBuilderTests: XCTestCase {
898899
let outputs = try builder.run()
899900
XCTAssertEqual(outputs.sources.paths, [AbsolutePath("/File.swift")])
900901
XCTAssertEqual(outputs.resources, [])
902+
XCTAssertEqual(outputs.ignored, [])
901903
XCTAssertEqual(outputs.others, [AbsolutePath("/foo.bar")])
902904

903905
XCTAssertFalse(observability.hasWarningDiagnostics)
@@ -957,7 +959,11 @@ class TargetSourcesBuilderTests: XCTestCase {
957959
fileSystem: fs,
958960
observabilityScope: observability.topScope
959961
)
960-
_ = try builder.run()
962+
let outputs = try builder.run()
963+
XCTAssertEqual(outputs.sources.paths, [AbsolutePath("/File.swift")])
964+
XCTAssertEqual(outputs.resources, [])
965+
XCTAssertEqual(outputs.ignored, [AbsolutePath("/Foo.docc")])
966+
XCTAssertEqual(outputs.others, [])
961967

962968
XCTAssertNoDiagnostics(observability.diagnostics)
963969
}
@@ -990,7 +996,7 @@ class TargetSourcesBuilderTests: XCTestCase {
990996
)
991997

992998
do {
993-
let (sources, resources, headers, others) = try builder.run()
999+
let (sources, resources, headers, _, others) = try builder.run()
9941000

9951001
testDiagnostics(observability.diagnostics, problemsOnly: checkProblemsOnly, file: file, line: line) { diagnostics in
9961002
try checker(sources, resources, headers, others, builder.packageIdentity, builder.packageKind, builder.packagePath, diagnostics)

0 commit comments

Comments
 (0)