From bfbfb555d804f42f0d36805b43b91c23fa99bdd4 Mon Sep 17 00:00:00 2001 From: Aditya Vyavahare Date: Tue, 12 May 2026 21:48:10 +0530 Subject: [PATCH 1/2] Use: Detect `Package.swift` as a project marker (issue #430) --- Sources/Swiftly/Use.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/Swiftly/Use.swift b/Sources/Swiftly/Use.swift index 6715a059..e5c19502 100644 --- a/Sources/Swiftly/Use.swift +++ b/Sources/Swiftly/Use.swift @@ -177,6 +177,7 @@ struct Use: SwiftlyCommand { } static func findNewVersionFile(_ ctx: SwiftlyCoreContext) async throws -> FilePath? { + let projectMarkers = [".git", "Package.swift"] var cwd = ctx.currentDirectory while !cwd.isEmpty && !cwd.removingRoot().isEmpty { @@ -184,10 +185,10 @@ struct Use: SwiftlyCommand { break } - let gitDir = cwd / ".git" - - if try await fs.exists(atPath: gitDir) { - return cwd / ".swift-version" + for marker in projectMarkers { + if try await fs.exists(atPath: cwd / marker) { + return cwd / ".swift-version" + } } cwd = cwd.removingLastComponent() From fd1efc85a5725b2fb8e92fd7766ec352662f8a24 Mon Sep 17 00:00:00 2001 From: Aditya Vyavahare Date: Tue, 12 May 2026 22:25:27 +0530 Subject: [PATCH 2/2] Add test to ensure `.swift-version` file is created --- Tests/SwiftlyTests/UseTests.swift | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Tests/SwiftlyTests/UseTests.swift b/Tests/SwiftlyTests/UseTests.swift index 1e7fbbca..af6cedf2 100644 --- a/Tests/SwiftlyTests/UseTests.swift +++ b/Tests/SwiftlyTests/UseTests.swift @@ -324,6 +324,20 @@ import Testing // THEN: the use command reports this toolchain to be in use #expect(output.contains(where: { $0.contains(ToolchainVersion.newMainSnapshot.name) })) + // GIVEN: a directory with no swift version file but containing a Package.swift + try await fs.remove(atPath: versionFile) + let packageFile = SwiftlyTests.ctx.currentDirectory / "Package.swift" + try "// swift-tools-version:6.3".write(to: packageFile, atomically: true) + // WHEN: using a toolchain version + try await SwiftlyTests.runCommand( + Use.self, ["use", ToolchainVersion.newReleaseSnapshot.name] + ) + // THEN: a swift version file is created + #expect(try await fs.exists(atPath: versionFile)) + // THEN: the version file contains the specified version + versionFileContents = try String(contentsOf: versionFile) + #expect(ToolchainVersion.newReleaseSnapshot.name == versionFileContents) + // GIVEN: a directory with no swift version file at the top of a git repository try await fs.remove(atPath: versionFile) let gitDir = SwiftlyTests.ctx.currentDirectory / ".git"