Skip to content

Add %p to LLVM_PROFILE_FILE pattern when running tests with coverage #8894

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
25 changes: 19 additions & 6 deletions Sources/Commands/Utilities/TestingSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,25 @@ enum TestingSupport {
if buildParameters.testingParameters.enableCodeCoverage {
// Defines the path at which the profraw files will be written on test execution.
//
// `%m` will create a pool of profraw files and append the data from
// each execution in one of the files. This doesn't matter for serial
// execution but is required when the tests are running in parallel as
// SwiftPM repeatedly invokes the test binary with the test case name as
// the filter.
let codecovProfile = buildParameters.buildPath.appending(components: "codecov", "\(library)%m.profraw")
// `%Nm` will create a pool of N profraw files and append the data from each execution
// in one of the files. The runtime takes care of selecting a raw profile from the pool,
// locking it, and updating it before the program exits. If N is not specified, it is
// inferred to be 1.
//
// This is fine for parallel execution within a process, but for parallel tests, SwiftPM
// repeatedly invokes the test binary with the testcase name as the filter and the
// locking cannot be enforced by the runtime across the process boundaries.
//
// It's also possible that tests themselves will fork (e.g. for exit tests provided by
// Swift Testing), which will inherit the environment of the parent process, and so
// write to the same file, leading to profile data corruption.
//
// For these reasons, we unilaterally also add a %p, which will cause uniquely named
// files per process.
//
// These are all merged using `llvm-profdata merge` once the outer test command has
// completed.
let codecovProfile = buildParameters.buildPath.appending(components: "codecov", "\(library)%m.%p.profraw")
env["LLVM_PROFILE_FILE"] = codecovProfile.pathString
}
#if !os(macOS)
Expand Down