realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.64k stars 2.22k forks source link

swiftlint --fix path error #4127

Closed wasdkhan closed 2 years ago

wasdkhan commented 2 years ago

Is there anyway to support piping the auto correctable rules into stdout or another file, similar to how SwiftFormat does here: https://github.com/nicklockwood/SwiftFormat#command-line-tool ?

To my knowledge, SwiftLint autocorrect (--fix) only writes to disk, so we do not see the change before it makes it, only the warning/error.

Replication: Colon spacing error sample file:

// Sample.swift
let x : String = " "

Command:

cat sample.swift | swiftlint lint --fix --use-stdin --path output.swift

Error:

file needs a path to call write(_:): file SwiftLintFile+Regex.swift, line 250
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Aborted (core dumped)
SimplyDanny commented 2 years ago

There is currently no way to print corrected content to a file. The purpose of the --path argument you are using is only to specify input files. It will be ignored when --use-stdin is given in addition. To avoid this and other misunderstandings, the --path options is deprecated in the most recent versions already.

I propose a fix in #4132 for the error you report that makes sure autocorrected STDIN input is printed to STDOUT. That in turn allows you to pipe it into a file like:

cat sample.swift | swiftlint lint --fix --use-stdin > corrected-sample.swift

Would this fix be enough for you?

wasdkhan commented 2 years ago

Would this fix be enough for you?

Thanks, this looks great. I went ahead and wrote a wrapper script (copy sample.swift, grab the configs, fix the copy, output contents, delete copy), but official support is even better!

Hopefully we can use this solution after we update our SwiftLint version.

SimplyDanny commented 2 years ago

The change has been merged and should be included in the next release.