Skip to content

fix(prompt-editor): fix flushSync warning in PromptEditor #7832

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/prompt-editor/src/PromptEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ export const PromptEditor: FunctionComponent<Props> = ({
setEditorState(state: SerializedPromptEditorState): void {
const editor = editorRef.current
if (editor) {
editor.setEditorState(editor.parseEditorState(state.lexicalEditorState))
// Use requestAnimationFrame to ensure the editor state is set outside of React's render cycle
// This prevents the flushSync warning
requestAnimationFrame(() => {
editor.setEditorState(editor.parseEditorState(state.lexicalEditorState))
})
}
},
getSerializedValue(): SerializedPromptEditorValue {
Expand Down Expand Up @@ -325,7 +329,11 @@ export const PromptEditor: FunctionComponent<Props> = ({
const currentEditorState = normalizeEditorStateJSON(editor.getEditorState().toJSON())
const newEditorState = initialEditorState.lexicalEditorState
if (!isEqual(currentEditorState, newEditorState)) {
editor.setEditorState(editor.parseEditorState(newEditorState))
// Use requestAnimationFrame to ensure the editor state is set outside of React's render cycle
// NOTE: This addresses the flushSync warning.
requestAnimationFrame(() => {
editor.setEditorState(editor.parseEditorState(newEditorState))
})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion vscode/test/e2e/initial-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
await sidebarSignin(page, sidebar)
const chatFrame = getChatSidebarPanel(page)
const lastChatInput = getChatInputs(chatFrame).last()

await lastChatInput.click({ delay: 100 })
// The current repository should be initially present in the chat input.
await expect(chatInputMentions(lastChatInput)).toHaveText(['myrepo'])

Check failure on line 24 in vscode/test/e2e/initial-context.test.ts

View workflow job for this annotation

GitHub Actions / test-e2e (ubuntu, 5/5)

initial-context.test.ts:16:82 › initial context - self-serve repo

1) initial-context.test.ts:16:82 › initial context - self-serve repo ───────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: frameLocator('iframe.webview:first-child:last-child').frameLocator('iframe').getByRole('textbox', { name: 'Chat message' }).last().locator('.context-item-mention-node') - Expected - 3 + Received + 1 - Array [ - "myrepo", - ] + Array [] Call log: - expect.toHaveText with timeout 5000ms - waiting for frameLocator('iframe.webview:first-child:last-child').frameLocator('iframe').getByRole('textbox', { name: 'Chat message' }).last().locator('.context-item-mention-node') - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements 22 | await lastChatInput.click({ delay: 100 }) 23 | // The current repository should be initially present in the chat input. > 24 | await expect(chatInputMentions(lastChatInput)).toHaveText(['myrepo']) | ^ 25 | } 26 | ) 27 | at /home/runner/work/cody/cody/vscode/test/e2e/initial-context.test.ts:24:56

Check failure on line 24 in vscode/test/e2e/initial-context.test.ts

View workflow job for this annotation

GitHub Actions / test-e2e (ubuntu, 5/5)

initial-context.test.ts:16:82 › initial context - self-serve repo

1) initial-context.test.ts:16:82 › initial context - self-serve repo ───────────────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: Timed out 5000ms waiting for expect(locator).toHaveText(expected) Locator: frameLocator('iframe.webview:first-child:last-child').frameLocator('iframe').getByRole('textbox', { name: 'Chat message' }).last().locator('.context-item-mention-node') - Expected - 3 + Received + 1 - Array [ - "myrepo", - ] + Array [] Call log: - expect.toHaveText with timeout 5000ms - waiting for frameLocator('iframe.webview:first-child:last-child').frameLocator('iframe').getByRole('textbox', { name: 'Chat message' }).last().locator('.context-item-mention-node') - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements - locator resolved to 0 elements 22 | await lastChatInput.click({ delay: 100 }) 23 | // The current repository should be initially present in the chat input. > 24 | await expect(chatInputMentions(lastChatInput)).toHaveText(['myrepo']) | ^ 25 | } 26 | ) 27 | at /home/runner/work/cody/cody/vscode/test/e2e/initial-context.test.ts:24:56
}
)

Expand Down
17 changes: 16 additions & 1 deletion vscode/webviews/chat/Transcript.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ChatMessage, FIXTURE_MODELS, errorToChatError, ps } from '@sourcegraph/cody-shared'
import { fireEvent, getQueriesForElement, render as render_, screen } from '@testing-library/react'
import type { ComponentProps } from 'react'
import { describe, expect, test, vi } from 'vitest'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { URI } from 'vscode-uri'
import { AppWrapperForTest } from '../AppWrapperForTest'
import { MockNoGuardrails } from '../utils/guardrails'
Expand All @@ -20,6 +20,21 @@ const PROPS: Omit<ComponentProps<typeof Transcript>, 'transcript'> = {
guardrails: new MockNoGuardrails(),
}

// Mock requestAnimationFrame to execute callback immediately in test environment
const originalRAF = global.requestAnimationFrame
beforeEach(() => {
global.requestAnimationFrame = (cb: FrameRequestCallback): number => {
// Execute the callback immediately
cb(0)
return 0
}
})

afterEach(() => {
// Restore the original requestAnimationFrame
global.requestAnimationFrame = originalRAF
})

vi.mock('../utils/VSCodeApi', () => ({
getVSCodeAPI: vi.fn().mockReturnValue({
onMessage: () => {},
Expand Down
Loading