Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): Version carrier rather than use SDK version #15132

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/core/src/carrier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { AsyncContextStack } from './asyncContext/stackStrategy';
import type { AsyncContextStrategy } from './asyncContext/types';
import type { Scope } from './scope';
import type { Logger } from './utils-hoist/logger';
import { SDK_VERSION } from './utils-hoist/version';
import { GLOBAL_OBJ } from './utils-hoist/worldwide';

/**
Expand All @@ -17,6 +16,11 @@ type VersionedCarrier = {
version?: string;
} & Record<Exclude<string, 'version'>, SentryCarrier>;

/**
* IMPORTANT - This must be updated if any breaking changes are made to the 'SentryCarrier' interface.
*/
export const CARRIER_VERSION = '9';

export interface SentryCarrier {
acs?: AsyncContextStrategy;
stack?: AsyncContextStack;
Expand All @@ -34,10 +38,6 @@ export interface SentryCarrier {

/**
* Returns the global shim registry.
*
* FIXME: This function is problematic, because despite always returning a valid Carrier,
* it has an optional `__SENTRY__` property, which then in turn requires us to always perform an unnecessary check
* at the call-site. We always access the carrier through this function, so we can guarantee that `__SENTRY__` is there.
**/
export function getMainCarrier(): Carrier {
// This ensures a Sentry carrier exists
Expand All @@ -50,11 +50,11 @@ export function getSentryCarrier(carrier: Carrier): SentryCarrier {
const __SENTRY__ = (carrier.__SENTRY__ = carrier.__SENTRY__ || {});

// For now: First SDK that sets the .version property wins
__SENTRY__.version = __SENTRY__.version || SDK_VERSION;
__SENTRY__.version = __SENTRY__.version || CARRIER_VERSION;

// Intentionally populating and returning the version of "this" SDK instance
// rather than what's set in .version so that "this" SDK always gets its carrier
return (__SENTRY__[SDK_VERSION] = __SENTRY__[SDK_VERSION] || {});
return (__SENTRY__[CARRIER_VERSION] = __SENTRY__[CARRIER_VERSION] || {});
}

/**
Expand All @@ -74,7 +74,7 @@ export function getGlobalSingleton<Prop extends keyof SentryCarrier>(
obj = GLOBAL_OBJ,
): NonNullable<SentryCarrier[Prop]> {
const __SENTRY__ = (obj.__SENTRY__ = obj.__SENTRY__ || {});
const carrier = (__SENTRY__[SDK_VERSION] = __SENTRY__[SDK_VERSION] || {});
const carrier = (__SENTRY__[CARRIER_VERSION] = __SENTRY__[CARRIER_VERSION] || {});
// Note: We do not want to set `carrier.version` here, as this may be called before any `init` is called, e.g. for the default scopes
return carrier[name] || (carrier[name] = creator());
}
17 changes: 9 additions & 8 deletions packages/core/test/lib/carrier.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getSentryCarrier } from '../../src/carrier';
import { SDK_VERSION } from '../../src/utils-hoist/version';
import { CARRIER_VERSION } from '../../src/carrier';

describe('getSentryCarrier', () => {
describe('base case (one SDK)', () => {
Expand All @@ -11,17 +11,17 @@ describe('getSentryCarrier', () => {

expect(globalObject).toEqual({
__SENTRY__: {
version: SDK_VERSION,
[SDK_VERSION]: {},
version: CARRIER_VERSION,
[CARRIER_VERSION]: {},
},
});
});

it('returns the existing sentry carrier object if it already exists', () => {
const originalGlobalObject = {
__SENTRY__: {
version: SDK_VERSION,
[SDK_VERSION]: {
version: CARRIER_VERSION,
[CARRIER_VERSION]: {
acs: {},
},
},
Expand All @@ -42,13 +42,14 @@ describe('getSentryCarrier', () => {
describe('multiple (older) SDKs', () => {
it("returns the version of the sentry carrier object of the SDK's version rather than the one set in .version", () => {
const sentryCarrier = getSentryCarrier({
// @ts-expect-error - this is just a test object
__SENTRY__: {
version: '8.0.0', // another SDK set this
'8.0.0': {
// @ts-expect-error - this is just a test object
stack: {},
},
[SDK_VERSION]: {
[CARRIER_VERSION]: {
// @ts-expect-error - this is just a test object
acs: {},
},
hub: {},
Expand Down Expand Up @@ -82,7 +83,7 @@ describe('getSentryCarrier', () => {
'8.0.0': {
acs: {},
},
[SDK_VERSION]: {},
[CARRIER_VERSION]: {},
},
});
});
Expand Down
Loading