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): Fix scope capturing via captureContext function #10735

Merged
merged 1 commit into from
Feb 20, 2024

Conversation

mydea
Copy link
Member

@mydea mydea commented Feb 20, 2024

In #9801, we introduced a regression that if you pass a function as captureContext to capture methods, the returned scope is not used.

The cause for this was a confusion on my end, based on the slightly weird way this works in scope.update(fn) - we don't actually merge this or update the scope based on the return value of fn, but fn receives the scope as argument, does nothing with the return type of fn and just returns it - which we didn't use, because I assumed that scope.update would actually return the scope (also, the return type of it is this which is not correct there).

This PR changes this so that the returned scope of fn is actually merged with the scope, same as if you'd pass a scope directly - so this is fundamentally the same now:

const otherScope = new Scope();
scope.update(otherScope);
scope.update(() => otherScope);

(which before would have had vastly different outcomes!)

I added a bunch of tests to verify how this works/should work.

Fixes #10686

@mydea mydea self-assigned this Feb 20, 2024
Copy link
Contributor

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay, Feedback) - Webpack (gzipped) 77.84 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 69.1 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing, Replay with Canvas) - Webpack (gzipped) 73.02 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped) 62.67 KB (+0.03% 🔺)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 33.28 KB (+0.05% 🔺)
@sentry/browser (incl. browserTracingIntegration) - Webpack (gzipped) 33.2 KB (+0.05% 🔺)
@sentry/browser (incl. Feedback) - Webpack (gzipped) 31.18 KB (+0.06% 🔺)
@sentry/browser (incl. sendFeedback) - Webpack (gzipped) 31.18 KB (+0.07% 🔺)
@sentry/browser - Webpack (gzipped) 22.43 KB (+0.07% 🔺)
@sentry/browser (incl. Tracing, Replay, Feedback) - ES6 CDN Bundle (gzipped) 76.2 KB (+0.01% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 67.7 KB (0%)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 33.62 KB (+0.03% 🔺)
@sentry/browser - ES6 CDN Bundle (gzipped) 24.76 KB (+0.12% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 213.11 KB (+0.07% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 101.74 KB (+0.13% 🔺)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 74.23 KB (+0.18% 🔺)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 36.9 KB (+0.1% 🔺)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 69.43 KB (+0.03% 🔺)
@sentry/react - Webpack (gzipped) 22.46 KB (+0.07% 🔺)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 86.89 KB (+0.02% 🔺)
@sentry/nextjs Client - Webpack (gzipped) 50.1 KB (+0.03% 🔺)
@sentry-internal/feedback - Webpack (gzipped) 17.15 KB (+0.11% 🔺)

@mydea mydea merged commit 97cc190 into develop Feb 20, 2024
97 checks passed
@mydea mydea deleted the fn/fix-scope-captureContext-fn branch February 20, 2024 09:20
mydea added a commit that referenced this pull request Feb 20, 2024
In #9801, we
introduced a regression that if you pass a function as `captureContext`
to capture methods, the returned scope is not used.

The cause for this was a confusion on my end, based on the slightly
weird way this works in `scope.update(fn)` - we don't actually merge
this or update the scope based on the return value of `fn`, but `fn`
receives the `scope` as argument, does nothing with the return type of
`fn` and just returns it - which we didn't use, because I assumed that
`scope.update` would actually return the scope (also, the return type of
it is `this` which is not correct there).

This PR changes this so that the returned scope of `fn` is actually
merged with the scope, same as if you'd pass a `scope` directly - so
this is fundamentally the same now:

```js
const otherScope = new Scope();
scope.update(otherScope);
scope.update(() => otherScope);
```

(which before would have had vastly different outcomes!)

I added a bunch of tests to verify how this works/should work.

Fixes #10686
mydea added a commit that referenced this pull request Feb 20, 2024
…#10737)

Backport to v7.

In #9801, we
introduced a regression that if you pass a function as `captureContext`
to capture methods, the returned scope is not used.

The cause for this was a confusion on my end, based on the slightly
weird way this works in `scope.update(fn)` - we don't actually merge
this or update the scope based on the return value of `fn`, but `fn`
receives the `scope` as argument, does nothing with the return type of
`fn` and just returns it - which we didn't use, because I assumed that
`scope.update` would actually return the scope (also, the return type of
it is `this` which is not correct there).

This PR changes this so that the returned scope of `fn` is actually
merged with the scope, same as if you'd pass a `scope` directly - so
this is fundamentally the same now:

```js
const otherScope = new Scope();
scope.update(otherScope);
scope.update(() => otherScope);
```

(which before would have had vastly different outcomes!)

I added a bunch of tests to verify how this works/should work.

Fixes #10686
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Passing () => scope as captureContext does not work
2 participants