SwiftUI previews do not work when Skip is enabled
marcprux opened this issue · comments
The Xcode preview canvas for a SwiftUI view does not currently work when Skip is enabled in an app project.
For example, adding the following to the default Hello app will fail to build the preview:
#Preview {
ContentView()
}
The error in the "Build for Previews HelloSkip" log is:
Showing All Messages
PhaseScriptExecution Run\ skip\ gradle /Users/marc/Library/Developer/Xcode/DerivedData/Skip-Everything-aqywrhrzhkbvfseiqgxuufbdwdft/Build/Intermediates.noindex/Previews/HelloSkip/Intermediates.noindex/HelloSkip.build/Debug-iphonesimulator/HelloSkip.build/Script-499CD4452AC5B869001AE8D8.sh (in target 'HelloSkip' from project 'HelloSkip')
cd /opt/src/github/skiptools/skipapp-hello/Darwin
/bin/sh -c /Users/marc/Library/Developer/Xcode/DerivedData/Skip-Everything-aqywrhrzhkbvfseiqgxuufbdwdft/Build/Intermediates.noindex/Previews/HelloSkip/Intermediates.noindex/HelloSkip.build/Debug-iphonesimulator/HelloSkip.build/Script-499CD4452AC5B869001AE8D8.sh
note: running gradle build with: /Users/marc/Library/Developer/Xcode/DerivedData/Skip-Everything-aqywrhrzhkbvfseiqgxuufbdwdft/Build/Intermediates.noindex/Previews/HelloSkip/Products/Debug/skip gradle -p /opt/src/github/skiptools/skipapp-hello/Darwin/../Android launchDebug
Error: Gradle run error: Optional(SkipDriveExternal.ProcessResult.ExitStatus.terminated(code: 1))
execGradle: /opt/homebrew/bin/gradle -p ../Android launchDebug --warning-mode all --console=plain
GRADLE>
GRADLE> FAILURE: Build failed with an exception.
GRADLE>
GRADLE> * What went wrong:
GRADLE> Skip output directory does not exist at: /Users/marc/Library/Developer/Xcode/DerivedData/Skip-Everything-aqywrhrzhkbvfseiqgxuufbdwdft/Build/Intermediates.noindex/Previews/HelloSkip/Products/Debug-iphonesimulator/../../../SourcePackages/plugins/skipapp-hello.output/HelloSkip/skipstone
GRADLE>
GRADLE> * Try:
GRADLE> > Run with --stacktrace option to get the stack trace.
GRADLE> > Run with --info or --debug option to get more log output.
GRADLE> > Run with --scan to get full insights.
GRADLE> > Get more help at https://help.gradle.org.
GRADLE>
GRADLE> BUILD FAILED in 330ms
running gradle build with: /Users/marc/Library/Developer/Xcode/DerivedData/Skip-Everything-aqywrhrzhkbvfseiqgxuufbdwdft/Build/Intermediates.noindex/Previews/HelloSkip/Products/Debug/skip gradle -p /opt/src/github/skiptools/skipapp-hello/Darwin/../Android launchDebug
Gradle FAILED:
Gradle run error: Optional(SkipDriveExternal.ProcessResult.ExitStatus.terminated(code: 1))
The issue is that the build script attempts to launch gradle to build and run the Android app, but since the output files are in an unexpected location (the preview-specific build output folder).
We shouldn't run the transpiler or try to launch gradle when building for previews. We can check for this with the ENABLE_PREVIEWS
environment variable, which is set to YES
when Xcode does the preview build.
This can be worked around by adding the following block to the beginning of the Run skip gradle
script phase of the Build Phases tab of the app project:
if [ "${ENABLE_PREVIEWS}" == "YES" ]; then
echo "note: skipping skip due to ENABLE_PREVIEWS"
exit 0
fi
Editing the script looks like this:
Once the script has been edited to disable the script phase when building for previews, then the SwiftUI preview views should be rendered as expected:
Why don't you add it within the Skip package itself ?
So we don't need to add the Script manually within every new app we created
Why don't you add it within the Skip package itself ? So we don't need to add the Script manually within every new app we created
We intend to do that as part of the fix.
We're happy to report that as of Skip 0.7.43, the preview process can now be run without interference by the Skip transpiler.