realm / SwiftLint

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

Nested configs ignored when running swiftlint from bash function #736

Closed mgrebenets closed 7 years ago

mgrebenets commented 8 years ago

I have a top-level swiftlint configuration in my home directory ~/.swiftlint.yml

disabled_rules:
  - nesting

Then I have another .swiflint.yml in the working directory, e.g. in ~/example:

disabled_rules:
  - variable_name
  - force_cast

Then the test.swift file:

let i: Int = 0
print(i as! Int)

So no I run swiftlint lint in the directory with the test file. All works good, no violations.

Next I create a run.sh script with the following contents:

#!/bin/bash
SWIFT_LINT=/usr/local/bin/swiftlint
${SWIFT_LINT} rules
${SWIFT_LINT} lint --path "$1"

So then I chmod +x run.sh and run it ./run.sh. All good, no violations.

Finally, I use the bash function like so in the run.sh:

SWIFT_LINT=/usr/local/bin/swiftlint

run_swiftlint() {
    echo "pwd: $(pwd)"
    ${SWIFT_LINT} rules
    ${SWIFT_LINT} lint --path "$1"
}

run_swiftlint "test.swift"

And this time if fails with 2 violations.

Loading configuration from '.swiftlint.yml'
Linting Swift files at path test.swift
Linting 'test.swift' (1/1)
/Users/maksym/Projects/tmp/swiftlint-issue/test.swift:4:9: error: Force Cast Violation: Force casts should be avoided. (force_cast)
/Users/maksym/Projects/tmp/swiftlint-issue/test.swift:3:1: error: Variable Name Violation: Variable name should be between 3 and 40 characters long: 'i' (variable_name)
Done linting! Found 2 violations, 2 serious in 1 file.

Somehow the .swiftlint.yml in current directory is ignored when running from shell function.

If I remove the ~/.swiftlint.yml the violations will no longer be reported.

I have tried to do ${SWIFT_LINT} lint --config $(pwd)/.swiftlint.yml --path "$1" and it didn't change anything

Here's the complete output: (you can see that force_cast is marked as "not enabled in config" but is still reported)

pwd: /Users/maksym/Projects/tmp/swiftlint-issue
Loading configuration from '.swiftlint.yml'
+-----------------------------+--------+-------------+------------------------+--------------------------------------------------+
| identifier                  | opt-in | correctable | enabled in your config | configuration                                    |
+-----------------------------+--------+-------------+------------------------+--------------------------------------------------+
| closing_brace               | no     | yes         | yes                    | warning                                          |
| colon                       | no     | yes         | yes                    | warning                                          |
| comma                       | no     | yes         | yes                    | warning                                          |
| conditional_binding_cascade | no     | no          | yes                    | warning                                          |
| control_statement           | no     | no          | yes                    | warning                                          |
| custom_rules                | no     | no          | yes                    | user-defined                                     |
| cyclomatic_complexity       | no     | no          | yes                    | warning: 10, error: 20                           |
| empty_count                 | yes    | no          | no                     | error                                            |
| file_length                 | no     | no          | yes                    | warning: 400, error: 1000                        |
| force_cast                  | no     | no          | no                     | error                                            |
| force_try                   | no     | no          | yes                    | error                                            |
| force_unwrapping            | yes    | no          | no                     | warning                                          |
| function_body_length        | no     | no          | yes                    | warning: 40, error: 100                          |
| function_parameter_count    | no     | no          | yes                    | warning: 5, error: 8                             |
| leading_whitespace          | no     | no          | yes                    | warning                                          |
| legacy_cggeometry_functions | no     | yes         | yes                    | warning                                          |
| legacy_constant             | no     | yes         | yes                    | warning                                          |
| legacy_constructor          | no     | yes         | yes                    | warning                                          |
| line_length                 | no     | no          | yes                    | warning: 100, error: 200                         |
| missing_docs                | yes    | no          | no                     | warning: public                                  |
| nesting                     | no     | no          | yes                    | warning                                          |
| opening_brace               | no     | yes         | yes                    | warning                                          |
| operator_whitespace         | no     | no          | yes                    | warning                                          |
| return_arrow_whitespace     | no     | yes         | yes                    | warning                                          |
| statement_position          | no     | yes         | yes                    | (statement_mode) default, (severity) warning     |
| todo                        | no     | no          | yes                    | warning                                          |
| trailing_newline            | no     | yes         | yes                    | warning                                          |
| trailing_semicolon          | no     | yes         | yes                    | warning                                          |
| trailing_whitespace         | no     | yes         | yes                    | warning, ignores_empty_lines: false              |
| type_body_length            | no     | no          | yes                    | warning: 200, error: 350                         |
| type_name                   | no     | no          | yes                    | (min_length) w/e: 3/0, (max_length) w/e: 40/1000 |
| valid_docs                  | no     | no          | yes                    | warning                                          |
| variable_name               | no     | no          | no                     | (min_length) w/e: 3/2, (max_length) w/e: 40/60   |
+-----------------------------+--------+-------------+------------------------+--------------------------------------------------+
Loading configuration from '.swiftlint.yml'
Linting Swift files at path test.swift
Linting 'test.swift' (1/1)
/Users/maksym/Projects/tmp/swiftlint-issue/test.swift:4:9: error: Force Cast Violation: Force casts should be avoided. (force_cast)
/Users/maksym/Projects/tmp/swiftlint-issue/test.swift:3:1: error: Variable Name Violation: Variable name should be between 3 and 40 characters long: 'i' (variable_name)
Done linting! Found 2 violations, 2 serious in 1 file.

This may be related to https://github.com/realm/SwiftLint/issues/554, https://github.com/realm/SwiftLint/issues/550.

Attached the sample setup (does not include the ~/.swiftlint.yml): swiftlint-issue.zip

jpsim commented 8 years ago

Thanks for filing this issue, @mgrebenets! I'm a bit busy at the moment, but hopefully someone else can help you (yay open source?), otherwise I'll get back to this eventually.

marcelofabri commented 7 years ago

@mgrebenets Could you please check if this is still valid with 0.20.1? We made some changes in this area and this might have been fixed too.

marcelofabri commented 7 years ago

This looks fixed in the latest version:

$ swiftlint
Loading configuration from '.swiftlint.yml'
'variable_name' rule has been renamed to 'identifier_name' and will be completely removed in a future release.
Linting Swift files in current working directory
Linting 'test.swift' (1/1)
Done linting! Found 0 violations, 0 serious in 1 file.

$ cat ../.swiftlint.yml 
disabled_rules:
  - nesting

Feel free to reopen if it's not the case.