pointfreeco / swift-snapshot-testing

📸 Delightful Swift snapshot testing.
https://www.pointfree.co/episodes/ep41-a-tour-of-snapshot-testing
MIT License
3.77k stars 578 forks source link

swift 6.0.1 crash when running tests swift testing with @MainActor annotation only on linux #919

Open doozMen opened 4 days ago

doozMen commented 4 days ago

Describe the bug Only happens on linux

  1. clone https://github.com/doozMen/swift-testing-main-bug
  2. run unit tests with visual studio in debug

You will see it crashes. The docs state that Thread.isMainThread is not available. Weirdly it is on macOS just not on linux.

To Reproduce run the test project on linux and maybe add code

    #expect(Thread.isMainThread == true)

This will work on mac and not on linux

Expected behavior

Not creating deadlock because of doing dispach on main in sync from main

Screenshots

Screenshot 2024-10-17 at 16 16 31

Environment

Additional context Maybe also a bug in swift but not sure as when you the check is unavailable in async context according to the docs. So you should not rely on it?

Maybe have a asser option that is marked with @MainActor might be better?

stephencelis commented 3 days ago

@doozMen Thanks for the report. We're running SnapshotTesting on Linux for pointfree.co, but are on a particular branch that uses this logic to dispatch to the main queue instead:

https://github.com/pointfreeco/swift-snapshot-testing/blob/7de6fa587c7fa38e435186e124f1d8c11d36ceb3/Sources/SnapshotTesting/AssertSnapshot.swift#L352-L359

Would you be open to exploring if this fix would address the problem for you when applied to the main branch?