Skip to content

Include search path for Swift Testing's macro plugin from toolchain if present #8670

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

Conversation

stmontgomery
Copy link
Contributor

@stmontgomery stmontgomery commented May 14, 2025

This modifies the logic for determining compiler flags for locating Swift Testing content such that it will always include a search path for Swift Testing's macro plugin at its newer, testing-specific location in the toolchain, if that directory exists.

Motivation:

Swift Testing installs a macro plugin library named TestingMacros into the official Swift toolchains for several platforms. By convention, most Swift macro plugins are installed into the toolchain's usr/lib/swift/host/plugins directory. However, testing libraries are somewhat special in the sense that they should generally only be available for use in "non-product" targets, i.e. targets which are not intended for distribution and only intended for use in qualifying the main product targets which will be distributed.

For that reason, in Darwin toolchains, Swift Testing's plugin is installed into a different location than for other platforms: usr/lib/swift/host/plugins/testing (note the final testing/ directory) — see the CMake rules where that is controlled. Over time, we'd like to move the macro plugin on other platforms to that location for consistency, because the current inconsistent install path has caused friction for users if they attempt to form search paths to the plugin on their own — see swiftlang/swift-testing#1039. So this PR paves the way for Swift Testing to begin to relocate its plugin by ensuring that if this distinct plugin directory exists in a toolchain, it will be preferred.

Modifications:

  • Modify logic in UserToolchain.swift to begin passing -plugin-path whenever the relevant, testing-specific plugin path exists in a toolchain.
  • Add a new test which validates this, by simulating the scenario of not having a custom toolchain and validating the expected flags are passed. (This most closely matches the current scenario today of using the toolchain included in Xcode, and in that toolchain the plugin is already installed into the testing/-suffixed path.)

rdar://151319768

@stmontgomery stmontgomery requested a review from briancroom May 14, 2025 18:19
@stmontgomery stmontgomery self-assigned this May 14, 2025
@stmontgomery stmontgomery added the swift test Changes impacting `swift test` tool label May 14, 2025
@stmontgomery stmontgomery added the macros Support for macros label May 14, 2025
@stmontgomery stmontgomery added the build system Changes to interactions with build systems label May 14, 2025
@stmontgomery
Copy link
Contributor Author

@swift-ci please test

@jakepetroules
Copy link
Contributor

Does SwiftBuild need any similar change here?

@stmontgomery
Copy link
Contributor Author

Does SwiftBuild need any similar change here?

Nope, SwiftBuild does the correct thing already! See the relevant code.

@stmontgomery stmontgomery merged commit 0c47332 into swiftlang:main May 15, 2025
6 checks passed
@stmontgomery stmontgomery deleted the testing-macros-plugin-path branch May 15, 2025 18:42
xedin added a commit to xedin/swift-package-manager that referenced this pull request May 21, 2025
stmontgomery added a commit to stmontgomery/swift-package-manager that referenced this pull request May 21, 2025
…f present (swiftlang#8670)

This modifies the logic for determining compiler flags for locating
Swift Testing content such that it will always include a search path for
Swift Testing's macro plugin at its newer, testing-specific location in
the toolchain, if that directory exists.

### Motivation:

Swift Testing installs a macro plugin library named `TestingMacros` into
the official Swift toolchains for several platforms. By convention, most
Swift macro plugins are installed into the toolchain's
`usr/lib/swift/host/plugins` directory. However, testing libraries are
somewhat special in the sense that they should generally only be
available for use in "non-product" targets, i.e. targets which are not
intended for distribution and only intended for use in qualifying the
main product targets which _will_ be distributed.

For that reason, in Darwin toolchains, Swift Testing's plugin is
installed into a different location than for other platforms:
`usr/lib/swift/host/plugins/testing` (note the final `testing/`
directory) — see the [CMake
rules](https://github.com/swiftlang/swift-testing/blob/72afbb418542654781a6b7853479c7e70a862b6f/Sources/TestingMacros/CMakeLists.txt#L67-L75)
where that is controlled. Over time, we'd like to move the macro plugin
on other platforms to that location for consistency, because the current
_inconsistent_ install path has caused friction for users if they
attempt to form search paths to the plugin on their own — see
swiftlang/swift-testing#1039. So this PR paves
the way for Swift Testing to begin to relocate its plugin by ensuring
that _if_ this distinct plugin directory exists in a toolchain, it will
be preferred.

### Modifications:

- Modify logic in `UserToolchain.swift` to begin passing `-plugin-path`
whenever the relevant, testing-specific plugin path exists in a
toolchain.
- Add a new test which validates this, by simulating the scenario of
_not_ having a custom toolchain and validating the expected flags are
passed. (This most closely matches the current scenario today of using
the toolchain included in Xcode, and in that toolchain the plugin is
already installed into the `testing/`-suffixed path.)

rdar://151319768
dschaefer2 pushed a commit that referenced this pull request May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build system Changes to interactions with build systems enhancement macros Support for macros swift test Changes impacting `swift test` tool
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants