Skip to content

Latest commit

 

History

History
459 lines (331 loc) · 16.6 KB

CONTRIBUTING.md

File metadata and controls

459 lines (331 loc) · 16.6 KB

Contributing to Swift Package Manager

There are several types of contributions one can make. Bug fixes, documentation and enhancements that do not materially change the user facing semantics of Swift Package Manager should be submitted directly as PR.

Larger changes that do materially change the semantics of Swift Package Manager (e.g. changes to the manifest format or behavior) are required to go through Swift Evolution Process.

To see how previous evolution decisions for SwiftPM have been made and have some direction for the development of future features please check out the Community Proposals.

For more information about making contributions to the Swift project in general see Swift Contribution Guide.

Reporting issues

Issues are tracked using SwiftPM GitHub Issue Tracker.

Fill the following fields:

  • Title: A one line summary of the problem you're facing.
  • Description: The complete description of the problem. Be specific.
  • Expected behavior: How you expect SwiftPM to behave.
  • Actual behavior : What actually happens.
  • Steps to reproduce: Be specific, provide steps to reproduce the bug.
  • Swift Package Manager version/commit hash : With which version are you testing.
  • Actual behavior : What actually happens.
  • Swift & OS version : (output of swift --version && uname -a).

Please include a minimal example package which can reproduce the issue. The sample package can be attached with the report or you can include the URL of the package hosted on places like GitHub. Also, include the verbose logs by adding --verbose or -v after a subcommand. For example:

$ swift build --verbose
$ swift package update --verbose

If the bug is with a generated Xcode project, include how the project was generated and the Xcode build log.

Setting up the development environment

First, clone a copy of SwiftPM code from https://github.com/apple/swift-package-manager.

If you are preparing to make a contribution you should fork the repository first and clone the fork which will make opening Pull Requests easier. See "Creating Pull Requests" section below.

SwiftPM is typically built with a pre-existing version of SwiftPM present on the system, but there are multiple ways to setup your development environment:

Using Xcode (Easiest)

  1. Install Xcode from https://developer.apple.com/xcode (including betas!).
  2. Verify the expected version of Xcode was installed.
  3. Open SwiftPM's Package.swift manifest with Xcode.
  4. Use Xcode to inspect, edit, and build the code.
  5. Select the SwiftPM-Package scheme to run the tests from Xcode. Note that the SwiftPM-Package should be built prior to running any other schemes. This is so the PackageDescription module can be built and cached for use.

Using the Command Line

If you are using macOS and have Xcode installed, you can use Swift from the command line immediately.

If you are not using macOS or do not have Xcode installed, you need to download and install a toolchain.

Installing a toolchain

  1. Download a toolchain from https://swift.org/download/
  2. Install it and verify the expected version of the toolchain was installed:

macOS

$> export TOOLCHAINS=swift
$> xcrun --find swift
/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/swift
$> swift package --version
Swift Package Manager - Swift 5.3.0
$> swift --version
Apple Swift version 5.3

Linux

$> export PATH=/path/to/swift-toolchain/usr/bin:"${PATH}"
$> which swift
/path/to/swift-toolchain/usr/bin/swift
$> swift package --version
Swift Package Manager - Swift 5.3.0
$> swift --version
Apple Swift version 5.3

Note: Alternatively use tools like swiftenv that help manage toolchains versions.

Local Development

With a Swift toolchain installed and the SwiftPM code cloned, you are ready to make changes and test them locally.

Building

$> swift build

A successful build will create a .build/ directory with the following approximate structure:

artifacts
checkouts
debug
repositories
x86_64-apple-macosx

Binary artifacts are located in x86_64-apple-macosx/ when building on macOS, or the equivalent on other architectures and operating systems.

These binaries can be used to test the code modification. For example, to test the swift package init and swift build commands from the new SwiftPM artifacts in .build/:

$> cd /tmp && mkdir hello && cd hello
$> /path/to/swiftpm/.build/x86_64-apple-macosx/debug/swift-package init
$> /path/to/swiftpm/.build/x86_64-apple-macosx/debug/swift-build

Testing

$> swift test

to run a single test:

$> swift test --filter PackageGraphTests.DependencyResolverTests/testBasics

Or another example, to run tests for the test targets BuildTests and WorkspaceTests, but skip some test cases:

$> swift test --filter BuildTests --skip BuildPlanTests --filter WorkspaceTests --skip InitTests

To run the performance tests, enable them with an ENV variable:

$> export TSC_ENABLE_PERF_TESTS=1
$> swift test -c release --filter PerformanceTests

The bootstrap script

The bootstrap script is designed for building SwiftPM on systems that do not have Xcode or a toolchain installed. It is used on bare systems to bootstrap the Swift toolchain (including SwiftPM), and as such not typically used outside the Swift team.

The bootstrap script requires having CMake and Ninja installed. Please refer to the Get Started guide on the Swift project repository for installation instructions.

Clone the following repositories beside the SwiftPM directory:

  1. swift-argument-parser and check out tag with the latest version.

    For example, if the latest tag is 0.4.3:

    $> git clone https://github.com/apple/swift-argument-parser --branch 0.4.3
  2. swift-llbuild as llbuild

    $> git clone https://github.com/apple/swift-llbuild llbuild

    Note: Make sure the directory for llbuild is called "llbuild" and not "swift-llbuild".

  3. swift-tools-support-core

    $> git clone https://github.com/apple/swift-tools-support-core
  4. Yams and checkout tag with the latest version before 5.0.0.

    For example, if the latest tag is 4.0.6:

    $> git clone https://github.com/jpsim/yams --branch 4.0.6
  5. swift-driver

    $> git clone https://github.com/apple/swift-driver
  6. swift-system and check out tag with the latest version.

    For example, if the latest tag is 1.0.0:

    $> git clone https://github.com/apple/swift-system --branch 1.0.0
  7. swift-collections and check out tag with the latest version.

    For example, if the latest tag is 1.0.1:

    $> git clone https://github.com/apple/swift-collections --branch 1.0.1
  8. swift-crypto and check out tag with the latest version.

    For example, if the latest tag is 2.3.0:

    $> git clone https://github.com/apple/swift-crypto --branch 2.3.0
  9. swift-asn1

    $> git clone https://github.com/apple/swift-asn1
  10. swift-certificates

    $> git clone https://github.com/apple/swift-certificates

Building

$> Utilities/bootstrap build

See "Using the Command Line / Building" section above for more information on how to test the new artifacts.

Testing

$> Utilities/bootstrap test

Working with Docker to build and test for Linux

When developing on macOS and need to test on Linux, install Docker and Docker compose and use the following docker compose commands:

Prepare the underlying image with the selected Ubuntu and Swift versions:

docker-compose \
  -f Utilities/docker/docker-compose.yaml \
  -f Utilities/docker/docker-compose.<os-version>.<swift-version>.yaml \
  build

Start an interactive shell session:

docker-compose \
  -f Utilities/docker/docker-compose.yaml \
  -f Utilities/docker/docker-compose.<os-version>.<swift-version>.yaml \
  run --rm shell

Build SwiftPM (using the pre-installed SwiftPM version).

docker-compose \
  -f Utilities/docker/docker-compose.yaml \
  -f Utilities/docker/docker-compose.<os-version>.<swift-version>.yaml \
  run --rm build

Test SwiftPM (using the pre-installed SwiftPM version).

docker-compose \
  -f Utilities/docker/docker-compose.yaml \
  -f Utilities/docker/docker-compose.<os-version>.<swift-version>.yaml \
  run --rm test

Build SwiftPM using the bootstrap script:

docker-compose \
  -f Utilities/docker/docker-compose.yaml \
  -f Utilities/docker/docker-compose.<os-version>.<swift-version>.yaml \
  run --rm bootstrap-build

Test SwiftPM using the bootstrap script:

docker-compose \
  -f Utilities/docker/docker-compose.yaml \
  -f Utilities/docker/docker-compose.<os-version>.<swift-version>.yaml \
  run --rm bootstrap-test

Note there are several Linux and Swift versions options to choose from, e.g.:

docker-compose.1804.53.yaml => Ubuntu 18.04, Swift 5.3

docker-compose.2004.54.yaml => Ubuntu 20.04, Swift 5.4

docker-compose.2004.main.yaml => Ubuntu 20.04, Swift nightly

Creating Pull Requests

  1. Fork: https://github.com/apple/swift-package-manager
  2. Clone a working copy of your fork
  3. Create a new branch
  4. Make your code changes
  5. Try to keep your changes (when possible) below 200 lines of code.
  6. We use SwiftFormat to enforce code style. Please install and run SwiftFormat before submitting your PR.
  7. Commit (include the Radar link or GitHub issue id in the commit message if possible and a description your changes). Try to have only 1 commit in your PR (but, of course, if you add changes that can be helpful to be kept aside from the previous commit, make a new commit for them).
  8. Push the commit / branch to your fork
  9. Make a PR from your fork / branch to apple: main
  10. While creating your PR, make sure to follow the PR Template providing information about the motivation and highlighting the changes.
  11. Reviewers are going to be automatically added to your PR
  12. Pull requests will be merged by the maintainers after it passes CI testing and receives approval from one or more reviewers. Merge timing may be impacted by release schedule considerations.

