swift-composable-architecture icon indicating copy to clipboard operation
swift-composable-architecture copied to clipboard

100% reproducible preview crash

Open Alex293 opened this issue 3 years ago • 6 comments

Description

Hi,

My preview crash and it's reproducible 100% with very little code. Looking at the stack trace I'd say it's a Swift or Swift runtime issue. Maybe the preview is using the Mac Swift runtime which cannot use latest Swift feature ?

Note that, converting the reducer to the new builder style avoid the crash.

Checklist

  • [ ] If possible, I've reproduced the issue using the main branch of this package.
  • [X] This issue hasn't been addressed in an existing GitHub issue.

Expected behavior

No response

Actual behavior

No response

Steps to reproduce

Pasting this into a newly created package and running the preview.

import Foundation
import XCTestDynamicOverlay
import SwiftUI
import ComposableArchitecture

public struct Client {
    public var foo: @Sendable () async throws -> Void

    public init(foo: @escaping @Sendable () async throws -> Void) {
        self.foo = foo
    }
}

public enum ClientKey: TestDependencyKey {
    public typealias Value = Client
    public static var previewValue = Client.mock
    public static let testValue = Client.unimplemented
}

public extension DependencyValues {
    var client: Client {
        get { self[ClientKey.self] }
        set { self[ClientKey.self] = newValue }
    }
}

public extension Client {
    static let mock = Self(
        foo: {
            try await Task.sleep(nanoseconds: NSEC_PER_SEC * 2)
        }
    )
}

public extension Client {
    static let unimplemented = Self(
        foo: XCTUnimplemented("\(Self.self).foo")
    )
}

public struct FooFeature: ReducerProtocol {
    public struct State {
        public init() {}
    }

    public enum Action {
        case startFoo
        case fooDone(TaskResult<Void>)
    }

    @Dependency(\.client) var client: Client

    public func reduce(into state: inout State, action: Action) -> Effect<Action, Never> {
        switch action {
        case .startFoo:
            return .task {
                await .fooDone(
                    TaskResult(catching: { try await self.client.foo() })
                )
            }
        case .fooDone(.success):
            return .none
        case .fooDone(.failure):
            return .none
        }
    }

    public init() {}
}

public struct FooFeatureView: View {

    private let store: StoreOf<FooFeature>

    public init(store: StoreOf<FooFeature>) {
        self.store = store
    }

    public var body: some View {
        WithViewStore(store.stateless) { viewStore in
            VStack {
                Spacer()
                Text("Hello")
                    .font(.largeTitle)
                Spacer()
                Button("StartFoo", action: { viewStore.send(.startFoo) })
                Spacer()
                Spacer()
            }
        }
        .background(Color(UIColor.systemBackground))
    }
}

struct FooFeatureView_Previews: PreviewProvider {

    static var previews: some View {
        NavigationView {
            FooFeatureView(store: .init(initialState: .init(), reducer: FooFeature()))
        }
    }
}

The Composable Architecture version information

protocol-beta

Destination operating system

Xcode preview

Xcode version information

Version 14.0 (14A309)

Swift Compiler version information

swift-driver version: 1.62.8 Apple Swift version 5.7 (swiftlang-5.7.0.127.4 clang-1400.0.29.50)
Target: arm64-apple-macosx12.0

Alex293 avatar Sep 11 '22 10:09 Alex293

@Alex293 Huh, I'm unable to reproduce this.

I copied and pasted your code into a TCA demo project (SwiftUI Case Studies) while pointed to the latest commit of protocol-beta in Xcode 14 RC and the preview seems to run fine without crashing.

Do you think something else in your project or Xcode could be causing the issue? Have you gone through the usual suspects to see if it gets things working?

  • Clean build folder
  • Delete derived data manually
  • Restart Xcode
  • Restart computer (😬 I know, but it sometimes works)

stephencelis avatar Sep 11 '22 17:09 stephencelis

I’m pretty sure I already tried clearing the derived data folder. I also made a separate package to reproduce in isolation that why I pasted the code here. One thing that I changed to the default package file is the platform by setting .iOS(.v14).

I’m not in front of my computer but I’ll try restarting juste un case and report here the stack trace I got.

Alex293 avatar Sep 11 '22 21:09 Alex293

I tried all the recommended steps, the crash still occurs. I don't know if its of importance but I updated my Mac to 12.5.1 (21G83) recently. crash.txt

If you don't find any clue in the stack trace, you can close the issue, at least it will be there if it occurs for someone else.

Alex293 avatar Sep 12 '22 08:09 Alex293

I was able to get @Alex293's code to crash as well as a Vanilla SwiftUI package with the following:

struct FooView: View {
  var body: some View {
    VStack {
      Text("Hello").font(.largeTitle)
      Button("StartFoo", action: { Task { await Task.yield() } })
    }
  }
}

I probably won't be able to dig into it right now. But just wanted to point out it's reproducible in Vanilla.

@Alex293 can you try the above example on your end?

iampatbrown avatar Sep 12 '22 09:09 iampatbrown

@iampatbrown this crashes too, I guess it's time to report a bug to Apple.

Alex293 avatar Sep 12 '22 09:09 Alex293

Completely forgot about this, sorry, just reported the fb to Apple: FB11569526

Alex293 avatar Sep 22 '22 16:09 Alex293

Going to close this issue since it seems to reside with Apple. Thanks @iampatbrown for tracking that down!

mbrandonw avatar Sep 27 '22 14:09 mbrandonw