microsoft / microsoft/monaco-editor

[Bug] `JSONDocument.getNodeFromOffset` is not a function at runtime

Open
#4,576 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
46.8k
Forks
4.1k
Avg merge
17h 58m
Merged PRs (30d)
1

Description

Reproducible in vscode.dev or in VS Code Desktop?
  • Not reproducible in vscode.dev or VS Code Desktop
Reproducible in the monaco editor playground?
Monaco Editor Playground Link

https://microsoft.github.io/monaco-editor/playground.html?source=v0.50.0#XQAAAAJQBQAAAAAAAABBqQkHQ5NjdMjwa-jY7SIQ9S7DNlzs5W-mwj0fe1ZCDRFc9ws9XQE0SJE1jc2VKxhaLFIw9vEWSxW3yscw83J8RgmSaumWUF-TBOsVJ4S4FwfCDQb5-2uUHXTVAefbVSd9IzaZ_vv9VR9KugsKiuGc2v5CrpC-28kzerv7FIINy73ctGSXrXyLSJzMzDkuKaa3tMg-UK4lhIYaP9NgpLNPfl7xF4BFMGqe-EJcVnNjGP6HeT9D9pWb93Wo4ZQgXuxOwtwin2u1xyz2Gx2slmhr8HXj7qdLwdlI27k25rc_v3AgJaKUIY-HvvR-F0sYfn_RN_g0QEv9MxSIEaFssFLtvkiFbazxmuPXPkHE0d_5xmqDVrtxkmN5uPUH9JDZF7Yd1uWIv8GMpep5xZVFvSw2-cIz0w28dQsPRy09JtpdmqY_n8RDaTrTESu5TYMgqSUFAK5NLKs_SnqA-TxUA5t2LxcbDcESk_xWw1TvxfXMdIYn2kDAktHQZpw_WFa0VsjWzI5VGXfodLhidItlPOQvpLzdH4Ubvi9ri3HxQM--aLPv4u380HU3Ut-LoNAsG1DNderr5zcICxXC9wfSh-3lGctsMGComlylPMCAu147wsBYYxTuk4rrCNAvpTYvdvkDtNYbuaz6tKxtSbM86ty-wmMi4B45WXyoykT741zQSpdgyfhF67hv6JUWVhA8cL88H1mCrWtUsyiLkYFp0dHOfje430WShbPDsEICuuNK4F_jM1Px4NL713M2FRudga9BWP5wCjpUHsE_dhg3U0HKbDsr-6QaXG-UGmWOcfuVFjEfOuM94FkVYH78P9jQRx7xr6bgf6XjoZiGKyFyKrxRVvkCwRJfIsplC3WSQAaGLXEEothGIRxm7NjXTdFCUwN1wUH_-Exwyg

Monaco Editor Playground Code
const value = /* set from `myEditor.getModel()`: */ `{
    
}`;

// Hover on each property to see its docs!
const myEditor = monaco.editor.create(document.getElementById("container"), {
	value,
	language: "json",
	automaticLayout: true,
});

// Register dynamic completions based on AST
monaco.languages.registerCompletionItemProvider('json', {
  provideCompletionItems: async (model, position, context) => {
    console.log(monaco.languages.json);

    const jsonWorker = await (
      await monaco.languages.json.getWorker()
    )(model.uri);
    const jsonDocument = await jsonWorker.parseJSONDocument(
      model.uri.toString()
    );
    console.log(jsonDocument);

    // This works just fine in Monaco Editor Playground
    const node = jsonDocument?.getNodeFromOffset(model.getOffsetAt(position));

    console.log(node);

    const word = model.getWordUntilPosition(position);

    return {
      suggestions: [
        {
          kind: monaco.languages.CompletionItemKind.Text,
          insertText: '"foo"',
          label: 'Foo',
          range: {
            startLineNumber: position.lineNumber,
            endLineNumber: position.lineNumber,
            startColumn: word.startColumn,
            endColumn: word.endColumn,
          },
        },
      ],
    };
  },
});
Reproduction Steps

I'm trying to create dynamic completions based on the JSON AST. For that I'm utilizing the functionality exposed with this PR: https://github.com/microsoft/monaco-editor/pull/4299, specifically JSONDocument.getNodeFromOffset. TypeScript tells me the method is there, but I'm getting a runtime error: Uncaught Error: jsonDocument?.getNodeFromOffset is not a function.

Reproduction environment with Vite: https://stackblitz.com/edit/vitejs-vite-bhgqgc?file=src%2Fmain.ts

Actual (Problematic) Behavior

Runtime error: Uncaught Error: jsonDocument?.getNodeFromOffset is not a function

Expected Behavior

jsonDocument?.getNodeFromOffset should work as it does in the Monaco Editor Playground.

Additional Context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Vite reproduction and compare its JSON worker behavior with the Monaco Editor Playground example at v0.50.0. Check why the runtime JSONDocument lacks getNodeFromOffset even though TypeScript exposes it; done means the documented call works without a runtime error in the reproduction.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.