realm / SwiftLint

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

SwiftLint configuration ignored during Xcode build? #5608

Closed JozefLipovsky closed 2 weeks ago

JozefLipovsky commented 1 month ago

New Issue Checklist

Describe the bug

I have a simple modular project (public sample project repo) configured with a package description like this:

let package = Package(
    name: "Modules",
    platforms: [
        .iOS(.v17),
        .tvOS(.v17)
    ],
    products: [
        .targetLibrary("Feature")
    ],
    dependencies: [
        .package(url: "https://github.com/realm/SwiftLint.git", from: "0.54.0")
    ],
    targets: [
        .target(name: "Feature"),
        .testTarget(name: "FeatureTests")
    ]
)

extension Product {
    static func targetLibrary(_ name: String) -> Product {
        .library(name: name, targets: [name])
    }
}

// Inject base plugins into each target
package.targets = package.targets.map { target in
    var plugins = target.plugins ?? []
    plugins.append(.plugin(name: "SwiftLintBuildToolPlugin", package: "swiftlint"))
    target.plugins = plugins
    return target
}

And I have a .swiftlint.yml file for custom linting rules:

excluded:
  - Packages/Modules/.build/checkouts/

disabled_rules:
  - force_cast
#   - nesting

nesting:
  type_level:
    warning: 2
    error: 3

type_name:
  allowed_symbols: "_"

I first noticed this on the nesting type_level rule warnings but I also added force_cast and column violations in my example:

public struct Feature {
    let someForceCast = NSObject() as! Int
    let colonOnWrongSide :Int = 0
    public init() {}
}

public extension Feature {
    enum NestedEnum {
        public enum ViewAction {}
        public enum DelegateAction {}

        case view(ViewAction)
        case delegate(DelegateAction)
    }
}

When I run swiftlint lint from the command line, the linter applies my configurations correctly, and I get the expected results (based on disabled_rules and nesting rule config):

Linting Swift files in current working directory
Linting 'LintTest_tvOS.swift' (1/5)
Linting 'LintTest_iOS.swift' (2/5)
Linting 'FeatureTests.swift' (3/5)
Linting 'Package.swift' (4/5)
Linting 'Feature.swift' (5/5)
/Users/jl/Developer/LintTest/Packages/Modules/Sources/Feature/Feature.swift:7:25: warning: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)
Done linting! Found 1 violation, 0 serious in 5 files.

However, when I build the project using Xcode, the linting rules specified in the .swiftlint.yml file seem to be ignored. The same setup was working fine before. I am not sure if it could be Xcode or SwiftLint package version update. Any ideas what am I missing here, please?

image

Xcode logs:

Showing All Messages

Prepare packages

/Applications/Xcode-15.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -L /Applications/Xcode-15.4.0.app/Contents/SharedFrameworks/SwiftPM.framework/SharedSupport/PluginAPI -lPackagePlugin -Xlinker -rpath -Xlinker /Applications/Xcode-15.4.0.app/Contents/SharedFrameworks/SwiftPM.framework/SharedSupport/PluginAPI -target arm64-apple-macos13.0 -sdk /Applications/Xcode-15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -g -swift-version 5 -package-description-version 5.9.0 -I /Applications/Xcode-15.4.0.app/Contents/SharedFrameworks/SwiftPM.framework/SharedSupport/PluginAPI -sdk /Applications/Xcode-15.4.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.5.sdk -parse-as-library -Xfrontend -serialize-diagnostics-path -Xfrontend /Users/jl/Library/Developer/Xcode/DerivedData/LintTest-ecgtlurtvgckfcfjesaljjjfqkcv/SourcePackages/plugins/SwiftLintBuildToolPlugin.dia /Users/jl/Library/Developer/Xcode/DerivedData/LintTest-ecgtlurtvgckfcfjesaljjjfqkcv/SourcePackages/checkouts/SwiftLint/Plugins/SwiftLintBuildToolPlugin/Path+Helpers.swift /Users/jl/Library/Developer/Xcode/DerivedData/LintTest-ecgtlurtvgckfcfjesaljjjfqkcv/SourcePackages/checkouts/SwiftLint/Plugins/SwiftLintBuildToolPlugin/SwiftLintBuildToolPlugin.swift /Users/jl/Library/Developer/Xcode/DerivedData/LintTest-ecgtlurtvgckfcfjesaljjjfqkcv/SourcePackages/checkouts/SwiftLint/Plugins/SwiftLintBuildToolPlugin/SwiftLintBuildToolPluginError.swift -o /Users/jl/Library/Developer/Xcode/DerivedData/LintTest-ecgtlurtvgckfcfjesaljjjfqkcv/SourcePackages/plugins/SwiftLintBuildToolPlugin

[debug]: Compiling plugin to executable at /Users/jl/Library/Developer/Xcode/DerivedData/LintTest-ecgtlurtvgckfcfjesaljjjfqkcv/SourcePackages/plugins/SwiftLintBuildToolPlugin
[debug]: Using compiler /Applications/Xcode-15.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc
[debug]: Plugin compilation output directory '/Users/jl/Library/Developer/Xcode/DerivedData/LintTest-ecgtlurtvgckfcfjesaljjjfqkcv/SourcePackages/plugins'
[debug]: Computed hash of plugin compilation inputs: 25ed944651b1d62a505230f05bca8bff7b0e998afede9fd5fd35b474f36f0fad

