finestructure / Arena

A command line tool to create Swift Playground projects with SPM package dependencies
MIT License
685 stars 16 forks source link

Packages that import XCTest break generated playgrounds #75

Closed vzsg closed 1 year ago

vzsg commented 4 years ago

As we've discussed on Discord, using Arena 1.0.0 to make a playground for RxSwift (5.1.1 at the time of writing) results in a seemingly valid workspace playground that Xcode loads successfully, with autocomplete to boot.

However, running the playground fails with errors such as:

error: MyPlayground.playground:8:15: error: cannot find type 'Observable' in scope
let triggers: Observable<Int> = Observable.merge(
              ^~~~~~~~~~

error: MyPlayground.playground:15:29: error: cannot find type 'Observable' in scope
    .flatMapLatest { max -> Observable<Int> in
                            ^~~~~~~~~~

An example repo is available here (GH link), or here (ZIP).


As a workaround, editing Dependencies/Package.swift and removing all product dependencies except RxSwift makes the playground work.

From:

package.targets = [
    .target(name: "Dependencies",
        dependencies: [
            .product(name: "RxSwift", package: "RxSwift"),
.product(name: "RxCocoa", package: "RxSwift"),
.product(name: "RxRelay", package: "RxSwift"),
.product(name: "RxBlocking", package: "RxSwift"),
.product(name: "RxTest", package: "RxSwift")
        ]
    )
]

To:

package.targets = [
    .target(name: "Dependencies",
        dependencies: [
            .product(name: "RxSwift", package: "RxSwift")
        ]
    )
]
vzsg commented 4 years ago

I did some tests, it seems the RxTest product is the one that kills the mood. The other four, in any combination, work fine.

I wonder what's so special about it... The only thing I see is this symlink.

That symlink is a red herring, Xcode could clone it just fine and it appeared as a regular file at the proper place. In fact, the complete Sources folder in RxSwift is just a bunch of symlinks.

There was another red herring in the build logs:

image

Changing the deployment target in Dependencies/Package.swift to macOS 10.15 made the warning go away, but did not change the original error.


My next suspicion is the fact that RxTest is pulling in XCTest, and that might somehow be illegal for a playground.

vzsg commented 4 years ago

It's definitely the XCTest import! There is a single file inside RxTest that imports it. After removing this file from the project, the playground started working.


On a whim, I tried to make a playground for Nimble, and that resulted in the same kind of errors.

finestructure commented 4 years ago

Thanks a lot for raising this and finding out about XCTest!

I know XCTest didn't work in playgrounds in the past but I thought that wasn't a problem anymore. I'll see what I can do.

finestructure commented 4 years ago

Raised as FB8882995. From what I gather this might be a problem with playgrounds.

Peter-Schorn commented 3 years ago

A similar issue occurs when you try to add a SPM package product that imports XCTest to a command line project. In order for the project to build you must make certain changes to the build settings:

For the project build settings, search for "Runpath Search Paths" and add the following paths:

For the project build settings, set "Enable Testing Search Paths" to "Yes"

You don't have access to these settings in a playground, so there's probably nothing you can do.

finestructure commented 3 years ago

@pointfreeco have come up with a workaround to ship XCTFail in library code and I wonder if this would work in a playground as well.

It's using their new package XCTest Dynamic Overlay. I'll need to try this but perhaps adding this package and import XCTestDynamicOverlay at the top might work?

finestructure commented 3 years ago

This might require defining all the other XCTest symbols though - the library only declares XCTFail.

finestructure commented 2 years ago

I wonder if conditional linking of XCTest would be a workable solution (upstream): https://github.com/apple/swift-collections/blob/main/Package.swift#L72-L81

finestructure commented 1 year ago

I've just tried this with RxSwift 6.5.0 and Xcode 14.1 and this seems to be fixed now:

CleanShot 2022-11-30 at 08 44 35@2x