Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Sources/Swiftly/Use.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,18 @@ 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 {
guard try await fs.exists(atPath: cwd) else {
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()
Expand Down
14 changes: 14 additions & 0 deletions Tests/SwiftlyTests/UseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down