working directory:
   /Users/jl/Developer/LintTest/Packages/Modules
tool mapping:
   swiftlint: /Users/jl/Library/Developer/Xcode/DerivedData/LintTest-ecgtlurtvgckfcfjesaljjjfqkcv/SourcePackages/artifacts/swiftlint/SwiftLintBinary/SwiftLintBinary.artifactbundle/swiftlint-0.55.1-macos/bin/swiftlint [arm64-apple-macosx]
tool search paths:
   /Applications/Xcode-15.4.0.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
   /Applications/Xcode-15.4.0.app/Contents/Developer/usr/bin
   /bin
   /sbin
   /usr/bin
   /usr/sbin

Environment: ["BUILD_WORKSPACE_DIRECTORY": "/Users/jl/Developer/LintTest/Packages/Modules"]

Environment: ["BUILD_WORKSPACE_DIRECTORY": "/Users/jl/Developer/LintTest/Packages/Modules"]

/usr/bin/sandbox-exec -p "(version 1)
(deny default)
(import \"system.sb\")
(allow file-read*)
(allow process*)
(allow mach-lookup (global-name \"com.apple.lsd.mapdb\"))
(allow file-write*
    (subpath \"/private/tmp\")
    (subpath \"/private/var/folders/fr/2rpw_r4d6xg96b2zbp8354540000gn/T\")
)
(deny file-write*
    (subpath \"/Users/jl/Developer/LintTest/Packages/Modules\")
)
(allow file-write*
    (subpath \"/Users/jl/Library/Developer/Xcode/DerivedData/LintTest-ecgtlurtvgckfcfjesaljjjfqkcv/SourcePackages/plugins/modules.output/Feature/SwiftLintBuildToolPlugin\")
    (subpath \"/private/var/folders/fr/2rpw_r4d6xg96b2zbp8354540000gn/T/TemporaryItems\")
    (subpath \"/private/var/folders/fr/2rpw_r4d6xg96b2zbp8354540000gn/T/TemporaryItems/NSIRD_Xcode_RvTDu5\")
)
" /Users/jl/Library/Developer/Xcode/DerivedData/LintTest-ecgtlurtvgckfcfjesaljjjfqkcv/SourcePackages/artifacts/swiftlint/SwiftLintBinary/SwiftLintBinary.artifactbundle/swiftlint-0.55.1-macos/bin/swiftlint lint --quiet --force-exclude --cache-path /Users/jl/Library/Developer/Xcode/DerivedData/LintTest-ecgtlurtvgckfcfjesaljjjfqkcv/SourcePackages/plugins/modules.output/Feature/SwiftLintBuildToolPlugin/Cache /Users/jl/Developer/LintTest/Packages/Modules/Sources/Feature/Feature.swift

/Users/jl/Developer/LintTest/Packages/Modules/Sources/Feature/Feature.swift:7:25: warning: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)
/Users/jl/Developer/LintTest/Packages/Modules/Sources/Feature/Feature.swift:6:36: error: Force Cast Violation: Force casts should be avoided (force_cast)
/Users/jl/Developer/LintTest/Packages/Modules/Sources/Feature/Feature.swift:14:16: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)
/Users/jl/Developer/LintTest/Packages/Modules/Sources/Feature/Feature.swift:15:16: warning: Nesting Violation: Types should be nested at most 1 level deep (nesting)

/Users/jl/Developer/LintTest/Packages/Modules/Sources/Feature/Feature.swift:7:25: Colon Spacing Violation: Colons should be next to the identifier when specifying a type and next to the key in dictionary literals (colon)

/Users/jl/Developer/LintTest/Packages/Modules/Sources/Feature/Feature.swift:6:36: Force Cast Violation: Force casts should be avoided (force_cast)

/Users/jl/Developer/LintTest/Packages/Modules/Sources/Feature/Feature.swift:14:16: Nesting Violation: Types should be nested at most 1 level deep (nesting)

/Users/jl/Developer/LintTest/Packages/Modules/Sources/Feature/Feature.swift:15:16: Nesting Violation: Types should be nested at most 1 level deep (nesting)

Plug-in ended with non-zero exit code: 2)

Build stopped    2024-05-26, 3:38 PM    0.3 seconds

Environment

SimplyDanny commented 3 weeks ago

Where is your .swiftlint.yml located in your folder hierarchy? Where do the Swift files reside?

JozefLipovsky commented 2 weeks ago

.swiftlint.yml is in the root folder /LintTest , and swift files are in separate modules sub-folder(s) LintTest/Packages/Modules/Sources/Feature/ . Here is a sample test repo.

338753650-41feb1f7-b00a-4295-89be-01f7c75fbc61
SimplyDanny commented 2 weeks ago

I don't recall the exact reason, but configuration files outside of your working directory cannot be accessed. What works, however, is to place another .swiftlint.yml file with the content

parent_config: ../../.swiftlint.yml

next to your Package.swift.

JozefLipovsky commented 2 weeks ago

Perfect, this worked https://github.com/JozefLipovsky/LintTest/commit/24507719900b8e334d00b1d77aa54ff0dcde45cf & https://github.com/JozefLipovsky/LintTest/commit/adf55c04f447ff6fb02848dda971a6882ae5a2db. Thank you so much @SimplyDanny 👍