Pointing the `swift` command to a nightly toolchain on macOS

apologies if this is a very basic question, i do not develop on macOS most of the time.

right now, i am installing nightly-5.8 in GitHub actions using the following:

    SWIFT_TOOLCHAIN_DIRECTORY: /Library/Developer/Toolchains/swift-${{ matrix.swift.toolchain }}.xctoolchain
            -   runner: macos-12
                prefix: xcode
            -   toolchain:  5.8-DEVELOPMENT-SNAPSHOT-2023-03-07-a
                branch:     swift-5.8-branch
    -   uses: actions/checkout@v2

    -   name: cache swift toolchains
        uses: actions/cache@v2
            path: swift-${{ matrix.swift.toolchain }}.pkg
            key: ${{ matrix.os.runner }}:swift:${{ matrix.swift.toolchain }}
    -   name: cache status
        id:   cache_status
        uses: andstor/file-existence-action@v1
            files: swift-${{ matrix.swift.toolchain }}.pkg
    -   name: download swift toolchain 
        if: steps.cache_status.outputs.files_exists == 'false'
        run:   "curl https://download.swift.org/\
                ${{ matrix.swift.branch }}/\
                ${{ matrix.os.prefix }}/\
                swift-${{ matrix.swift.toolchain }}/\
                swift-${{ matrix.swift.toolchain }}-osx.pkg \
                --output swift-${{ matrix.swift.toolchain }}.pkg"
    -   name: set up swift
        run: |
            sudo installer -pkg swift-${{ matrix.swift.toolchain }}.pkg -target /

and then i use the toolchain in subsequent steps like:

-   name: build 
    run: |
        $SWIFT_TOOLCHAIN_DIRECTORY/usr/bin/swift --version
        $SWIFT_TOOLCHAIN_DIRECTORY/usr/bin/swift build

is there a simple way to configure the macOS runner so that the swift command uses the $SWIFT_TOOLCHAIN_DIRECTORY/usr/bin/swift toolchain instead of the 5.7.3 toolchain?

You can prefix build commands with xcrun --toolchain <name-or-id> or set the TOOLCHAINS environment variable.

1 Like

the goal is to use the same presubmit pipeline script (which contains many swift invocations) on macOS and linux. so i will try the TOOLCHAINS variable. thanks!