Skip to content

Crash Current context must not be nil in recordSnapshot #875

Closed
@finestructure

Description

@finestructure

I mentioned this crash to @stephencelis a couple of weeks ago when we first saw it with Xcode 16b2 but it had "disappeared" when I revisited our tests with Xcode 16b3.

Turns out it didn't really disappear but the conditions to trigger it changed in our project (SPI-Server). The conditions in that project are now:

  • a test is recording a snapshot in a closure marked @Sendable
  • the test must be be actually trying to record a snapshot (i.e. unless we had snapshot changes the crash wouldn't trigger)
  • it only crashes when snapshots are created within Xcode (due to the __XCODE_BUILT_PRODUCTS_DIR_PATHS env variable check), not when the test are run via swift test

NB: we see the crash also with Xcode 15.4 and it seems to be related to the @Sendable changes we introduced while making the project ready for Swift 6. Reverting to a non-sendable closure makes the snapshot write succeed.

However, I've just set up a new project and it's much simpler to trigger the crash there: https://github.com/finestructure/snapshot-testing-crash

CleanShot 2024-07-19 at 13 36 32@2x

NB: This test crashes on both Xcode 15.4 and Xcode 16b3. The culprit seems to be the async attribute on the test. It does not crash without it:

    func test_crash() async throws {
        // This crashes with `Current context must not be nil`
        assertSnapshot(of: "foo", as: .lines)
    }

    func test_no_crash() throws {
        // This does not crash
        assertSnapshot(of: "foo", as: .lines)
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions