[Runtime crash] running test swift project
I have a full swift app, with some unit tests. Here my BUCK file. It works fine for building and running the app on the simulator. But I got a runtime crash when I want to run the tests.
buck test :chatApp
apple_library(
name = 'SnapKit',
visibility = ['PUBLIC'],
srcs = glob([
'Pods/SnapKit/**/*.swift',
]),
frameworks = [
'$SDKROOT/System/Library/Frameworks/UIKit.framework',
'$SDKROOT/System/Library/Frameworks/Foundation.framework'
],
)
apple_asset_catalog(
name = 'GRPCAssets',
app_icon = 'AppIcon',
dirs = ['grpc-go-chat/Assets.xcassets'],
)
apple_resource(
name = 'grpc-go-chatAppResources',
files = glob(['grpc-go-chat/*.png', 'grpc-go-chat/**/*.storyboard']),
)
apple_bundle(
name = 'chatApp',
binary = ':grpc-go-chatAppBinary',
extension = 'app',
info_plist = 'grpc-go-chat/Info.plist',
tests = [':appTests'],
deps = [':SnapKit'],
)
apple_binary(
name = 'grpc-go-chatAppBinary',
deps = [':grpc-go-chatAppResources', ':SnapKit'],
srcs = glob([
'grpc-go-chat/**/*.swift'
]),
frameworks = [
'$SDKROOT/System/Library/Frameworks/UIKit.framework',
'$SDKROOT/System/Library/Frameworks/Foundation.framework',
]
)
apple_package(
name = 'grpc-go-chatAppPackage',
bundle = ':chatApp',
)
apple_test(
name = 'appTests',
test_host_app = ':chatApp',
info_plist = 'grpc-go-chatTests/Info.plist',
srcs = [
'grpc-go-chatTests/grpc_go_chatTests.swift'
],
frameworks = [
'$SDKROOT/System/Library/Frameworks/Foundation.framework',
'$SDKROOT/System/Library/Frameworks/UIKit.framework',
'$PLATFORM_DIR/Developer/Library/Frameworks/XCTest.framework',
],
)
Here the error I got :
Building... 0.4 sec
[2017-12-18 16:21:26.111][error][command:null][tid:465][com.facebook.buck.cli.Main] Uncaught exception at top level
java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Optional.<init>(Optional.java:96)
at java.util.Optional.of(Optional.java:108)
at com.facebook.buck.apple.AppleTestDescription.createTestHostInfo(AppleTestDescription.java:496)
at com.facebook.buck.apple.AppleTestDescription.createBuildRule(AppleTestDescription.java:233)
at com.facebook.buck.apple.AppleTestDescription.createBuildRule(AppleTestDescription.java:84)
at com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer.transform(DefaultTargetNodeToBuildRuleTransformer.java:88)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.lambda$requireRule$0(SingleThreadedBuildRuleResolver.java:111)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.computeIfAbsent(SingleThreadedBuildRuleResolver.java:84)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.requireRule(SingleThreadedBuildRuleResolver.java:107)
at com.facebook.buck.swift.SwiftLibraryDescription.createCompanionBuildRule(SwiftLibraryDescription.java:391)
at com.facebook.buck.apple.AppleLibraryDescription.lambda$requireSingleArchUnstrippedBuildRule$8(AppleLibraryDescription.java:587)
at java.util.Optional.flatMap(Optional.java:241)
at com.facebook.buck.apple.AppleLibraryDescription.requireSingleArchUnstrippedBuildRule(AppleLibraryDescription.java:585)
at com.facebook.buck.apple.AppleLibraryDescription.requireUnstrippedBuildRule(AppleLibraryDescription.java:546)
at com.facebook.buck.apple.AppleLibraryDescription.createLibraryBuildRule(AppleLibraryDescription.java:441)
at com.facebook.buck.apple.AppleTestDescription.createTestLibraryRule(AppleTestDescription.java:411)
at com.facebook.buck.apple.AppleTestDescription.createBuildRule(AppleTestDescription.java:251)
at com.facebook.buck.apple.AppleTestDescription.createBuildRule(AppleTestDescription.java:84)
at com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer.transform(DefaultTargetNodeToBuildRuleTransformer.java:88)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.lambda$requireRule$0(SingleThreadedBuildRuleResolver.java:111)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.computeIfAbsent(SingleThreadedBuildRuleResolver.java:84)
at com.facebook.buck.rules.SingleThreadedBuildRuleResolver.requireRule(SingleThreadedBuildRuleResolver.java:107)
at com.facebook.buck.rules.ActionGraphCache$2.visit(ActionGraphCache.java:330)
at com.facebook.buck.rules.ActionGraphCache$2.visit(ActionGraphCache.java:327)
at com.facebook.buck.graph.AbstractBottomUpTraversal.traverse(AbstractBottomUpTraversal.java:36)
at com.facebook.buck.rules.ActionGraphCache.createActionGraphSerially(ActionGraphCache.java:332)
at com.facebook.buck.rules.ActionGraphCache.createActionGraph(ActionGraphCache.java:264)
at com.facebook.buck.rules.ActionGraphCache.getActionGraph(ActionGraphCache.java:167)
at com.facebook.buck.rules.ActionGraphCache.getActionGraph(ActionGraphCache.java:68)
at com.facebook.buck.cli.TestCommand.runWithoutHelp(TestCommand.java:549)
at com.facebook.buck.cli.AbstractCommand.run(AbstractCommand.java:232)
at com.facebook.buck.cli.AbstractContainerCommand.run(AbstractContainerCommand.java:79)
at com.facebook.buck.cli.BuckCommand.run(BuckCommand.java:82)
at com.facebook.buck.cli.Main.runMainWithExitCode(Main.java:1101)
at com.facebook.buck.cli.Main.runMainThenExit(Main.java:395)
at com.facebook.buck.cli.Main.nailMain(Main.java:1855)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.martiansoftware.nailgun.NGSession.run(NGSession.java:329)
In my tests I need to import my app to test the API like this:
@testable import grpc_go_chat
Anyone got the chance to make it works with tests? I am maybe doing something wrong, but impossible to fix it.
I am facing the same right now and really upset about it... :(
So, for now I've narrowed down to the case that my tests always fail on rule with apple-test-library flavor.
Caused by: java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at java.util.Optional.<init>(Optional.java:96)
at java.util.Optional.of(Optional.java:108)
at com.facebook.buck.apple.AppleTestDescription.createTestHostInfo(AppleTestDescription.java:863)
at com.facebook.buck.apple.AppleTestDescription.createBuildRule(AppleTestDescription.java:288)
at com.facebook.buck.apple.AppleTestDescription.createBuildRule(AppleTestDescription.java:117)
at com.facebook.buck.core.rules.transformer.impl.DefaultTargetNodeToBuildRuleTransformer.transform(DefaultTargetNodeToBuildRuleTransformer.java:116)
... 28 more
When creating rule //MyApp:MyBinary#apple-test-library,dwarf,iphonesimulator-x86_64,mach-o-bundle,no-linkermap,swift-companion.
I've debugged BUCK itself with the help of #2369 and as I see in my case it is SwiftLibrary object is passed into AppleTestDescription class as a binary build rule (what testHostWithTargetApp.getBinaryBuildRule() returns), and it does not have any output path.
When tried to .getSourcePathToOutput() on it, it throws back with return null from NoopBuildRuleWithDeclaredAndExtraDeps class, which finally results in NPE at line 863 in AppleTestDescription class.
And, for example, when there is CxxBinary instead of SwiftLibrary object on the same breakpoint, it has linkRule (CxxLink), which in turn has output path. And this is what finally gets into testHostAppBinarySourcePath and everything passes further just fine in that case.
Thus, at this point I'm still not sure whether it is an issue anywhere in my configs, or may be a bug here and it should accept null..?
I tend to think it is my project 😄 but anyways, where to look in it to try to fix this?
@v-jizhang , could you please have a glance at what I'm talking about, and maybe give me just a tiny hint which direction I should look first in order to identify the solution faster..? Huge thanks in advance!!
@RAM237, Sorry i'm not familiar with apple test but looks like AppleTestDescription expects an output path. So I'll suggest to look into how SwiftLibrary object is created and why it does not have an output path. Maybe it is related to project configurations. Thanks
@v-jizhang, I've figured out that this exception is thrown when I try to build apple_test with any Swift file included in srcs section. Everything builds well when there is only Obj-C sources are in the target. Seems like there is no path to output for a SwiftLibrary that is being built right after CxxBinary and crashes with NPE.
Removing test_host_app helps building the test target, but it crashes on running tests because there is no app where it should be run:
The bundle at ....#apple-test-bundle,dwarf,no-include-frameworks,no-linkermap/Tests.xctest does not contain an executable.
Here is the issue:

@pomozoff Thank you for looking into this issue. What you discovered "Seems like there is no path to output for a SwiftLibrary" raises a question, why? I think if we know the answer, we will be able to fix it.
@v-jizhang, that would be great!