Skip to content

Flaky Test: EnvironmentTests.current() #8739

Open
@kcieplak

Description

@kcieplak

Is it reproducible with SwiftPM command-line tools: swift build, swift test, swift package etc?

  • Confirmed reproduction steps with SwiftPM CLI. The description text must include reproduction steps with either of command-line SwiftPM commands, swift build, swift test, swift package etc.

Description

The test EnvironmentTests.current() is failing in a flaky manner.

[2025-05-28T20:06:46.816Z] ✘ Test current() recorded an issue at EnvironmentTests.swift:128:9: Expectation failed: (Environment.current["PATH"] → "/Users/jenkins/Library/Developer/Toolchains/swift-6.1-RELEASE.xctoolchain/usr/bin:/Users/jenkins/.swiftly/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/appleinternal/bin:/opt/apple/bin:/Library/Apple/usr/bin:/opt/xbs/bin:/usr/appleinternal/bin:/private/var/tanuki:/Users/jenkins/.swiftly/bin:/opt/homebrew/bin:/opt/homebrew/sbin") == (ProcessInfo.processInfo.environment[pathEnvVarName] → "/foo/bar")

This is because there is another test that runs in parallel that updates the environment.

    /// Important: This test is inherently race-prone, if it is proven to be
    /// flaky, it should run in a singled threaded environment/removed entirely.
    @Test(.disabled(if: isInCiEnvironment, "This test can disrupt other tests running in parallel."))
    func makeCustomPathEnv() async throws {
        let customEnvironment: Environment = .current
        let origPath = customEnvironment[.path]

        try Environment.makeCustom(["PATH": "/foo/bar"]) {
            #expect(Environment.current[.path] == "/foo/bar")
        }
        #expect(Environment.current[.path] == origPath)
    }

There is a boolean to disable this test isInCiEnvironment, which is linked to an environment variable.

public let isInCiEnvironment = ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] != nil

However this environment variable is not set in all pipeline cases, for example the Windows Self hosted pipeline does not set this.

This set of environment tests are inherently flaky as environment will be global across the test process not per thread.

The test should be not run in parallel, and maybe not at all.

Expected behavior

EnvironmentTests should not be flaky.

Actual behavior

No response

Steps to reproduce

swift test --parrallel

Swift Package Manager version/commit hash

No response

Swift & OS version (output of swift --version ; uname -a)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions