microsoft / monaco-editor

A browser based code editor
https://microsoft.github.io/monaco-editor/
MIT License
39.9k stars 3.56k forks source link

[Bug] typescriptDefaults.setExtraLibs not working as expected #4678

Open Torsten85 opened 6 days ago

Torsten85 commented 6 days ago

Reproducible in vscode.dev or in VS Code Desktop?

Reproducible in the monaco editor playground?

Monaco Editor Playground Link

https://microsoft.github.io/monaco-editor/playground.html?source=v0.51.0#XQAAAAK4BQAAAAAAAABBqQkHQ5NjdQultUhUwcsQIjjaoVayL-wYsMeg24JItvniWevaGyL6ot7GNI6RFj5KIZKnPs8bDABIDSRRqux-oVQT185N0-S9kY8B6TtqQu6-29b1SJjdE3dN-UK1KVtCc43xLj9Jo3QOMz5EH-2xH_NeNavjeq3BMdfl-VDtZ2z4OjBhFRpkU34K6geDzpm9ooZGKEw-1c_GUgYGqEt40F0TiTqOUENu33HWjGIMJoOed9lxM0i_6O2wc5leZUMgfmL5n_viyd8lS-EAz_5V70Qe81d8GDmQ_o6mJXIowRoIlmoe7j9JhyAXd_fvpo5iEjWJ9qMsLL6oLgAiZiEGCydSMzyxrjO_ScgGz1yYlQH-8HiFrosCXcN2p8FUU9tkdYs36UYzHdzIQ0vzvthQaPZg8-qOOJe_RgJyYYYZgyyy2mdncrHPRwWGegmz0o6n2NxsfNI43i7C-Ac3Aqlm_-nQl31MQdrbVTQb9ILdDFiBnIDpQAOu61BDhk8pxnirzVEjBQV4xrOprdpoTaSAq8z_MLAfWXNYZx3NEZq9-ALnVEvh2YanLIN3E3tt89YyIhgPEmh4CjfSs6TPh9LWavKNHV2GKqgIxyxShS2b2tDBcvWRduYVoPob7qD-gAawYRSn9yEd_m9rT69Qz7E2BrKKNva2wbjdb6jbep8W-9fuCdb3EZdGaskKppTIBNZ6l-b9OmTXADGUjrMyocd5kPPKB2RNN1ftYNOD8Hc__-R0qcc

Monaco Editor Playground Code

monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
    allowNonTsExtensions: true,
    moduleDetection: 3, // @see https://github.com/microsoft/monaco-editor/issues/2976#issuecomment-2334468503
})

const myEditor1 = monaco.editor.create(document.getElementById("container1"), {
    value: `// when this editor has focus, line 2 should be valid and line 3 should show an error
const a = api.thisShouldWorkInTheTopEditor()
const b = api.thisShouldWorkInTheBottomEditor()
`,
    language: "typescript",
});

myEditor1.onDidFocusEditorWidget(() => {
     monaco.languages.typescript.typescriptDefaults.setExtraLibs([
        {
            content: `
                interface Api {
                    thisShouldWorkInTheTopEditor(): string
                }

                declare const api: Api
            `,
            filePath: 'globals.d.ts'
        }
    ])
})

const myEditor2 = monaco.editor.create(document.getElementById("container2"), {
    value: `// when this editor has focus, line 2 should show an error and line 3 should be valid
const a = api.thisShouldWorkInTheTopEditor()
const b = api.thisShouldWorkInTheBottomEditor()
`,
    language: "typescript",
});

myEditor2.onDidFocusEditorWidget(() => {
     monaco.languages.typescript.typescriptDefaults.setExtraLibs([
        {
            content: `
                interface Api {
                    thisShouldWorkInTheBottomEditor(): string
                }

                declare const api: Api
            `,
            filePath: 'globals.d.ts'
        }
    ])
})

Reproduction Steps

Actual (Problematic) Behavior

setExtraLibs seems to merge the extra libs instead of replacing the existing libs

Expected Behavior

setExtraLibs should correctly replace all the existing libs

Additional Context

the latest version where the behaviour of setExtraLibs is correct is 0.36.1 . But I can't manage to isolate the modules in 0.36.1 without an export, so downgrading is no option for me

Torsten85 commented 6 days ago

while there might stil be something wrong with setExtraLibs, I got around this issue by don't using extra libs at all but adding a model that provides the types. The value of the modal can easily be changes. Here is a working playground:

https://microsoft.github.io/monaco-editor/playground.html?source=v0.51.0#XQAAAALJBgAAAAAAAABBqQkHQ5NjdQultUhUwcsQIjjaoVayL-wYsMeg24JItvniWevaGyL6ot7GNI6RFj5KIZKnPs8bDABIDSRRqux-oVQUFlIG0-S9kY8B6TtqQu6-29b1SJjdE3dN-UK1KVtCc43xLj9Jo3QOMz5EH-2xH_NeNavjeq3BMdfl-VDtZ2z4OjBhFRpkU34K6geDzpm9ooZGKEw-1c_GUgYGqEt40F0TiTqOUENu33HWjGIMJoOed9lxM0i_6O2wc5leZUMgfmL5n_viyd8lS-EAz_5V70Qe81d8GDmQ_o6mJXIowRoIlR5_rjMZFak5Amy97-ew-UwqG4Ozd_bj4K3LSKcBfHNFA7_5zCzhpV5VGe90V1YgVkaCGv20gVMPPEEzYQ2mRgY6XTaVCoPGjVCLAMiLMPq8Hh5wg61VwXeQwnASLRj6vyKKjqWV6NLZP8pu9-O8XDxxnxTrm-vF3Xm29ZgwX4Q-HIiC7soU5WhYJWGl_PQay_eAQjTUlGFNOFjQknKH6z8dU2pcXBZQUpvnlN7pGaWVMvsO8S19hBTecE3TVIlAhtxgRUUfA7OP9TyhIsZ08kpQYaUIWKwAluJ-xB-XW95BVRikUdlKSHl85okpcLbYib2dWsD4st9jRueMG6_qcgik9Gluh18DO42rWwoCcECG8t_1_rnFW0vIXlPnh1VGOltVRqlSR62Dqk74iATLy97Kw1zaFvfUNQoI1F8H_1-E-48zy15DgGV8tju0Hp5SlfmRlzjWH8P0UijSVXZGW14kVeyNQYPpk2TOzLl9qWitmnT7-Wo1es4SPsG4tPMsn7k2XqmEag2labapLpkKBMcWrbWr0e2IUJ1xvaai3aYZmNr0q7CgrSbMYQQgnRtFPOmB9hwZehbk2NVRVztTHsXcMf_rRBYg