Skip to content

refactor how plugins call the build system when using the CLI #4185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions Sources/Commands/SwiftPackageTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1170,32 +1170,20 @@ final class PluginDelegate: PluginInvocationDelegate {

// Create a build operation. We have to disable the cache in order to get a build plan created.
let outputStream = BufferedOutputByteStream()
let buildOperation = BuildOperation(
buildParameters: buildParameters,
let buildOperation = try self.swiftTool.createBuildOperation(
explicitProduct: explicitProduct,
cacheBuildManifest: false,
packageGraphLoader: { try self.swiftTool.loadPackageGraph(explicitProduct: explicitProduct) },
pluginScriptRunner: try self.swiftTool.getPluginScriptRunner(),
pluginWorkDirectory: try self.swiftTool.getActiveWorkspace().location.pluginWorkingDirectory,
outputStream: outputStream,
logLevel: logLevel,
fileSystem: swiftTool.fileSystem,
observabilityScope: self.swiftTool.observabilityScope
customBuildParameters: buildParameters,
customOutputStream: outputStream,
customLogLevel: logLevel
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the key change - basically use the generic swiftTool.createBuildOperation which will record things with the cancellation utility which is less error prone than doing it manually

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great.

)

// Save the instance so it can be canceled from the interrupt handler.
self.swiftTool.buildSystemRef.buildSystem = buildOperation

// Get or create the build description and plan the build.
let _ = try buildOperation.getBuildDescription()
let buildPlan = buildOperation.buildPlan!

// Run the build. This doesn't return until the build is complete.
var success = true
do {
try buildOperation.build(subset: buildSubset)
}
catch {
success = false
let success = buildOperation.buildIgnoringError(subset: buildSubset)

// Get the build plan used
guard let buildPlan = buildOperation.buildPlan else {
throw InternalError("invalid state, buildPlan is undefined")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the above is just a small cleanup

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better than the !. Thanks for cleaning it up! In practice, should never happen but still.

}

// Create and return the build result record based on what the delegate collected and what's in the build plan.
Expand Down Expand Up @@ -1942,3 +1930,14 @@ private extension Basics.Diagnostic {
.error("missing required argument \(argument)")
}
}

extension BuildOperation {
fileprivate func buildIgnoringError(subset: BuildSubset) -> Bool {
do {
try self.build(subset: subset)
return true
} catch {
return false
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also use observabilityScope.trap instead of this, but this preserved the previous behavior exactly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there was only one call site for this. Do you think there will be more, or would it be better to keep this do/catch at the one callsite?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normally you want to do something with the error, but in this case the code only seems to care about the end result.

3 changes: 2 additions & 1 deletion Sources/Commands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,7 @@ public class SwiftTool {
customBuildParameters: BuildParameters? = .none,
customPackageGraphLoader: (() throws -> PackageGraph)? = .none,
customOutputStream: OutputByteStream? = .none,
customLogLevel: Diagnostic.Severity? = .none,
customObservabilityScope: ObservabilityScope? = .none
) throws -> BuildOperation {
let graphLoader = { try self.loadPackageGraph(explicitProduct: explicitProduct) }
Expand All @@ -748,7 +749,7 @@ public class SwiftTool {
pluginScriptRunner: self.getPluginScriptRunner(),
pluginWorkDirectory: try self.getActiveWorkspace().location.pluginWorkingDirectory,
outputStream: customOutputStream ?? self.outputStream,
logLevel: self.logLevel,
logLevel: customLogLevel ?? self.logLevel,
fileSystem: self.fileSystem,
observabilityScope: customObservabilityScope ?? self.observabilityScope
)
Expand Down