By submitting a pull request, you represent that you have the right to license your contribution to Apple and the community, and agree by submitting the patch that your contributions are licensed under the Swift license.

Continuous Integration

SwiftPM uses swift-ci infrastructure for its continuous integration testing. The bots can be triggered on pull-requests if you have commit access. Otherwise, ask one of the code owners to trigger them for you.

To run smoke test suite with the trunk compiler and other projects use:

@swift-ci please smoke test

This is required before a pull-request can be merged.

To run just the self-hosted test suite (faster turnaround times so it can be used to get quick feedback) use:

@swift-ci please smoke test self hosted

To run the swift toolchain test suite including SwiftPM use:

@swift-ci please test

To run package compatibility test suite (validates we do not break 3rd party packages) use:

@swift-ci please test package compatibility

Generating Documentation

SwiftPM uses DocC to generate some of its documentation (currently only the PackageDescription module). Documentation can be built using Xcode's GUI (Product → Build Documentation or ⌃⇧⌘D) or manually:

  1. Build and dump the symbol graph metadata used to generate the documentation:
swift package dump-symbol-graph
  1. Generate the documentation and start a local preview server to review your changes:
xcrun docc preview Sources/PackageDescription/PackageDescription.docc --additional-symbol-graph-dir .build/*/symbolgraph/

Note that this may generate documentation for multiple modules — the preview link for PackageDescription will typically be: http://localhost:8000/documentation/packagedescription

Advanced

Using Custom Swift Compilers

SwiftPM needs the Swift compiler to parse Package.swift manifest files and to compile Swift source files. You can use the SWIFT_EXEC and SWIFT_EXEC_MANIFEST environment variables to control which compiler to use for these operations.

SWIFT_EXEC_MANIFEST: This variable controls which compiler to use for parsing Package.swift manifest files. The lookup order for the manifest compiler is: SWIFT_EXEC_MANIFEST, swiftc adjacent to the swiftpm binaries, then SWIFT_EXEC

SWIFT_EXEC: This variable controls which compiler to use for compiling Swift sources. The lookup order for the sources' compiler is: SWIFT_EXEC, then swiftc adjacent to swiftpm binaries. This is also useful for Swift compiler developers when they want to use a debug compiler with SwiftPM.

$> SWIFT_EXEC=/path/to/my/built/swiftc swift build

Overriding the Path to the Runtime Libraries

SwiftPM computes the path of its runtime libraries relative to where it is installed. This path can be overridden by setting the environment variable SWIFTPM_CUSTOM_LIBS_DIR to a directory containing the libraries, or a colon-separated list of absolute search paths. SwiftPM will choose the first path which exists on disk. If none of the paths are present on disk, it will fall back to built-in computation.

Making changes in TSC targets

SwiftPM uses Tools Support Core (aka TSC) for many of its general purpose utilities. Changes in SwiftPM often require changes in TSC first. To coordinate changes, open a PR against TSC first, then a second one against SwiftPM pulling the correct TSC version.

Community and Support

If you want to connect with the Swift community you can:

Additional resources

Troubleshooting

  • If during swift build you encounter this error:
/../apple-repos/swift-package-manager/.build/checkouts/swift-driver/Sources/SwiftDriver/Explicit Module Builds/InterModuleDependencyGraph.swift:102:3: error: unknown attribute '_spi'
  @_spi(Testing) public var isFramework: Bool
  ^

Make sure you are using SwiftPM 5.3

$> swift package --version
Swift Package Manager - Swift 5.3.0
  • If during swift build you encounter this error:
/../swift-package-manager/Sources/PackageLoading/Target+PkgConfig.swift:84:36: error: type 'PkgConfigError' has no member 'prohibitedFlags'
            error = PkgConfigError.prohibitedFlags(filtered.unallowed.joined(separator: ", "))
                    ~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~

Make sure to update your TSC (Tools Support Core):

$> swift package update

Alternatively, if you are using Xcode, you can update to the latest version of all packages:
Xcode App > File > Swift Packages > Update to Latest Package Versions