SwiftLint
SwiftLint copied to clipboard
Unable to set `type_body_length` `function_body_length` `file_length` only as warnings with no error.
New Issue Checklist
- [X] I've Updated SwiftLint to the latest version.
- [X] I've searched for existing GitHub issues.
Bug Description
I'm trying to set type_body_length function_body_length file_length to be warning only with custom length. I want to have a warning if the length reaches a certain limit, but no errors.
This is not clearly documented, but should be available #1647 #1691
I'm testing this by running:
swiftlint --quiet | grep -E 'error.*(type_body_length|function_body_length|file_length)'
And I would expect no errors in the output, but I do get errors using the default error limit of the respective rule.
I tried the following variations:
type_body_length: 400
type_body_length:
warning: 400
type_body_length:
- 400
type_body_length:
warning: 400
error: null
type_body_length:
warning: 400
error: nil
type_body_length:
warning: 400
error: ~
but none is working. It either gets the defults value of the rule:
error: Type Body Length Violation: Type body should span 500 lines or less excluding comments and whitespace: currently spans 528 lines (type_body_length)
or gives me:
warning: Invalid configuration for 'type_body_length' rule. Falling back to default.
Environment
- SwiftLint version: 0.57.0
- Xcode version: 16.0
- Installation method used: Homebrew
- Configuration file:
only_rules:
- superfluous_disable_command
- type_body_length
- function_body_length
- line_length
- file_length
# auto-correctable rules:
- leading_whitespace
- trailing_whitespace
- trailing_comma
- trailing_newline
- colon
- trailing_semicolon
- opening_brace
- closing_brace
- return_arrow_whitespace
- comma
- empty_enum_arguments
- empty_parameters
- void_return
excluded:
- fastlane/.tempbuild
- Package/**/*/PreviewAssets.swift
- Package/.build
type_body_length: 400
function_body_length: 91
file_length: 610
line_length:
warning: 400
error: 500
Can you provide a small reproducer? For me, this works. The configurations
type_body_length:
warning: 40
type_body_length:
warning: 40
error: 50
type_body_length: 40
type_body_length: [40, 50]
work as I'd expect.
Can't reproduce either outside of the main project, the same file that fails on the main project does't if isolated. I also noticed that it doesn't happen on our CI.
I wonder if it's due to some caching or derived data issue? It's a very large project.
Anyways I think the issue is solved, as it doesn't happen on CI where we enforce the linting. Do you have any insights on why this might happen locally and how to avoid it? Just to leave some pointers to other folks having similar issues.
Thanks @SimplyDanny!
How do you call SwiftLint in all these cases? Command line, plugin, Bazel, ...?
Can it be that the configuration isn't read at all in your main project? Or are other options considered?
When testing locally I just run the above command on cli, which doesn't work as expected for some reason.
On CI we use GitHub actions with Danger, which works as expected.
In both cases I'm sure the config is read.
Something must be different though. Hard to believe, that the same tool version with the same configuration and code base behaves differently. Did you try running SwiftLint from the command line in CI as well?
Without even getting CI involved, all I have to do is copy the same file + config in a new folder and it will work, while it wouldn't on the main project. I wonder if SwiftLint takes advantage of Xcode derived data or some other caching that could explain this?
Without even getting CI involved, all I have to do is copy the same file + config in a new folder and it will work, while it wouldn't on the main project. I wonder if SwiftLint takes advantage of Xcode derived data or some other caching that could explain this?
It shouldn't as it builds its own cache. However, caching is a good point. Try running the command with the --no-cache argument.
I found the culprit: we had a duplicated and outdated config file in a subfolder of a test target. Apparently SwiftLint from the cli was picking that up instead than the main one. Since the limits were the same of the default config, I assumed it was picking up the default config instead.
This explains also why on CI it was working, since there we explicitly set the path for the config.
I'll close this, thanks for your help and insights @SimplyDanny