monaco-editor
monaco-editor copied to clipboard
[Bug] Non reproducible error message: Cannot read properties of null (reading 'getText')
Reproducible in vscode.dev or in VS Code Desktop?
- [X] Not reproducible in vscode.dev or VS Code Desktop
Reproducible in the monaco editor playground?
- [X] Not reproducible in the monaco editor playground
Monaco Editor Playground Code
No response
Actual Behavior
No response
Expected Behavior
No response
Additional Context
I have a React application in which Monaco is used as a JSON editor (language="json"
). In rare and not reproducible cases, Monaco throws an error during E2E tests in Cypress, when accessing a page which a includes such an editor - without the tests actually manipulating any input in the Monaco JSON editor.
The error I get is:
TypeError: Cannot read properties of null (reading 'getText')
at Object.t.getFoldingRanges (../../../vs/language/json/jsonWorker.js:8:144449)
at eval (../../../vs/language/json/jsonWorker.js:8:156939)
at eval (../../../vs/language/json/jsonWorker.js:8:153259)
at Object.eval [as next] (../../../vs/language/json/jsonWorker.js:8:153364)
at eval (../../../vs/language/json/jsonWorker.js:8:152266)
at new Promise (<anonymous>)
at __awaiter (../../../vs/language/json/jsonWorker.js:8:152011)
at e.getFoldingRanges (../../../vs/language/json/jsonWorker.js:8:156795)
at h.fmr (http://localhost:9001/node_modules/monaco-editor/min/vs/base/worker/workerMain.js#json:13:119046)
at l._handleMessage (http://localhost:9001/node_modules/monaco-editor/min/vs/base/worker/workerMain.js#json:13:67446)
I know that the information I provide here is scare and probably not sufficient for debugging, but wanted it to mention nevertheless. I would be, of course, thankful for any hints on how to avoid that issue and prevent that test failure.
I get a similar error when my editor component re-mounts after a Suspense
state resolves, however I only get this error when the language is set to html
. css
, typescript
, json
, and sql
all work fine. Only html
seems to cause a problem:
errors.ts:19 Uncaught Error: Cannot read properties of null (reading 'getText')
TypeError: Cannot read properties of null (reading 'getText')
at Object.t.findDocumentLinks (htmlWorker.js:7:183574)
at e.<anonymous> (htmlWorker.js:7:459701)
at htmlWorker.js:7:84096
at Object.next (htmlWorker.js:7:84201)
at htmlWorker.js:7:83103
at new Promise (<anonymous>)
at __awaiter (htmlWorker.js:7:82848)
at e.findDocumentLinks (htmlWorker.js:7:459557)
at o.fmr (workerMain.js:13:116138)
at c._handleMessage (workerMain.js:13:65317)
at errors.ts:19:12
Some additional error context:
(anonymous) | @ | errors.ts:19
-- | -- | --
| setTimeout (async) | |
| unexpectedErrorHandler | @ | errors.ts:17
| onUnexpectedExternalError | @ | errors.ts:40
| I | @ | errors.ts:57
| Promise.then (async) | |
| (anonymous) | @ | getLinks.ts:144
| h | @ | getLinks.ts:143
| (anonymous) | @ | links.ts:220
| S | @ | async.ts:21
| (anonymous) | @ | links.ts:220
| (anonymous) | @ | cancellation.ts:97
| ke | @ | cancellation.ts:97
| beginCompute | @ | links.ts:204
| onModelChanged | @ | links.ts:192
| (anonymous) | @ | links.ts:176
| fire | @ | event.ts:562
| M | @ | standaloneCodeEditor.ts:442
| (anonymous) | @ | standaloneEditor.ts:78
| P | @ | standaloneEditor.ts:62
| F | @ | standaloneEditor.ts:77
| (anonymous) | @ | Editor.js:98
| (anonymous) | @ | Editor.js:113
This is in @monaco-editor/react
version 4.3.1
.
EDIT: I wouldn't be surprised if this was caused by some internal logic that is deferred and when it gets a chance to run, the component is no longer mounted thus causing the error. Just a guess though.
UPDATE:
Setting keepCurrentModel
to TRUE
seems to eliminate this error. keepCurrentOriginalModel
and keepCurrentModifiedModel
did not eliminate the error.
We have the same problem with a React application in which Monaco is also used as a JSON editor.
In our app, we show a list of JSON records to select from. When selected, we load a component that uses a Monaco editor to edit the JSON value. If we move rapidly back to the list while the Monaco editor is loading, we get this error.
Looks like when the jsonWorker is calling getText, the instance of Monaco editor in our edit component is already unmounted.
Is there a way to add a validation before calling getText and simply terminate the process when the editor is undefined?
I see that there are null checks since https://github.com/Microsoft/monaco-editor/commit/4f03f6c4bf64e7cb997855aab24c4db19a142923
That should be in builds >= 0.31. What version are you on?
We are using version 0.31.1.
I checked the playground and I see the null checks:
If that can help shed some light on this, I see a similar error, using monaco webpack plugin, inside a vue2 app, using monaco-editor 0.32.1 and monaco-editor-webpack-plugin 7.0.1.
If I load it with no option ( new MonacoWebpackPlugin()
), I get the error TypeError: Cannot set properties of undefined (setting 'apex')
.
But if I load with only typescript new MonacoWebpackPlugin({languages: ['typescript']})
then it all loads fine.
And if I try and add html new MonacoWebpackPlugin({languages: ['typescript', 'html']})
, I get a new error TypeError: Cannot set properties of undefined (setting 'html')
.
And same thing with css: new MonacoWebpackPlugin({languages: ['typescript', 'css']})
gives me TypeError: Cannot set properties of undefined (setting 'css')
Can you inspect this in the developer tools?
How do you solve?
Closing due to lack of information. Likly a webpack issue, if someone could inspect how the packed code looks.