Skip to content

ref(core): Replace Scope.clone() with non-static scope.clone() #9801

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

Merged
merged 2 commits into from
Dec 12, 2023

Conversation

mydea
Copy link
Member

@mydea mydea commented Dec 12, 2023

To avoid this static method there. It is deprecated to use Scope.clone() (but still works), but better to just use scope.clone() or new Scope() directly.

@mydea mydea requested review from lforst and AbhiPrasad December 12, 2023 13:24
@mydea mydea self-assigned this Dec 12, 2023
Copy link
Contributor

github-actions bot commented Dec 12, 2023

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay, Feedback) - Webpack (gzipped) 74.7 KB (-0.04% 🔽)
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 66.07 KB (-0.02% 🔽)
@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped) 59.68 KB (+0.01% 🔺)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.13 KB (+0.01% 🔺)
@sentry/browser (incl. Feedback) - Webpack (gzipped) 29.74 KB (+0.02% 🔺)
@sentry/browser - Webpack (gzipped) 21.39 KB (+0.03% 🔺)
@sentry/browser (incl. Tracing, Replay, Feedback) - ES6 CDN Bundle (gzipped) 71.31 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 63.04 KB (+0.02% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 29.66 KB (+0.04% 🔺)
@sentry/browser - ES6 CDN Bundle (gzipped) 21.71 KB (+0.03% 🔺)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 198.61 KB (+0.06% 🔺)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 89.89 KB (+0.13% 🔺)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 64.86 KB (+0.17% 🔺)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 32.33 KB (+0.08% 🔺)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 66.44 KB (+0.01% 🔺)
@sentry/react - Webpack (gzipped) 21.44 KB (+0.02% 🔺)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 83.18 KB (+0.01% 🔺)
@sentry/nextjs Client - Webpack (gzipped) 48.23 KB (+0.01% 🔺)
@sentry-internal/feedback - Webpack (gzipped) 16.19 KB (+0.04% 🔺)

@mydea mydea merged commit 2b4d56b into develop Dec 12, 2023
@mydea mydea deleted the fn/avoid-scope-clone branch December 12, 2023 16:14
@timfish
Copy link
Collaborator

timfish commented Dec 13, 2023

We used Scope.clone() in the Electron SDK to rehydrate scope either sent as JSON via IPC or saved as JSON to file.

Not sure what the best solution is.

@mydea
Copy link
Member Author

mydea commented Dec 13, 2023

So for now Scope.clone() is still around (but deprecated). but you can easily replace it with:

function cloneScope(scope?: Scope): Scope {
  return scope ? scope.clone() : new Scope();
}

Or what do you mean?

@timfish
Copy link
Collaborator

timfish commented Dec 13, 2023

The scope we have has come from JSON.parse so there are no methods on it so scope.clone() will fail.

The previous clone copied all the properties off the supplied scope:

  public static clone(scope?: Scope): Scope {
    const newScope = new Scope();
    if (scope) {
      newScope._breadcrumbs = [...scope._breadcrumbs];
      newScope._tags = { ...scope._tags };
      newScope._extra = { ...scope._extra };
      newScope._contexts = { ...scope._contexts };

@timfish
Copy link
Collaborator

timfish commented Dec 13, 2023

We can fudge this with types in the Electron SDK but my concern would be that if any private properties in Scope are changed, things will break.

@mydea
Copy link
Member Author

mydea commented Dec 14, 2023

Ahh, I see, so you're basically duck-typing scope and not really passing a proper Scope class to Scope.clone(json), do I understand that correctly? 🤔

IMHO maybe the better solution for this is to have something like createScopeFromJson() (or something like that) utility that actually takes an object there. Or more specifically, we can maybe eventually, when we go forward with #9799 for all SDKs, do this with ScopeData, which is basically a representation of the scope data and should be safe to re-create a scope from, more or less?

@timfish
Copy link
Collaborator

timfish commented Dec 14, 2023

not really passing a proper Scope class to Scope.clone(json)

Yep!

the better solution for this is to have something like createScopeFromJson()

That would work. Want me to do the PR?

@mydea
Copy link
Member Author

mydea commented Dec 14, 2023

not really passing a proper Scope class to Scope.clone(json)

Yep!

the better solution for this is to have something like createScopeFromJson()

That would work. Want me to do the PR?

I think I'd hold off on that until we have the scope changes done, as this is a bit in flux right now! This should happen very soon though, I'll ping you then! :)

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
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.

4 participants