Skip to content

feat: add source name logging to $inspect.trace #16060

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 55 commits into from
Jun 10, 2025

Conversation

Ocean-OS
Copy link
Contributor

@Ocean-OS Ocean-OS commented Jun 2, 2025

This adds name logging to $inspect.trace. Currently, $inspect.trace logs the type of dependency ($state or $derived), its current value, and a few stack traces related to it. In this PR, to better understand your dependencies, it includes the name of each dependency— if it's a variable, it'll log its name, and if it's an object property, it'll log the path (eg object.foo.bar.baz[0]).

Probably closes #15954

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Jun 2, 2025

🦋 Changeset detected

Latest commit: 4047913

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Jun 2, 2025

Playground

pnpm add https://pkg.pr.new/svelte@16060

@Rich-Harris
Copy link
Member

What does PROXY_REMOVE_PATH do? No tests fail if I remove it altogether

@Rich-Harris
Copy link
Member

I removed the tag_if_necessary stuff, because remnants of wrapper functions like that can end up in prod bundles (optimizers are rightly reluctant to remove unused arguments, because they affect fn.length), and because optimizers will occasionally do strange things with wrapper functions (terser/terser#1617)

@Rich-Harris
Copy link
Member

It does the [$state proxy] tag when a proxy is shared by two sources

Is that desirable? In the very rare case that two sources share a single proxy, replacing foo.n with [$state proxy].n just makes it harder to see that either thing is being mutated. I think we'd be better off just leaving the path intact. The worst case scenario is that someone who doesn't realise that bar === foo is momentarily confused to see foo.n show up as having changed in response to a bar.n update, but [$state.proxy] doesn't make that any clearer — either way you would need to look at the UpdatedAtError trace to understand where the change originated.

If we get rid of PROXY_REMOVE_PATH we can simplify things a bit: #16126

* remove PROXY_REMOVE_PATH

* simplify a bit

* simplify

* tweak

* tweak implementation

* tweak implementation

* tweak implementation

* hoist

* tweak

* fix

* WIP (reduce number of with_parent calls, move towards possibility of combining tag and tag_proxy)

* DRY out
@Rich-Harris
Copy link
Member

This is a huge improvement! Makes $inspect.trace() so much more useful. Spotted some gibberish code in the existing trace implementation but was already getting carried away with the PR, so will address it in a follow-up.

Also, I broke the tests when I swapped the labels around — fixing

@Rich-Harris Rich-Harris merged commit 91272d7 into main Jun 10, 2025
14 checks passed
@Rich-Harris Rich-Harris deleted the $inspect.trace-source-tagging branch June 10, 2025 23:23
@github-actions github-actions bot mentioned this pull request Jun 10, 2025
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.

Confusing output by $inspect.trace
2 participants