SwiftLint icon indicating copy to clipboard operation
SwiftLint copied to clipboard

How to exclude files with a certain name? (implement recursive globs)

Open ZsoltMolnarrr opened this issue 7 years ago • 30 comments

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

Environment

  • SwiftLint version (run swiftlint version to 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

ZsoltMolnarrr avatar Aug 03 '18 09:08 ZsoltMolnarrr

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 avatar Aug 03 '18 16:08 marcelofabri

@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 avatar Aug 09 '18 11:08 ZsoltMolnarrr

@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 avatar Aug 09 '18 14:08 marcelofabri

@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?

GregHilston avatar May 02 '19 15:05 GregHilston

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.

bdrelling avatar May 31 '19 20:05 bdrelling

@marcelofabri Any thoughts on how to work around this?

bdrelling avatar Nov 30 '19 01:11 bdrelling

Same issue

pgorzelany-objectivity avatar Dec 05 '19 18:12 pgorzelany-objectivity

PRs are welcome 😊

marcelofabri avatar Dec 05 '19 19:12 marcelofabri

@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

RolandasRazma avatar Dec 10 '19 22:12 RolandasRazma

Any updates for this issue?

kuyazee avatar Jul 11 '20 13:07 kuyazee

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!

stale[bot] avatar Nov 08 '20 02:11 stale[bot]

Any update on this issue?

dantes-git avatar Nov 09 '20 14:11 dantes-git

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!

stale[bot] avatar Jan 08 '21 14:01 stale[bot]

Any update?

jbdtky avatar Jan 25 '21 12:01 jbdtky

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

nahung89 avatar Jan 31 '21 03:01 nahung89

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

rogerluan avatar Jun 23 '21 03:06 rogerluan

Why is this marked as "Won't Fix" when clearly many people and expressing the need for this?

cameroncooke avatar Jul 01 '21 15:07 cameroncooke

Because it was a bot that closed it and no one came here to help us reopen it 😞

rogerluan avatar Jul 01 '21 21:07 rogerluan

still does not work properly

niorko avatar Oct 01 '21 08:10 niorko

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

mostafaelshazly9 avatar Nov 08 '21 08:11 mostafaelshazly9

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

This tool helps:

image

Could someone reopen this issue? 🙏

rogerluan avatar Nov 19 '21 13:11 rogerluan

Still broken 🙁

timwredwards avatar May 10 '22 10:05 timwredwards

WHERE IS THE SOLUTION???

hlung avatar Jun 14 '22 06:06 hlung

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?

hlung avatar Jun 14 '22 06:06 hlung

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

jkmathew avatar Nov 28 '22 18:11 jkmathew

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?

JanC avatar Dec 21 '22 18:12 JanC

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

lhunath avatar Mar 28 '23 19:03 lhunath

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.

mildm8nnered avatar Mar 28 '23 21:03 mildm8nnered

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.

mildm8nnered avatar Mar 28 '23 22:03 mildm8nnered

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.

lazarevzubov avatar Feb 07 '24 18:02 lazarevzubov