SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

No exclude rules are applied for --use-stdin files

Open GusevAndrey opened this issue 3 years ago • 4 comments

New Issue Checklist

Describe the bug

SwiftLint do not exclude a file provided via stdin event with --force-exclude flag.

Looks like no file exclusion is performed for stdin-provided data: Screenshot 2022-03-22 at 12 47 05

It would be nice if there is a way to provide a file-path along with the stdin data to participate in unified file exclusion logic according to config as expected.

Environment

  • SwiftLint version: 0.46.5
  • Installation method: download from releases

GusevAndrey avatar Mar 22 '22 10:03 GusevAndrey

May be using existing --path argument:

     if visitor.useSTDIN {
            if let filePath = visitor.paths.first, !filePath.isEmpty {
                let filteredPaths = visitor.useExcludingByPrefix
                    ? filterExcludedPathsByPrefix(in: [filePath])
                    : filterExcludedPaths(in: [filePath])
                let isExcludedPath = filteredPaths.isEmpty
                if isExcludedPath {
                    return .success([])
                }
            }
            let stdinData = FileHandle.standardInput.readDataToEndOfFile()
            if let stdinString = String(data: stdinData, encoding: .utf8) {
                return .success([SwiftLintFile(contents: stdinString)])
            }
            return .failure(.usageError(description: "stdin isn't a UTF8-encoded string"))
    } else if visitor.useScriptInputFiles {

GusevAndrey avatar Mar 22 '22 12:03 GusevAndrey

I don't understand what you're trying to do.

The --use-stdin does not lint a file, it lints the input read via stdin, so there's no path associated to it.

For example:

$ echo "struct A {}" | swiftlint --use-stdin
<nopath>:1:8: warning: Type Name Violation: Type name should be between 3 and 40 characters long: 'A' (type_name)
Done linting! Found 1 violation, 0 serious in 1 file.

Notice how the violation is logged with <nopath> to refer to its location, because the input has no path.

jpsim avatar Mar 22 '22 15:03 jpsim

@jpsim Hi

Yes, --use-stdin lints the input read via stdin. And I speaking of case stdin provided by reading a file.

To be precise, in my case I have number of files partially (just some lines from file) staged and I need to lint the exact changes being commited (not full working copy). To achieve this I'm using git-format-staged tool recommended by SwiftFormat authors for the same case.

The tool (via low level git) is gathering actual "state to be commited" for each file and provide in via stdin alongside with a file path.

So when I trying to use it like so:

git-format-staged --no-write --formatter "swiftlint lint --force-exclude --use-stdin" "*.swift"

I'm getting linting issues for the files being excluded via swiftlint.yaml.

GusevAndrey avatar Mar 23 '22 05:03 GusevAndrey

I have the exact same use case as mentioned in the previous message (using git-format-staged). Would be really cool to have a solution for this. For example, swiftformat can be given --stdinpath /path/to/file.swift.

oonoo avatar Aug 15 '23 14:08 oonoo