Skip to content

Commit bb9a135

Browse files
Fix command plugin when run on targets with dependencies (#801)
### Motivation The command plugin is used for ahead-of-time (AOT) code generation. It can be run with `--target`, or without, in which case it considers all targets based on the presence of the requisite OpenAPI document and config file. If a target is detected that doesn't have either of these files, it should be considered an error if that target was explicitly asked for using `--target`. If it wasn't—either because we're looking at all targets, or because it is a dependency—it should be skipped. ### Modifications - Extend integration test to repro issue for AOT targets with dependencies - Skip targets that have no requisite files for generation unless they were asked for ### Result Fixes command plugin when used for AOT generation with targets with target dependencies. ### Test Plan This PR starts out with a commit that updated the integration test to repro the issue, which we should see fail in CI. Only then was a commit added to address the issue, restoring the CI to green.
1 parent 576e620 commit bb9a135

File tree

11 files changed

+51
-8
lines changed

11 files changed

+51
-8
lines changed

.licenseignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ Examples/streaming-chatgpt-proxy/players.txt
4343
**/Makefile
4444
**/*.html
4545
.editorconfig
46+
IntegrationTest/**/Empty.swift

IntegrationTest/Package.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,12 @@ let package = Package(
5151
name: "MockTransportServer",
5252
dependencies: ["Server", .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")]
5353
),
54+
// Targets to integration test the command plugin
55+
.target(name: "Empty"),
56+
.target(name: "TypesAOT", dependencies: [.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")]),
57+
.target(
58+
name: "TypesAOTWithDependency",
59+
dependencies: ["Empty", .product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")]
60+
),
5461
]
5562
)

IntegrationTest/Sources/Empty/Empty.swift

Whitespace-only changes.

IntegrationTest/Sources/TypesAOT/Empty.swift

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
generate:
2+
- types
3+
accessModifier: package
4+
namingStrategy: idiomatic
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../openapi.yaml

IntegrationTest/Sources/TypesAOTWithDependency/Empty.swift

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
generate:
2+
- types
3+
accessModifier: package
4+
namingStrategy: idiomatic
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../openapi.yaml

Plugins/OpenAPIGeneratorCommand/plugin.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,23 @@ extension SwiftOpenAPIGeneratorPlugin: CommandPlugin {
8181
log("- ✅ OpenAPI code generation for target '\(target.name)' successfully completed.")
8282
hadASuccessfulRun = true
8383
} catch let error as PluginError {
84-
if targetNameArguments.isEmpty, case .fileErrors(let errors) = error,
85-
Set(errors.map(\.fileKind)) == Set(FileError.Kind.allCases),
84+
if case .fileErrors(let errors) = error, Set(errors.map(\.fileKind)) == Set(FileError.Kind.allCases),
8685
errors.map(\.issue).allSatisfy({ $0 == FileError.Issue.noFilesFound })
8786
{
88-
// The command plugin was run with no --target argument so its looping over all targets.
89-
// If a target does not have any of the required files, this should only be considered an error
90-
// if the plugin is being explicitly run on a target, either using the build plugin, or using the
91-
// command plugin with a --target argument.
92-
log("- Skipping because target isn't configured for OpenAPI code generation.")
93-
continue
87+
// The error is that neither of the required files are present for code generation for this target.
88+
// This should only be considered an error if this target was explicitly provided as a target for
89+
// code generation with --target.
90+
// We may get this error for other targets if:
91+
//
92+
// 1. The command plugin was run with no --target arguments, in which case the plugin loops over
93+
// all targets; or
94+
// 2. This target is a dependency of a target that was requested using --target.
95+
//
96+
// In either of these cases, we should not consider this an error and skip the target.
97+
if !targetNameArguments.contains(target.name) {
98+
log("- Skipping because target isn't configured for OpenAPI code generation.")
99+
continue
100+
}
94101
}
95102

96103
if error.isMisconfigurationError {

0 commit comments

Comments
 (0)