From fcbe1847e0b69a30e3ead39aa9ea2f696e518f56 Mon Sep 17 00:00:00 2001 From: belkhadir Date: Thu, 12 Jun 2025 18:26:36 +0100 Subject: [PATCH 1/2] Replace hardcoded config fallback version with dynamic constant MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, when loading the config file, the fallback version `”0.3.0"` was hardcoded to handle unversioned configs from earlier releases. This commit replaces the hardcoded value with the centralized `version` constant, ensuring consistency across the codebase and reducing the risk of version mismatches in the future. --- Sources/Swiftly/Config.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Swiftly/Config.swift b/Sources/Swiftly/Config.swift index 0e8547c1..bdd4b26c 100644 --- a/Sources/Swiftly/Config.swift +++ b/Sources/Swiftly/Config.swift @@ -32,7 +32,7 @@ public struct Config: Codable, Equatable, Sendable { if config.version == nil { // Assume that the version of swiftly is 0.3.0 because that is the last // unversioned release. - config.version = try? SwiftlyVersion(parsing: "0.3.0") + config.version = SwiftlyCore.version } return config } catch { From 8b9d4338ad7d32e33d1874e5cabc99d8cf5f6b85 Mon Sep 17 00:00:00 2001 From: belkhadir Date: Fri, 13 Jun 2025 18:06:07 +0100 Subject: [PATCH 2/2] Make `version` field in config mandatory and remove fallback logic This commit aligns with the decision to drop support for configurations without a version field (pre-0.3.0). The `version` property in `Config` is now non-optional and must be explicitly set at creation. - Removed the fallback assignment to "0.3.0" - Updated all code to require and use non-optional `version` - Adjusted the config initializer to take `version` as a parameter - Simplified conditional checks and eliminated `.version?` This ensures that swiftly no longer silently accepts or upgrades legacy configurations without an explicit version. --- Sources/Swiftly/Config.swift | 13 +++++-------- Sources/Swiftly/Init.swift | 7 +++---- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/Sources/Swiftly/Config.swift b/Sources/Swiftly/Config.swift index bdd4b26c..83eaaca5 100644 --- a/Sources/Swiftly/Config.swift +++ b/Sources/Swiftly/Config.swift @@ -9,12 +9,13 @@ public struct Config: Codable, Equatable, Sendable { public var inUse: ToolchainVersion? public var installedToolchains: Set public var platform: PlatformDefinition - public var version: SwiftlyVersion? + public var version: SwiftlyVersion - init(inUse: ToolchainVersion?, installedToolchains: Set, platform: PlatformDefinition) { + init(inUse: ToolchainVersion?, installedToolchains: Set, platform: PlatformDefinition, version: SwiftlyVersion) { self.inUse = inUse self.installedToolchains = installedToolchains self.platform = platform + self.version = version } private static func makeEncoder() -> JSONEncoder { @@ -28,12 +29,8 @@ public struct Config: Codable, Equatable, Sendable { do { let configFile = Swiftly.currentPlatform.swiftlyConfigFile(ctx) let data = try await fs.cat(atPath: configFile) - var config = try JSONDecoder().decode(Config.self, from: data) - if config.version == nil { - // Assume that the version of swiftly is 0.3.0 because that is the last - // unversioned release. - config.version = SwiftlyCore.version - } + let config = try JSONDecoder().decode(Config.self, from: data) + return config } catch { let msg = """ diff --git a/Sources/Swiftly/Init.swift b/Sources/Swiftly/Init.swift index 60185699..d7d46b6e 100644 --- a/Sources/Swiftly/Init.swift +++ b/Sources/Swiftly/Init.swift @@ -48,7 +48,7 @@ struct Init: SwiftlyCommand { ( config.version == SwiftlyVersion(major: 0, minor: 4, patch: 0, suffix: "dev") || config.version == SwiftlyVersion(major: 0, minor: 4, patch: 0) || - (config.version?.major == 1 && config.version?.minor == 0) + (config.version.major == 1 && config.version.minor == 0) ) { // This is a simple upgrade from the 0.4.0 pre-releases, or 1.x @@ -178,9 +178,8 @@ struct Init: SwiftlyCommand { // Force the configuration to be present. Generate it if it doesn't already exist or overwrite is set if overwrite || config == nil { let pd = try await Swiftly.currentPlatform.detectPlatform(ctx, disableConfirmation: assumeYes, platform: platform) - var c = Config(inUse: nil, installedToolchains: [], platform: pd) - // Stamp the current version of swiftly on this config - c.version = SwiftlyCore.version + let c = Config(inUse: nil, installedToolchains: [], platform: pd, version: SwiftlyCore.version) + try c.save(ctx) config = c }