nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.63k stars 623 forks source link

How to exclude SPM packages from SwiftFormat (Question) #1675

Closed aFernandezEspinosa closed 2 months ago

aFernandezEspinosa commented 2 months ago

I'm getting:

Reference to property 'key' in closure requires explicit use of 'self' to make capture semantics explicit

on an SPM Swift file, I can't update or modify that file for obvious reasons as it's not part of my codebase, I was hoping I could exclude all the SPMs from being formatted, however I realized the SPM files are stored in the derived data path.

Any good option to exclude these files from being analyzed by the swiftFormat tool?

nicklockwood commented 2 months ago

You can exclude specific files or paths using the --exclude path/to/files option

aFernandezEspinosa commented 2 months ago

Hmmm yeah the problem with SPM is that the path to those files goes into derived data

/Users/myuser/Library/Developer/Xcode/DerivedData/myApp-cemufujozguknefkeaxcvnlcvwje/SourcePackages/checkouts/spmLibrary
nicklockwood commented 2 months ago

SwiftFormat shouldn't be scanning those files at all unless you are running it at the root of your user folder or something. You should normally only run SwiftFormat inside your project folder.

aFernandezEspinosa commented 2 months ago

I see let me check what's happening it's a very simple piece of code not sure why the compiler is complaining about it.

public var wrappedValue: DataType {
        get {
            do {
                guard let storedData: Data = try container.value(forKey: key),
                      let value: DataType = try? JSONDecoder().decode(DataType.self, from: storedData) else {
                    return fallbackValue
                }
                fallbackValue = value
            } catch let error {
                logger.error("Fetch value for '\(key)' from \(container) failed. \(error.localizedDescription)")
            }
            return fallbackValue
        }
       set  { ... }

Interestingly I have the same package in a different app, and in the other app it works fine, maybe I have some compiler flag configured in a different way 😓

nicklockwood commented 2 months ago

I think the SPM thing is a red herring. My guess is that your logger is using string interpolation so it's not safe to remove self inside logged strings. Add --selfrequired logger.error to your .swiftformat configuration and it should solve the problem.

aFernandezEspinosa commented 2 months ago

Thank you 🙏