vscode-js-debug icon indicating copy to clipboard operation
vscode-js-debug copied to clipboard

Custom toString causes non observable side effects when debugging

Open robbiespeed opened this issue 1 year ago • 2 comments

Describe the bug If a object has a toString method that produces side effects, the debugger triggers those side effects in a non observable way.

Seems related to #1296

To Reproduce Place a breakpoint in the following code:

export class Foo {
  #counter = 0;
  get() {
    console.log("Some side effects happen here");
    // ^--- add breakpoint here
    return this.#counter += 1;
  }
  toString () {
    return String(this.get());
  }
}

const foo = new Foo();

foo.get();

By the time the debugger stops at the breakpoint (before log should run), one log has already happened and the counter has increased.

VS Code Version: 1.89.1

robbiespeed avatar May 18 '24 03:05 robbiespeed

Yea, this is as-designed, we assume that toString() does not have side effects.

connor4312 avatar May 18 '24 23:05 connor4312

That's not a great assumption to make for something that's not opt-in. It makes the VS Code debugger unusable for any library that doesn't have an entirely pure toString, like preact/signals where it calls the getter for the value prop which on Computed signals will automatically recompute any value the debugger comes across.

robbiespeed avatar May 19 '24 03:05 robbiespeed