Skip to content

Commit f865129

Browse files
committed
[PackageLoading] Ignore version-specific manifests.
1 parent d34d7f1 commit f865129

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

Sources/PackageLoading/PackageBuilder.swift

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,33 @@ public struct PackageBuilder {
222222
// MARK: Utility Predicates
223223

224224
private func isValidSource(_ path: AbsolutePath) -> Bool {
225-
return isValidSource(path, validExtensions: SupportedLanguageExtension.validExtensions)
226-
}
227-
228-
private func isValidSource(_ path: AbsolutePath, validExtensions: Set<String>) -> Bool {
229-
if path.basename.hasPrefix(".") { return false }
230-
if path == manifest.path { return false }
231-
if excludedPaths.contains(path) { return false }
225+
// Ignore files which don't match the expected extensions.
226+
guard let ext = path.extension, SupportedLanguageExtension.validExtensions.contains(ext) else {
227+
return false
228+
}
229+
230+
// Ignore dotfiles.
231+
let basename = path.basename
232+
if basename.hasPrefix(".") { return false }
233+
234+
// Ignore symlinks to non-files.
232235
if !fileSystem.isFile(path) { return false }
233-
guard let ext = path.extension else { return false }
234-
return validExtensions.contains(ext)
236+
237+
// Ignore excluded files.
238+
if excludedPaths.contains(path) { return false }
239+
240+
// Ignore manifest files.
241+
if path.parentDirectory == packagePath {
242+
if basename == Manifest.filename { return false }
243+
244+
// Ignore version-specific manifest files.
245+
if basename.hasPrefix(Manifest.basename + "@") && basename.hasSuffix(".swift") {
246+
return false
247+
}
248+
}
249+
250+
// Otherwise, we have a valid source file.
251+
return true
235252
}
236253

237254
private func shouldConsiderDirectory(_ path: AbsolutePath) -> Bool {
@@ -414,7 +431,7 @@ public struct PackageBuilder {
414431
}
415432
}
416433

417-
/// Private function that constructs a single Module object for the moduel at `path`, having the name `name`. If `isTest` is true, the module is constructed as a test module; if false, it is a regular module.
434+
/// Private function that constructs a single Module object for the module at `path`, having the name `name`. If `isTest` is true, the module is constructed as a test module; if false, it is a regular module.
418435
private func createModule(_ path: AbsolutePath, name: String, isTest: Bool) throws -> Module {
419436

420437
// Validate the module name. This function will throw an error if it detects a problem.
@@ -424,9 +441,11 @@ public struct PackageBuilder {
424441
let walked = try walk(path, fileSystem: fileSystem, recursing: shouldConsiderDirectory).map{ $0 }
425442

426443
// Select any source files for the C-based languages and for Swift.
427-
let cSources = walked.filter{ isValidSource($0, validExtensions: SupportedLanguageExtension.cFamilyExtensions) }
428-
let swiftSources = walked.filter{ isValidSource($0, validExtensions: SupportedLanguageExtension.swiftExtensions) }
429-
444+
let sources = walked.filter(isValidSource)
445+
let cSources = sources.filter{ SupportedLanguageExtension.cFamilyExtensions.contains($0.extension!) }
446+
let swiftSources = sources.filter{ SupportedLanguageExtension.swiftExtensions.contains($0.extension!) }
447+
assert(sources.count == cSources.count + swiftSources.count)
448+
430449
// Create and return the right kind of module depending on what kind of sources we found.
431450
if cSources.isEmpty {
432451
// No C sources, so we expect to have Swift sources, and we create a Swift module.

Tests/PackageLoadingTests/ConventionTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,23 @@ class ConventionTests: XCTestCase {
673673
}
674674
}
675675

676+
func testVersionSpecificManifests() throws {
677+
var fs = InMemoryFileSystem()
678+
try fs.createEmptyFiles(
679+
"/Package.swift",
680+
681+
"/Sources/Package.swift",
682+
"/Sources/[email protected]")
683+
684+
let name = "Foo"
685+
PackageBuilderTester(name, in: fs) { result in
686+
result.checkModule(name) { moduleResult in
687+
moduleResult.check(c99name: name, type: .library, isTest: false)
688+
moduleResult.checkSources(root: "/Sources", paths: "Package.swift", "[email protected]")
689+
}
690+
}
691+
}
692+
676693
// MARK:- Invalid Layouts Tests
677694

678695
func testMultipleRoots() throws {
@@ -845,6 +862,7 @@ class ConventionTests: XCTestCase {
845862
("testManifestTargetDeclErrors", testManifestTargetDeclErrors),
846863
("testProducts", testProducts),
847864
("testBadProducts", testBadProducts),
865+
("testVersionSpecificManifests", testVersionSpecificManifests),
848866
("testTestsProduct", testTestsProduct),
849867
]
850868
}

0 commit comments

Comments
 (0)