Skip to content

Commit

Permalink
fix: Don't start the SDK inside Xcode preview (#4601)
Browse files Browse the repository at this point in the history
The SDK was running inside Xcode preview for SwiftUI and slowing things down for development
  • Loading branch information
brustolin authored Jan 20, 2025
1 parent 825f0cb commit f59d0bf
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 6 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

## Unreleased

### Fixes

- Don't start the SDK inside Xcode preview (#4601)

### Improvements

- Add native SDK information in the replay option event (#4663)
- Add error logging for invalid `cacheDirectoryPath` (#4693)

### Features

Expand All @@ -13,9 +18,6 @@
- Allow hybrid SDK to set replay options tags information (#4710)
- Add threshold to always log fatal logs (#4707)

### Improvements

- Add error logging for invalid `cacheDirectoryPath` (#4693)
### Internal

- Change macros TEST and TESTCI to SENTRY_TEST and SENTRY_TEST_CI (#4712)
Expand Down
4 changes: 2 additions & 2 deletions Samples/iOS-SwiftUI/iOS-SwiftUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@
};
D833D61B2D13216300961E7A /* libSentrySwiftUITests.a */ = {
isa = PBXReferenceProxy;
fileType = archive.ar;
path = libSentrySwiftUITests.a;
fileType = wrapper.cfbundle;
path = SentrySwiftUITests.xctest;
remoteRef = D833D61A2D13216300961E7A /* PBXContainerItemProxy */;
sourceTree = BUILT_PRODUCTS_DIR;
};
Expand Down
5 changes: 5 additions & 0 deletions SentryTestUtils/TestSentryNSProcessInfoWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class TestSentryNSProcessInfoWrapper: SentryNSProcessInfoWrapper {
public var processorCount: UInt?
public var processDirectoryPath: String?
public var thermalState: ProcessInfo.ThermalState?
public var environment: [String: String]?
}

public var overrides = Override()
Expand All @@ -20,4 +21,8 @@ public class TestSentryNSProcessInfoWrapper: SentryNSProcessInfoWrapper {
public override var thermalState: ProcessInfo.ThermalState {
overrides.thermalState ?? super.thermalState
}

public override var environment: [String: String] {
overrides.environment ?? super.environment
}
}
5 changes: 5 additions & 0 deletions Sources/Sentry/SentryNSProcessInfoWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,9 @@ - (NSProcessInfoThermalState)thermalState
return NSProcessInfo.processInfo.thermalState;
}

- (NSDictionary<NSString *, NSString *> *)environment
{
return NSProcessInfo.processInfo.environment;
}

@end
11 changes: 10 additions & 1 deletion Sources/Sentry/SentrySDK.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#import "SentryLog.h"
#import "SentryLogC.h"
#import "SentryMeta.h"
#import "SentryNSProcessInfoWrapper.h"
#import "SentryOptions+Private.h"
#import "SentryProfilingConditionals.h"
#import "SentryReplayApi.h"
Expand All @@ -39,6 +40,8 @@
# import "SentryProfiler+Private.h"
#endif // SENTRY_TARGET_PROFILING_SUPPORTED

NSString *const SENTRY_XCODE_PREVIEW_ENVIRONMENT_KEY = @"XCODE_RUNNING_FOR_PREVIEWS";

@interface SentrySDK ()

@property (class) SentryHub *currentHub;
Expand All @@ -47,7 +50,6 @@ @interface SentrySDK ()

NS_ASSUME_NONNULL_BEGIN
@implementation SentrySDK

static SentryHub *_Nullable currentHub;
static NSObject *currentHubLock;
static BOOL crashedLastRunCalled;
Expand Down Expand Up @@ -201,6 +203,13 @@ + (void)setStartTimestamp:(NSDate *)value

+ (void)startWithOptions:(SentryOptions *)options
{
if ([SentryDependencyContainer.sharedInstance.processInfoWrapper
.environment[SENTRY_XCODE_PREVIEW_ENVIRONMENT_KEY] isEqualToString:@"1"]) {
// Using NSLog because SentryLog was not initialized yet.
NSLog(@"[SENTRY] [WARNING] SentrySDK not started. Running from Xcode preview.");
return;
}

startOption = options;
[SentryLog configure:options.debug diagnosticLevel:options.diagnosticLevel];

Expand Down
1 change: 1 addition & 0 deletions Sources/Sentry/include/SentryNSProcessInfoWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nullable, nonatomic, readonly) NSString *processPath;
@property (readonly) NSUInteger processorCount;
@property (readonly) NSProcessInfoThermalState thermalState;
@property (readonly) NSDictionary<NSString *, NSString *> *environment;

#if defined(SENTRY_TEST) || defined(SENTRY_TEST_CI) || defined(DEBUG)
- (void)setProcessPath:(NSString *)path;
Expand Down
13 changes: 13 additions & 0 deletions Tests/SentryTests/SentrySDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ class SentrySDKTests: XCTestCase {
XCTAssertIdentical(scope, SentrySDK.currentHub().scope)
}

func testDontStartInsideXcodePreview() {
let testProcessInfoWrapper = TestSentryNSProcessInfoWrapper()
testProcessInfoWrapper.overrides.environment = ["XCODE_RUNNING_FOR_PREVIEWS": "1"]

SentryDependencyContainer.sharedInstance().processInfoWrapper = testProcessInfoWrapper

SentrySDK.start { options in
options.debug = true
}

XCTAssertFalse(SentrySDK.isEnabled)
}

func testCrashedLastRun() {
XCTAssertEqual(SentryDependencyContainer.sharedInstance().crashReporter.crashedLastLaunch, SentrySDK.crashedLastRun)
}
Expand Down

0 comments on commit f59d0bf

Please sign in to comment.