SwiftLint does not recognise the file path correctly
environment
mac os: 15.2 version: 3.21.1
description
I encountered an issue where, if the Dangerfile is not located in the root directory, SwiftLint does not correctly recognize the file paths. After reviewing the library's code, it seems there is no option to change the working directory for executing the SwiftLint command.
steps to reproduce
example: https://github.com/taise-hikawa/danger-sample
if you add SwiftLint.lint(inline: true, configFile: "../.swiftlint.yml") to Dangerfile.swift and try following command at ./LocalPackage directory, you can get following error messages.
swift run danger-swift pr https://github.com/taise-hikawa/danger-sample/pull/2 --verbose
error messages
Could not read contents of `/Users/hikawataisei/Desktop/code/private/danger-sample/LocalPackage/LocalPackage/Dangerfile.swift`
Could not read contents of `/Users/hikawataisei/Desktop/code/private/danger-sample/LocalPackage/LocalPackage/Package.swift`
Could not read contents of `/Users/hikawataisei/Desktop/code/private/danger-sample/LocalPackage/LocalPackage/Sources/DangerTarget/Fake.swift`
Could not read contents of `/Users/hikawataisei/Desktop/code/private/danger-sample/LocalPackage/LocalPackage/Sources/Hoge/HogeView.swift`
Could not read contents of `/Users/hikawataisei/Desktop/code/private/danger-sample/LocalPackage/danger-sample/DangerSampleApp.swift`
It appears this might be a bug in the Danger library. Could you please look into this? Let me know if you need any further details. Thank you for your time and help.
I got a similar issue on my end trying to run danger from a different directory like this:
DEBUG="*" swift run danger-swift ci --dangerfile ../../../Dangerfile.swift
It seems danger dsl is getting the modified paths without taking into account I'm running danger from another directory, I ended up correcting the files paths myself like this:
let rootFolderPath = "../../.."
let files = (danger.git.createdFiles + danger.git.modifiedFiles)
.compactMap { "\(rootFolderPath)/\($0)" }
SwiftLint.lint(
.files(files),
configFile: "\(rootFolderPath)/.swiftlint.yml",
strict: true,
swiftlintPath: .bin("\(rootFolderPath)/utils/swiftlint/swiftlint")
)
@Arafo
thanks!!
I also solved this problem with the workaround using .file!
I think it would be ideal to have a solution without needing a workaround, so I'll keep this issue open.
I think you might be looking at the --cwd option https://github.com/danger/swift?tab=readme-ov-file#current-working-directory let me know if that fixes it for you!