diff --git a/CHANGELOG.md b/CHANGELOG.md index 50835855b4..c41505fec1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,11 @@ [Hokila](https://github.com/Hokila) [#6897](https://github.com/realm/SwiftLint/issues/6897) +* Fix `--working-directory` having no effect. The `defer` restoring the previous + directory was scoped to the `if` block, so it ran before linting started. + [haruna-miyamoto](https://github.com/haruna-miyamoto) + [#6907](https://github.com/realm/SwiftLint/issues/6907) + ## 0.65.1: Fresh Folded Fixtures ### Breaking diff --git a/Source/SwiftLintFramework/LintOrAnalyzeCommand.swift b/Source/SwiftLintFramework/LintOrAnalyzeCommand.swift index a6214bb8c6..636752f113 100644 --- a/Source/SwiftLintFramework/LintOrAnalyzeCommand.swift +++ b/Source/SwiftLintFramework/LintOrAnalyzeCommand.swift @@ -129,13 +129,14 @@ package struct LintOrAnalyzeOptions { package struct LintOrAnalyzeCommand { package static func run(_ options: LintOrAnalyzeOptions) async throws { Request.disableSourceKitOverride = options.mode == .lint && options.disableSourceKit - if let workingDirectory = options.workingDirectory { - let currentDirectory = FileManager.default.currentDirectoryPath - defer { - if !FileManager.default.changeCurrentDirectoryPath(currentDirectory) { - queuedFatalError("Could not change back to the original directory '\(currentDirectory)'.") - } + var currentDirectory: String? + defer { + if let currentDirectory, !FileManager.default.changeCurrentDirectoryPath(currentDirectory) { + queuedFatalError("Could not change back to the original directory '\(currentDirectory)'.") } + } + if let workingDirectory = options.workingDirectory { + currentDirectory = FileManager.default.currentDirectoryPath if !FileManager.default.changeCurrentDirectoryPath(workingDirectory) { throw SwiftLintError.usageError( description: """