tinymist icon indicating copy to clipboard operation
tinymist copied to clipboard

fix: resolve DocumentDropOrPasteEditKind undefined error in older VS Code versions

Open Copilot opened this issue 7 months ago • 3 comments

Fixes a critical runtime error that prevented tinymist from loading in Cursor and other editors using older VS Code versions.

Problem

The extension was failing with TypeError: Cannot read properties of undefined (reading 'Empty') in drop-paste.def.ts when running on VS Code versions prior to 1.97.0 (including Cursor). This occurred because the code was directly accessing vscode.DocumentDropOrPasteEditKind.Empty at module import time, before any runtime compatibility checks could execute.

// This failed in older VS Code versions
export const typstPasteLinkEditKind = vscode.DocumentDropOrPasteEditKind.Empty.append("typst", "link");

Solution

Implemented lazy evaluation of DocumentDropOrPasteEditKind constants using getter functions:

// New approach with runtime detection
function hasDocumentDropOrPasteEditKind(): boolean {
  return typeof (vscode as any).DocumentDropOrPasteEditKind !== "undefined";
}

export function getTypstPasteLinkEditKind() {
  return getEditKinds().typstPasteLinkEditKind; // Returns null for older versions
}

Changes Made

  1. Lazy Evaluation: Converted immediate constant evaluation to lazy getter functions that check API availability at runtime
  2. Graceful Degradation: Return null values for edit kinds when the API is unavailable, allowing the existing paste API checks to work correctly
  3. Caching: Added a caching layer to avoid repeated computation of edit kinds
  4. Updated References: Modified all usage sites in drop-paste.ts to use the new getter functions

Result

  • ✅ Cursor and other older VS Code versions now load tinymist without errors
  • ✅ Full paste API functionality preserved for VS Code 1.97+
  • ✅ Graceful degradation for older versions (paste features disabled with warning)
  • ✅ No behavioral changes for modern VS Code installations

This enables Cursor users to access tinymist's language server features while maintaining full functionality for users with newer VS Code versions.

Fixes #1657.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot avatar Jul 09 '25 13:07 Copilot

Fix error in editors/vscode/src/features/drop-paste.def.ts:

TypeError: Cannot read properties of undefined (reading 'Empty')
TypeError: Cannot read properties of undefined (reading 'Empty')

Myriad-Dreamin avatar Jul 09 '25 15:07 Myriad-Dreamin

Fix error in editors/vscode/src/features/drop-paste.def.ts:

TypeError: Cannot read properties of undefined (reading 'Empty')
TypeError: Cannot read properties of undefined (reading 'Empty')

Myriad-Dreamin avatar Aug 03 '25 19:08 Myriad-Dreamin

Fix error in editors/vscode/src/features/drop-paste.def.ts:

TypeError: Cannot read properties of undefined (reading 'Empty')

...

Fixed the TypeError in drop-paste.def.ts by implementing lazy evaluation of DocumentDropOrPasteEditKind constants. The error was caused by accessing vscode.DocumentDropOrPasteEditKind.Empty when DocumentDropOrPasteEditKind is undefined in older VS Code versions like Cursor. The fix converts the problematic constants to getter functions that safely handle missing APIs. Commit: 9ce7a9b

Fix error in editors/vscode/src/features/drop-paste.def.ts:

TypeError: Cannot read properties of undefined (reading 'Empty')

...

Fixed the TypeError in drop-paste.def.ts by implementing lazy evaluation of DocumentDropOrPasteEditKind constants. The error occurred when accessing .Empty on undefined DocumentDropOrPasteEditKind in older VS Code versions. The solution uses getter functions that gracefully handle missing APIs. Commit: 9ce7a9b

Copilot avatar Aug 03 '25 19:08 Copilot