pointfreeco / swift-composable-architecture

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.
https://www.pointfree.co/collections/composable-architecture
MIT License
12.35k stars 1.44k forks source link

Release build library that uses TCA results in a runtime crash inside debug application #2362

Closed mrtnlst closed 1 year ago

mrtnlst commented 1 year ago

Description

I'm facing an obscure crash when building a static library that uses TCA (only links, not embeds) in a release configuration to customers. When the framework is added to an app target (alongside adding TCA) and building it in a debug configuration, a crash occurs in WithViewStore's initializer. If we remove all code inside the #if DEBUG compiler directive there, it works.

If this is an issue of TCA or something entirely else we don't know. We have faced this issue since at least version 0.35.1.

Crash

Checklist

Expected behavior

I expect that a release built binary framework (that uses TCA as a dependency) can be build and run inside an app in either debug or release configuration.

Actual behavior

A release built binary framework (that uses TCA as a dependency) can only be run inside an app in release configuration.

Steps to reproduce

I have provided an example project at https://github.com/mrtnlst/SampleProject. It consists of:

By default the project embeds the framework using the + button under Frameworks, Libraries, and Embedded Content and you can see it builds and launches successfully (displaying a button).

To reproduce the crash:

  1. Remove the embedded framework under Frameworks, Libraries, and Embedded Content
  2. Select the Framework scheme and build it (it's already configured to run in release configuration)
  3. Select the product in the project navigator below "Products"
  4. Open the SampleProjectFramework in Finder
  5. Drag the SampleProjectFramework inside the framework artefact to Frameworks, Libraries, and Embedded Content
  6. Build and run the App target again

Hints

The issue doesn't occur when

Removed content inside compiler directive

The Composable Architecture version information

1.0.0

Destination operating system

iOS 16

Xcode version information

Xcode 14.3

Swift Compiler version information

swift-driver version: 1.75.2 Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100)
mbrandonw commented 1 year ago

Hi @mrtnlst, have you confirmed that this is actually a problem with TCA, or just a problem in general Swift projects when importing release libraries into debug projects? I'm not aware of anything we can do specifically in the library to prevent this kind of crash. I would imagine this happens in any Swift project.

And beyond that, we highly recommend against doing this in general. Building TCA for release causes all types of warnings, preconditions and helpers to be omitted from the library. You will no longer be notified when the library detects improper usage of an API, the _printChanges operator will not work, and worst of all TestStore will not work at all so you can't even write tests.

Since we're not really sure this is an actual TCA problem I am going to convert this to a discussion as we all try to figure out what is going on.