Skip to content

command plugins: Inherit SwiftPM's --configuration flag in packageManager.build #7262

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 2 commits into from
Jan 18, 2024

Conversation

euanh
Copy link
Contributor

@euanh euanh commented Jan 17, 2024

This commit makes it possible for a build run on behalf of a command plugin to inherit the build configuration (debug or release) set for the whole swift package run using the --configuration flag.

Motivation:

When a command plugin asks for a target to be built by calling packageManager.build, it must specify a release or debug build. If no configuration is given, debug is the default. This overrides any configuration specified by the user with swift package -c <debug|release>.

A command plugin might often be used as an alterative entry point to Swift PM, responsible for building a target and then processing it in some way to generate the final product. The user might run swift package -c release some-plugin --target some-target, expecting a release build of target to be made, but currently the result will be a debug binary if the plugin uses the default options.

Modifications:

  • Added a new .inherit option for packageManager.build's configuration argument.; .debug remains the default, as before.
  • Added a test to verify that plugin-initiated builds are of the correct type.

Result:

A command plugin can request a target build matching the configuration specified on the SwiftPM command line. The default is still to make a debug build, however in future we might change this to inherit the overall SwiftPM configuration.

Alternatives:

A command plugin does not currently seem to have access to SwiftPM's build configuration. We could pass this information to the plugin, allowing the plugin author to pass it back in the packageManager.build call. This would be a less invasive change to SwiftPM, however the approach in this commit makes it easier to change the default to .inherit in the future.

@euanh
Copy link
Contributor Author

euanh commented Jan 17, 2024

Please let me know if I missed an existing way to do this.

Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

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

This is great, thanks!

@MaxDesiatov
Copy link
Contributor

@swift-ci test

@MaxDesiatov
Copy link
Contributor

One of the tests is failing with this error:

/home/build-user/swiftpm/Tests/CommandsTests/PackageToolTests.swift:2027: error: PackageToolTests.testCommandPluginTargetBuilds : failed - No such fixture: /home/build-user/swiftpm/Fixtures/Miscellaneous/Plugins/CommandPluginDiagnosticsStub
<EXPR>:0: error: PackageToolTests.testCommandPluginTargetBuilds : threw error "packagePathNotFound"

@euanh euanh force-pushed the plugin-inherit-build-configuration branch from 63ea6a6 to 7b6e99f Compare January 17, 2024 18:06
@euanh
Copy link
Contributor Author

euanh commented Jan 17, 2024

One of the tests is failing with this error:

/home/build-user/swiftpm/Tests/CommandsTests/PackageToolTests.swift:2027: error: PackageToolTests.testCommandPluginTargetBuilds : failed - No such fixture: /home/build-user/swiftpm/Fixtures/Miscellaneous/Plugins/CommandPluginDiagnosticsStub
<EXPR>:0: error: PackageToolTests.testCommandPluginTargetBuilds : threw error "packagePathNotFound"

Thanks, I missed these in a rebase. :(

@MaxDesiatov
Copy link
Contributor

@swift-ci test

@MaxDesiatov
Copy link
Contributor

@swift-ci test windows

@MaxDesiatov MaxDesiatov enabled auto-merge (squash) January 17, 2024 21:26
@MaxDesiatov
Copy link
Contributor

@swift-ci test linux

@euanh
Copy link
Contributor Author

euanh commented Jan 18, 2024

@MaxDesiatov It looks like the Linux smoke test might have hit swiftlang/sourcekit-lsp#1025

/home/build-user/sourcekit-lsp/Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift:212:20: warning: 'init(buildParameters:graph:additionalFileRules:buildToolPluginInvocationResults:prebuildCommandResults:fileSystem:observabilityScope:)' is deprecated: replaced by 'init(productsBuildParameters:toolsBuildParameters:graph:)'
    let plan = try BuildPlan(
                   ^
/home/build-user/sourcekit-lsp/Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift:212:20: note: use 'init(productsBuildParameters:toolsBuildParameters:graph:)' instead
    let plan = try BuildPlan(
                   ^
/home/build-user/sourcekit-lsp/Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift:227:40: error: subscript 'subscript(_:)' requires that 'ResolvedTarget' conform to 'RangeExpression'
          guard let td = plan.targetMap[target] else {
                                       ^
Swift.Collection:2:23: note: where 'R' = 'ResolvedTarget'
    @inlinable public subscript<R>(r: R) -> Self.SubSequence where R : RangeExpression, Self.Index == R.Bound { get }
                      ^
/home/build-user/sourcekit-lsp/Sources/SKSwiftPMWorkspace/SwiftPMWorkspace.swift:241:38: error: subscript 'subscript(_:)' requires that 'ResolvedTarget' conform to 'RangeExpression'
        guard let td = plan.targetMap[target] else {
                                     ^
Swift.Collection:2:23: note: where 'R' = 'ResolvedTarget'
    @inlinable public subscript<R>(r: R) -> Self.SubSequence where R : RangeExpression, Self.Index == R.Bound { get }
                      ^

Could you please trigger the test run again? I've rebased on main.

euanh added 2 commits January 18, 2024 10:50
…ager.build

This commit makes it possible for a build run on behalf of a command plugin to inherit the build configuration (debug or release) set for the whole `swift package` run using the `--configuration` flag.

 ### Motivation:

When a command plugin asks for a target to be built by calling packageManager.build, it must specify a release or debug build.  If no configuration is given, debug is the default.   This overrides any configuration specified by the user with `swift package -c <debug|release>`.

A command plugin might often be used as an alterative entry point to Swift PM, responsible for building a target and then processing it in some way to generate the final product.   The user might run `swift package -c release some-plugin --target some-target`, expecting a release build of target to be made, but currently the result will be a debug binary if the plugin uses the default options.

 ### Modifications:

* Added a new `.inherit` option for packageManager.build's `configuration` argument.;  .debug remains the default, as before.
* Added a test to verify that plugin-initiated builds are of the correct type.

 ### Result:

A command plugin can request a target build matching the configuration specified on the SwiftPM command line.   The default is still to make a debug build, however in future we might change this to inherit the overall SwiftPM configuration.

 ### Alternatives:

A command plugin does not currently seem to have access to SwiftPM's build configuration.   We could pass this information to the plugin, allowing the plugin author to pass it back in the packageManager.build() call.   This would be a less invasive change to SwiftPM, however the approach in this commit makes it easier to change the default to .inherit in the future.
@euanh euanh force-pushed the plugin-inherit-build-configuration branch from 7b6e99f to 8656b7c Compare January 18, 2024 10:51
@MaxDesiatov
Copy link
Contributor

@swift-ci test

@MaxDesiatov
Copy link
Contributor

@swift-ci test windows

@MaxDesiatov MaxDesiatov merged commit 3ef830d into swiftlang:main Jan 18, 2024
@euanh euanh deleted the plugin-inherit-build-configuration branch January 18, 2024 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants