JohnSundell / ShellOut

Easily run shell commands from a Swift script or command line tool
MIT License
865 stars 83 forks source link

Cannot build SwiftUI preview #45

Closed pballart closed 3 years ago

pballart commented 4 years ago

I have a project with SwiftUI and since I introduced ShellOut via Swift Package Manager the canvas preview does not build although the app compiles fine if you run it on a simulator or device. It's just the preview that fails when building with the following error:

SchemeBuildError: Failed to build the scheme "MyApp"

use of undeclared type 'Process'

Compile /Users/Pau/Library/Developer/Xcode/DerivedData/MyApp-ctrdkueooaaxgiatjsycywzfdcql/SourcePackages/checkouts/ShellOut/Sources/ShellOut.swift:
/Users/Pau/Library/Developer/Xcode/DerivedData/MyApp-ctrdkueooaaxgiatjsycywzfdcql/SourcePackages/checkouts/ShellOut/Sources/ShellOut.swift:34:14: error: use of undeclared type 'Process'
    process: Process = .init(),

I believe that the problem may be that Process is only available on macOS, not on iOS. I'm using ShellOut as a dependency of SwiftyMocky so when I run the tests on my mac it works fine but for some reason when Xcode compiles SwiftUI previews it's also compiling the test target and the dependencies and tries to compile ShellOut in iOS

ohitsdaniel commented 4 years ago

Hi there Pau, 👋

I ran into the same issue. As you mentioned, the issue is that Process is only part of Foundation on macOS and Linux but not on iOS.

See docs: Foundation Process Docs Linux's Foundation's Process

Whenever you try to run a Preview in Xcode, it compiles all targets in the workspace with the iOS SDK (Not sure why it was implemented this way and there is an implicit target which we have no control over). If you now have a target that either directly or indirectly depends on ShellOut (SwiftyMockyCLI for example), Xcode tries to build ShellOut for iOS which fails because Process is not part of iOS's Foundation.

@JohnSundell, as Process is not available on iOS and SPM currently has no measures in place to blacklist or whitelist a certain platforms, one solution would be to guard the tests and the implementation using pre-processor macros #if os(macOS) || os(Linux). What do you think? :)

I'll open a PR to suggest these changes and link it with this issue.

pballart commented 3 years ago

Seems that his issue is already resolved as per #49