Skip to content

Commit abf142f

Browse files
mydeaclaude
andauthored
feat(core)!: Remove scope.clear() method (#23230)
This was only used in tests, where we can better solve this by resetting the ACS totally. Will also deprecate this on v10. --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 16deb06 commit abf142f

60 files changed

Lines changed: 168 additions & 499 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

dev-packages/deno-integration-tests/src/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import type { TransactionEvent } from '@sentry/core';
2-
import { getCurrentScope, getGlobalScope, getIsolationScope } from '@sentry/deno';
2+
import { getAsyncContextStrategy, getMainCarrier, setAsyncContextStrategy } from '@sentry/core';
33

44
/**
5-
* Clear the current, isolation, and global scopes and detach the client so each
6-
* test starts from a clean slate.
5+
* Wipe the Sentry carrier so the current, isolation, and global scopes (and the
6+
* client) are recreated fresh, letting each test start from a clean slate.
7+
*
8+
* The async-context strategy is preserved across the wipe: channel integrations
9+
* subscribe once per process and capture the strategy's `AsyncLocalStorage` at
10+
* that point. Dropping it here would strand that ALS, so scope propagation into
11+
* channel callbacks would silently break for every test after the first.
712
*/
813
export function resetGlobals(): void {
9-
getCurrentScope().clear();
10-
getCurrentScope().setClient(undefined);
11-
getIsolationScope().clear();
12-
getGlobalScope().clear();
14+
const acs = getAsyncContextStrategy(getMainCarrier());
15+
getMainCarrier().__SENTRY__ = undefined;
16+
setAsyncContextStrategy(acs);
1317
}
1418

1519
export interface TransactionSink {

dev-packages/node-integration-tests/suites/public-api/configureScope/clear_scope/scenario.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

dev-packages/node-integration-tests/suites/public-api/configureScope/clear_scope/test.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

docs/migration/v11-end-state.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ Affected SDKs: `@sentry/cloudflare`.
664664
### `@sentry/core` / All SDKs
665665
666666
- The internal, deprecated `addAutoIpAddressToUser` export was removed.
667+
- `Scope.clear()` was removed. To reset scope state, re-initialize the SDK or run your code in a fresh scope via `withScope`/`withIsolationScope`.
667668
- The deprecated positional `spanOrigin` argument of `instrumentFetchRequest` was removed. Pass an options object (e.g. `{ spanOrigin }`) as the last argument instead.
668669
- The `createSpanEnvelope` function and the `SpanEnvelope` / `SpanItem` types were removed. They existed only to send standalone (v1) spans as their own segment envelope, which the SDK no longer does. Standalone spans are gone; spans are sent either on their transaction or, with span streaming, as streamed spans (`StreamedSpanEnvelope`).
669670
- The `disableInstrumentationWarnings` option and the `MissingInstrumentationContext` type were removed. Now that instrumentation is channel-based, the SDK can no longer detect the "you imported a framework before `Sentry.init()`" case, so the warning it gated and the context it attached no longer exist.

packages/astro/test/client/sdk.test.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import type { BrowserClient } from '@sentry/browser';
2-
import {
3-
browserTracingIntegration,
4-
getActiveSpan,
5-
getClient,
6-
getCurrentScope,
7-
getGlobalScope,
8-
getIsolationScope,
9-
SDK_VERSION,
10-
} from '@sentry/browser';
2+
import { browserTracingIntegration, getActiveSpan, getClient, SDK_VERSION } from '@sentry/browser';
3+
import { getMainCarrier } from '@sentry/core';
114
import * as SentryBrowser from '@sentry/browser';
125
import { afterEach, describe, expect, it, vi } from 'vitest';
136
import { init } from '../../src/client/sdk';
@@ -19,10 +12,7 @@ describe('Sentry client SDK', () => {
1912
afterEach(() => {
2013
vi.clearAllMocks();
2114

22-
getCurrentScope().clear();
23-
getCurrentScope().setClient(undefined);
24-
getIsolationScope().clear();
25-
getGlobalScope().clear();
15+
getMainCarrier().__SENTRY__ = undefined;
2616
});
2717

2818
it('adds Astro metadata to the SDK options', () => {

packages/astro/test/server/sdk.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getMainCarrier } from '@sentry/core';
12
import * as SentryNode from '@sentry/node';
23
import { SDK_VERSION } from '@sentry/node';
34
import { afterEach, describe, expect, it, vi } from 'vitest';
@@ -10,10 +11,7 @@ describe('Sentry server SDK', () => {
1011
afterEach(() => {
1112
vi.clearAllMocks();
1213

13-
SentryNode.getGlobalScope().clear();
14-
SentryNode.getIsolationScope().clear();
15-
SentryNode.getCurrentScope().clear();
16-
SentryNode.getCurrentScope().setClient(undefined);
14+
getMainCarrier().__SENTRY__ = undefined;
1715
});
1816

1917
it('adds Astro metadata to the SDK options', () => {

packages/browser-utils/test/browser/utils.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { getCurrentScope, getIsolationScope, SentrySpan, setCurrentClient, spanToJSON } from '@sentry/core';
1+
import { getMainCarrier, SentrySpan, setCurrentClient, spanToJSON } from '@sentry/core';
22
import { beforeEach, describe, expect, it, test } from 'vitest';
33
import { extractNetworkProtocol, startAndEndSpan } from '../../src/performance/utils';
44
import { getDefaultClientOptions, TestClient } from '../utils/TestClient';
55

66
describe('startAndEndSpan()', () => {
77
beforeEach(() => {
8-
getCurrentScope().clear();
9-
getIsolationScope().clear();
8+
getMainCarrier().__SENTRY__ = undefined;
109

1110
const client = new TestClient(
1211
getDefaultClientOptions({

packages/browser-utils/test/performance/browserMetrics.test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { Span, SpanAttributes } from '@sentry/core';
22
import {
33
getClient,
4-
getCurrentScope,
5-
getIsolationScope,
4+
getMainCarrier,
65
SEMANTIC_ATTRIBUTE_SENTRY_OP,
76
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
87
SentrySpan,
@@ -46,8 +45,7 @@ function mockPerformanceResourceTiming(
4645
describe('addWebVitalsToSpan', () => {
4746
beforeEach(() => {
4847
vi.restoreAllMocks();
49-
getCurrentScope().clear();
50-
getIsolationScope().clear();
48+
getMainCarrier().__SENTRY__ = undefined;
5149

5250
const client = new TestClient(
5351
getDefaultClientOptions({
@@ -151,8 +149,7 @@ describe('_addResourceSpans', () => {
151149
});
152150

153151
beforeEach(() => {
154-
getCurrentScope().clear();
155-
getIsolationScope().clear();
152+
getMainCarrier().__SENTRY__ = undefined;
156153

157154
const client = new TestClient(
158155
getDefaultClientOptions({
@@ -590,8 +587,7 @@ describe('_addNavigationSpans', () => {
590587
});
591588

592589
beforeEach(() => {
593-
getCurrentScope().clear();
594-
getIsolationScope().clear();
590+
getMainCarrier().__SENTRY__ = undefined;
595591

596592
const client = new TestClient(
597593
getDefaultClientOptions({

packages/browser-utils/test/performance/userTiming.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Span } from '@sentry/core';
2-
import { getCurrentScope, getIsolationScope, SentrySpan, setCurrentClient, spanToJSON } from '@sentry/core';
2+
import { getMainCarrier, SentrySpan, setCurrentClient, spanToJSON } from '@sentry/core';
33
import { beforeEach, describe, expect, it, vi } from 'vitest';
44
import { _addUserTimingSpan, userTimingIntegration } from '../../src/performance/userTiming';
55
import * as utils from '../../src/performance/utils';
@@ -12,8 +12,7 @@ describe('userTimingIntegration', () => {
1212

1313
beforeEach(() => {
1414
vi.restoreAllMocks();
15-
getCurrentScope().clear();
16-
getIsolationScope().clear();
15+
getMainCarrier().__SENTRY__ = undefined;
1716

1817
client = new TestClient(getDefaultClientOptions({ tracesSampleRate: 1 }));
1918
setCurrentClient(client);
@@ -133,8 +132,7 @@ describe('_addUserTimingSpan', () => {
133132

134133
beforeEach(() => {
135134
vi.restoreAllMocks();
136-
getCurrentScope().clear();
137-
getIsolationScope().clear();
135+
getMainCarrier().__SENTRY__ = undefined;
138136

139137
const client = new TestClient(getDefaultClientOptions({ tracesSampleRate: 1 }));
140138
setCurrentClient(client);

packages/browser/test/index.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
import {
66
eventFiltersIntegration,
7-
getGlobalScope,
8-
getIsolationScope,
7+
getMainCarrier,
98
getReportDialogEndpoint,
109
lastEventId,
1110
SDK_VERSION,
@@ -49,10 +48,7 @@ describe('SentryBrowser', () => {
4948
const beforeSend = vi.fn(event => event);
5049

5150
beforeEach(() => {
52-
getGlobalScope().clear();
53-
getIsolationScope().clear();
54-
getCurrentScope().clear();
55-
getCurrentScope().setClient(undefined);
51+
getMainCarrier().__SENTRY__ = undefined;
5652

5753
init({
5854
beforeSend,

0 commit comments

Comments
 (0)