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() 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"