From 6e733a460f456b97ebb85c9fd4a7af90ff196ffd Mon Sep 17 00:00:00 2001 From: comfy-tag-bot Date: Tue, 22 Sep 2026 04:26:35 +0000 Subject: [PATCH] fix: avoid TDZ crash restoring saved multiline widget value (#18324) Manual backport of #18324 to cloud/1.54 (the automated backport bot errored out before reaching this target). 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 value during workflow load, before the const widget = node.addDOMWidget(...) assignment completes. Reading widget.options at that point threw a ReferenceError from the temporal dead zone. The fix 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 07731f18ec7..e9d5640e946 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( @@ -46,10 +51,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