swiftlang / swift-package-manager

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

Command plugin can't run from CLI #6804

Closed MahdiBM closed 1 year ago

MahdiBM commented 1 year ago

Description

As some bullet points:

Expected behavior

The list command should show the command plugin and the command plugin should run.

Actual behavior

As described in Description.

Steps to reproduce

Use https://github.com/vapor/penny-bot/releases/tag/spm-issue-6804. Do as described in Description.

Swift Package Manager version/commit hash

Swift Package Manager - Swift 5.9.0

Swift & OS version (output of swift --version ; uname -a)

swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.2 clang-1500.0.40.1) Target: arm64-apple-macosx14.0 Darwin Macbook.local 23.0.0 Darwin Kernel Version 23.0.0: Tue Aug 1 03:25:51 PDT 2023; root:xnu-10002.0.242.0.6~31/RELEASE_ARM64_T6000 arm64

neonichu commented 1 year ago

The error looks like a compilation error while building argument parser, not sure why that would happen. It seems to work fine for me using

swift package plugin --allow-writing-to-package-directory generate-code-from-openapi GitHubAPI
[...]
- ✅ OpenAPI code generation for target 'GitHubAPI' successfully completed.

swift package plugin --list should be fixed, though.

neonichu commented 1 year ago

Not sure yet why it doesn't contain the OpenAPI plugin, but this already seems wrong? Only plugin products should be available, AFAIK.

static func availableCommandPlugins(in graph: PackageGraph) -> [PluginTarget] {
        graph.allTargets.compactMap { $0.underlyingTarget as? PluginTarget }
    }
MahdiBM commented 1 year ago

The error looks like a compilation error while building argument parser

Yeah it does look very much like a compilation error. The weird thing to me was that Xcode didn't complain about it and successfully ran the plugin.

neonichu commented 1 year ago

Ooooh, took me a moment to realize, but the bug is in https://github.com/apple/swift-package-manager/blob/main/Sources/Commands/PackageTools/PluginCommand.swift#L85:

for plugin in allPlugins.sorted(by: { $0.name < $1.name }) {
    guard case .command(let intent, _) = plugin.capability else { return }

So the list stops at the first non-command plugin.

neonichu commented 1 year ago

Also fixing --list so that it matches the expected behavior of only including products of direct dependencies: https://github.com/apple/swift-package-manager/pull/6806

❯ swift package plugin --list
‘archive’ (plugin ‘AWSLambdaPackager’ in package ‘swift-aws-lambda-runtime’)
‘generate-code-from-openapi’ (plugin ‘OpenAPIGeneratorCommand’ in package ‘swift-openapi-generator’)