SwiftLint
SwiftLint copied to clipboard
How to exclude files with a certain name? (implement recursive globs)
I want to exclude from linting every file named "R.generated.swift", but I can't figure out the proper exclude entry for this. Pleas help me!:D
New Issue Checklist
- [x] Updated SwiftLint to the latest version
- [x] I searched for existing GitHub issues
Environment
- SwiftLint version (run
swiftlint versionto be sure)? - SwiftLint (0.25.1) (from Podfile.lock) - Installation method used (Homebrew, CocoaPods, building from source, etc)? - CocoaPods
- Paste your configuration file:
excluded: # paths to ignore during linting. Takes precedence over `included`.
- Pods
- MyFeatureKit/iOS/Source/Resources/R.generated.swift # works but very inconvenient
- MyApp/Source/Resources/R.generated.swift # works but very inconvenient
# - */R.generated.swift # Doesn't work
# - */*/R.generated.swift # Doesn't work
# - .*/R.generated.swift # Doesn't work
- Are you using nested configurations? If so, paste their relative paths and respective contents. - NO (My repository only has one .swiftlint.yml at the root of the repository.)
- Which Xcode version are you using (check
xcode-select -p)? - Xcode 9.4.1
This is possible on SwiftLint 0.26. I'm closing this for now, feel free to reopen if you update and can't make it work.
@marcelofabri thanks for the reply! Please reopen the issue! I don't seem to have the rights for it.
Same configuration, but updated to SwiftLint 0.27.
Tried the following entries:
- */R.generated.swift" -> Causes error: Scanner: while scanning an alias in line #, column 7
- **/R.generated.swift" -> Causes error: Scanner: while scanning an alias in line #, column 7
- */*/R.generated.swift" -> Causes error: Scanner: while scanning an alias in line #, column 7
- R.generated.swift -> Generated file is not getting excluded
- /*R.generated.swift -> Generated file is not getting excluded
- .*/R.generated.swift -> Generated file is not getting excluded
- ./*/R.generated.swift -> Generated file is not getting excluded
- ./*/*/*/*/R.generated.swift - Works but this requires knowledge about the file hierarchy! Source of different build targets may contain the generated source files at a different level of folder hierarchy.
The exclusion entry I am looking for is excepted to work with the same logic as the following .gitignore entry:
R.generated.swift
Also if you could provide an example with the logical equivalent of the gitignore line below, that would be an even better, more generic solution.
*.generated.swift
Please provide some exclusion examples about this usecase.
@ZsoltMolnarrr the errors you're getting is because you need to escape the string on yaml:
excluded:
- "*.generated.swift"
However, I think what you want is not currently supported because it'd need recursive globs. Let's keep this open as an enhancement.
@marcelofabri You're solution above is not ignoring my R.generated.swift file. Would this be related to my .swiftlint.yml being in a child folder, relative to R.generated.swift?
Any chance of bumping this back up? I can't do any of the following:
excluded:
- "**/*/_*.swift"
- "**/*/*.generated.swift"
- "_*.swift"
- "*.generated.swift"
My current workaround is just moving them into other folders, but I'd like to keep some generated files alongside their counterparts. Similarly, we use another tool that uses the _ prefix for generated files, so we want to account for both.
@marcelofabri Any thoughts on how to work around this?
Same issue
PRs are welcome 😊
@marcelofabri Any thoughts on how to work around this?
we work rounded by generating list of files ourselves and then passing list to SwiftLint instead of just path to dir to scan
Any updates for this issue?
This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!
Any update on this issue?
This issue has been automatically marked as stale because it has not had any recent activity. Please comment to prevent this issue from being closed. Thank you for your contributions!
Any update?
I think this issue is fixed already. This config works on my side:
excluded:
- "**/*/R.generated.swift"
I'm using SwiftLint 0.42.0. The project I'm working on separate R.generated.swift file in multiple sub-projects
I'm on 0.43.1 and I don't think this works yet. **/*/something not only fails to match all the "*something", but it's also the wrong glob syntax. The right syntax should be "**/something" and this one also doesn't work
Why is this marked as "Won't Fix" when clearly many people and expressing the need for this?
Because it was a bot that closed it and no one came here to help us reopen it 😞
still does not work properly
I believe I have found a solution, at least one that worked for me:
TLDR:
add
- builds
to your list of exclusions ALONG with the path you are already using (example at the very end of the post)
Cause of the issue:
So,
excluded:
- "*.generated.swift"
does work, in my case it cut the errors and warnings to about half. Why half? because it did ignore the generated file, however, there was another generated file being generated at runtime then moved immediately after, (guessing that's just how RSwift does thing). To get the exact path of the "intermediary generated file" do this: 1- build app 2- click on a warning/error for the R.generated.swift 3- on the top left corner, you will notice "R.generated.swift" without a previous path 4- right click on to see where it originated, it will show the path vertically, it's gonna be something like: R.generated.swift <Path> project-<number, probably 0> <number, probably 0> builds
The entire "builds" folder is generated and removed on build, but during that time it detects both of them and thus produces the warning
to fix this issue, you can add this to your yml file:
excluded:
- "R.generated.swift"
- builds
Recursive glob still doesn't work. Use case:
Folder structure:
.
├── Pods
└── another_directory
├── Pods
└── somefile.txt
How can I ignore both Pods directories, considering that one is nested, the other is at the top level? In other words, how can I ignore all directories called Pods regardless of where they are?
I've tried these, and none of them work (individually):
- Pods // Catches only the directory at the top level
- **/Pods // Doesn't work
- */**/Pods // Only catches Pods directory nested on the 3rd level
- .Pods // Doesn't work
- /Pods // Doesn't work
- */Pods // Only catches Pods directory nested on the 2nd level
Long story short it seems like I have to know beforehand how many levels nested my folders are going to be to add them explicitly, e.g.:
- Pods // level 0
- */Pods // level 1
- */**/Pods // level 2
- */**/**/Pods // level 3
- */**/**/**/Pods // level 4
…
Whereas with glob, this should work for all levels: **/Pods
Could someone reopen this issue? 🙏
Still broken 🙁
WHERE IS THE SOLUTION???
This is possible on SwiftLint 0.26. I'm closing this for now, feel free to reopen if you update and can't make it work.
@marcelofabri Any example?
This works with "**/*generated.swift".
For the directory tree like this, all *.generated.swift has been excluded.
── App
│ └── AppTests
│ ├── Mocks
│ │ ├── A.swift
│ │ ├── B.swift
│ │ ├── C.swift
│ │ ├── AAA.generated.swift
│ │ ├── Generated
│ │ │ ├── A.generated.swift
│ │ │ └── B.generated.swift
I am using Version 0.49.1
I tried "**/*generated.swift as suggested above but it does seem to match my file located at Projects/MyFeature/Sources/GraphQL/API.generated.swift
In fact, even if I specify the file explicitly, it is still linted/corrected
excluded:
- "Projects/MyFeature/Sources/GraphQL/API.generated.swift"
I'm using version 0.50.3
Am I doing wrong in my config?
Version 0.50.3, this leads to a Bus error:
Linting Swift files in current working directory
Bus error: 10
Config:
included:
- Sources
excluded:
- "**/*.graphql.swift"
LLDB:
frame #5: 0x0000000100152e28 swiftlint`static SwiftLintFramework.Glob.expandGlobstar(pattern: Swift.String) -> Swift.Array<Swift.String> + 604
swiftlint`static SwiftLintFramework.Glob.expandGlobstar(pattern: Swift.String) -> Swift.Array<Swift.String>:
frame #4: 0x000000010015381c swiftlint`function signature specialization <Arg[1] = Owned To Guaranteed, Arg[2] = Dead> of function signature specialization <Arg[0] = [Closure Propagated : closure #1 (Swift.String) -> Swift.Optional<Swift.String> in static SwiftLintFramework.Glob.(expandGlobstar in _A29680EDB2A67C45FA4E37C1A6FCC33B)(pattern: Swift.String) -> Swift.Array<Swift.String>, Argument Types : [Swift.String@thin SwiftLintFramework.Glob.Type]> of generic specialization <Swift.Array<Swift.String>, Swift.String> of Swift.Sequence.compactMap<τ_0_0>((τ_0_0.Element) throws -> Swift.Optional<τ_1_0>) throws -> Swift.Array<τ_1_0> + 308
swiftlint`function signature specialization <Arg[1] = Owned To Guaranteed, Arg[2] = Dead> of function signature specialization <Arg[0] = [Closure Propagated : closure #1 (Swift.String) -> Swift.Optional<Swift.String> in static SwiftLintFramework.Glob.(expandGlobstar in _A29680EDB2A67C45FA4E37C1A6FCC33B)(pattern: Swift.String) -> Swift.Array<Swift.String>, Argument Types : [Swift.String@thin SwiftLintFramework.Glob.Type]> of generic specialization <Swift.Array<Swift.String>, Swift.String> of Swift.Sequence.compactMap<τ_0_0>((τ_0_0.Element) throws -> Swift.Optional<τ_1_0>) throws -> Swift.Array<τ_1_0>:
frame #3: 0x00000001993535ec Foundation`-[NSFileManager fileExistsAtPath:isDirectory:] + 56
Foundation`-[NSFileManager fileExistsAtPath:isDirectory:]:
frame #2: 0x000000019932e9d4 Foundation`-[NSFileManager getFileSystemRepresentation:maxLength:withPath:] + 52
Foundation`-[NSFileManager getFileSystemRepresentation:maxLength:withPath:]:
frame #1: 0x00000001993300d0 Foundation`-[NSPathStore2 isEqualToString:] + 272
Foundation`-[NSPathStore2 isEqualToString:]:
frame #0: 0x000000019841325c CoreFoundation`-[__NSCFString getCharacters:range:] + 4
CoreFoundation`-[__NSCFString getCharacters:range:]:
Frame 5 - infinity is just a repeated Glob.expandGlobstar. Probably bad logic at https://github.com/realm/SwiftLint/blob/da27f1c7fdf2d1bc930287be89420998159a831f/Source/SwiftLintFramework/Helpers/Glob.swift#L82
So this all appears to work as expected for me with 0.50.3 and also 0.51.0, on Monterey, with Xcode 14.2
% find .
.
./t.yml
./A
./A/A
./A/A/X.graphql.swift
./A/X.graphql.swift
./X.graphql.swift
./B
./B/Foo.swift
%
%
% cat t.yml
included:
- A
- B
excluded:
- "**/*.graphql.swift"
%
%
% /opt/homebrew/bin/swiftlint --config t.yml
Linting Swift files in current working directory
Linting 'Foo.swift' (1/1)
Done linting! Found 0 violations, 0 serious in 1 file.
Similarly with directories
% find .
.
./t.yml
./A
./A/AFoo.swift
./A/B
./A/B/ABFoo.swift
./B
./B/BFoo.swift
%
%
% cat t.yml
included:
- A
- B
excluded:
- "**/B"
%
%
% /opt/homebrew/bin/swiftlint --config t.yml
Linting Swift files in current working directory
Linting 'AFoo.swift' (1/1)
Done linting! Found 0 violations, 0 serious in 1 file.
I have multiple files called "Something" inside different child folders on different levels of the hierarchy, and this works for me:
excluded:
- '**/something'
I'm using SwiftLint 0.54.0.