swiftlang / swift-package-manager

The Package Manager for the Swift Programming Language
Apache License 2.0
9.76k stars 1.35k forks source link

bug: integration via swift plugin does not seem to work #8040

Open mman opened 1 month ago

mman commented 1 month ago

I would like to lint my code during the build process.

Not sure if I'm doing anything wrongly, since the plugin integration seems to be undocumented, but using this simple Package.swift:

// swift-tools-version:6.0
import PackageDescription

let package = Package(
    name: "swift-mbedtls",
    platforms: [
        .iOS(.v12), .macOS(.v10_15), .tvOS(.v12), .watchOS(.v6), .visionOS(.v1)
    ],
    products: [
        .library(name: "SWMBEDTLS", targets: ["SWMBEDTLS", "mbedtls"])
    ],
    dependencies: [
        .package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
        .package(url: "https://github.com/swiftlang/swift-testing.git", branch: "release/6.0"),
        .package(url: "https://github.com/apple/swift-format.git", from: "600.0.0"),
    ],
    targets: [
        .target(name: "mbedtls", dependencies: [
            .product(name: "Logging", package: "swift-log"),
        ], exclude: ["3rdparty", "framework", "programs", "tests"], cSettings: []),
        .target(name: "SWMBEDTLS", dependencies: ["mbedtls"], plugins: [
            .plugin(name: "LintPlugin", package: "swift-format")]),
        .testTarget(name: "SWMBEDTLSTests", dependencies: [
            "SWMBEDTLS",
            .product(name: "Logging", package: "swift-log"),
            .product(name: "Testing", package: "swift-testing"),
        ])
    ]
)

will result in the following error while running swift build.

 € swift build 
warning: 'swift-format': found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
    /Users/mman/Developer/swift-mbedtls/.build/checkouts/swift-format/Sources/swift-format/CMakeLists.txt
warning: 'swift-format': found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
    /Users/mman/Developer/swift-mbedtls/.build/checkouts/swift-format/Sources/SwiftFormat/CMakeLists.txt
error: the library 'SWMBEDTLS' requires macos 10.15, but depends on the product 'LintPlugin' which requires macos 12.0; consider changing the library 'SWMBEDTLS' to require macos 12.0 or later, or the product 'LintPlugin' to require macos 10.15 or earlier.

Problem 1. swift-format is looking at files it should ignore and there is no way to override

Seeing the problem with deployment target, I change the platforms to .macOS(.v12) and try again:

€ swift build      
warning: 'swift-format': found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
    /Users/mman/Developer/swift-mbedtls/.build/checkouts/swift-format/Sources/swift-format/CMakeLists.txt
warning: 'swift-format': found 1 file(s) which are unhandled; explicitly declare them as resources or exclude from the target
    /Users/mman/Developer/swift-mbedtls/.build/checkouts/swift-format/Sources/SwiftFormat/CMakeLists.txt
Building for debugging...
[459/459] Applying swift-format-tool
Build of product 'swift-format' complete! (25.72s)
error: Plugin is declared with the `buildTool` capability, but doesn't conform to the `BuildToolPlugin` protocol
error: build stopped due to build-tool plugin failures

Problem 2. swift-format integrated via plugin just does not work.

Is this expected? Anything I can do about it / contribute code?

mman commented 1 month ago

Just to add, trying to build from Xcode 16 produces the following error:

internalError("invalid plugin capability command(intent: PackageModel.PluginCommandIntent.custom(verb: \"lint-source-code\", description: \"Lint source code for a specified target.\"), permissions: [])")
ahoppen commented 1 month ago

Synced to Apple’s issue tracker as rdar://137587533

ahoppen commented 1 month ago

The warning about CMakeLists.txt should be fixed on main and thus in the next swift-format release.

error: Plugin is declared with the `buildTool` capability, but doesn't conform to the `BuildToolPlugin` protocol

is very surprising because LintPlugin is declared as a command plugin, not as a build plugin. Sending the issue to SwiftPM to look at this.