From 0330854e4e01b7f1ba43cbe6e5534b041dc8c1a4 Mon Sep 17 00:00:00 2001 From: comfy-tag-bot Date: Tue, 22 Sep 2026 04:19:58 +0000 Subject: [PATCH] fix: avoid TDZ crash restoring saved multiline widget value (#18324) Backport of #18324 to core/1.53. setValue in useStringWidget.ts's addMultilineWidget() closed over the outer `widget` const to read widget.options, but node.addDOMWidget(...) can invoke setValue synchronously while restoring a previously saved widget value during workflow load, before that `const widget = node.addDOMWidget(...)` assignment completes. Reading widget.options at that point threw a ReferenceError (TDZ). Hoists the widget's options object into its own widgetOptions const, declared and fully initialized before addDOMWidget is called, and has setValue reference that instead of the outer widget binding. Fixes Sentry issue CLOUD-FRONTEND-STAGING-4M6. --- .../composables/useStringWidget.test.ts | 22 +++++++++++++++++++ .../widgets/composables/useStringWidget.ts | 13 ++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/renderer/extensions/vueNodes/widgets/composables/useStringWidget.test.ts b/src/renderer/extensions/vueNodes/widgets/composables/useStringWidget.test.ts index f3453659b08..f90748cf9e6 100644 --- a/src/renderer/extensions/vueNodes/widgets/composables/useStringWidget.test.ts +++ b/src/renderer/extensions/vueNodes/widgets/composables/useStringWidget.test.ts @@ -97,6 +97,28 @@ describe('useStringWidget (multiline)', () => { expect(entries.every((s) => s.type === 'customtext')).toBe(true) }) + it('does not throw when a saved value is restored synchronously during construction', () => { + // Mirrors LGraphNode.addCustomWidget, which restores a saved widget + // value (from a loaded workflow) by invoking `options.setValue` + // synchronously, from inside `node.addDOMWidget(...)`, before that + // call returns. + const node = createMockDOMWidgetNode({ + addDOMWidget: vi.fn( + ( + name: string, + type: string, + element: HTMLElement, + options?: { setValue?: (v: string) => void } + ) => { + options?.setValue?.('restored-value') + return { name, type, element, options: options ?? {}, value: '' } + } + ) + }) + + expect(() => createStringWidget(node)).not.toThrow() + }) + it('fires the widget callback on input', () => { const { inputEl, callback } = setup() inputEl.value = 'hello' diff --git a/src/renderer/extensions/vueNodes/widgets/composables/useStringWidget.ts b/src/renderer/extensions/vueNodes/widgets/composables/useStringWidget.ts index b939e93e616..e5b7fc26ad3 100644 --- a/src/renderer/extensions/vueNodes/widgets/composables/useStringWidget.ts +++ b/src/renderer/extensions/vueNodes/widgets/composables/useStringWidget.ts @@ -8,6 +8,7 @@ import { import { isStringInputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2' import type { InputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2' import { app } from '@/scripts/app' +import type { DOMWidgetOptions } from '@/scripts/domWidget' import type { ComfyWidgetConstructorV2 } from '@/scripts/widgets' import { useWidgetValueStore } from '@/stores/widgetValueStore' import { widgetId } from '@/types/widgetId' @@ -23,7 +24,11 @@ function addMultilineWidget( opts.placeholder || name ) - const widget = node.addDOMWidget(name, 'customtext', inputEl, { + // Declared separately (rather than reading `widget.options` from the + // outer closure below) because `addDOMWidget` can invoke `setValue` + // synchronously, while restoring a saved value, before the `widget` + // binding it returns is initialized. + const widgetOptions: DOMWidgetOptions = { getValue(): string { const graphId = resolveNodeRootGraphId(node, app.rootGraph.id) const widgetState = widgetStore.getWidget( @@ -44,10 +49,12 @@ function addMultilineWidget( widgetStore.registerWidget(id, { type: 'customtext', value: v, - options: widget.options ?? {} + options: widgetOptions }) } - }) + } + + const widget = node.addDOMWidget(name, 'customtext', inputEl, widgetOptions) widget.element = inputEl