Skip to content
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

addExtraLib() is not working as expected after disposing the previously added library #1998

Closed
jithamanyumanne opened this issue Jun 3, 2020 · 4 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug typescript
Milestone

Comments

@jithamanyumanne
Copy link

jithamanyumanne commented Jun 3, 2020

monaco-editor version: 0.20.0
Browser: Chrome
OS: macOS High Sierra

I am adding extraLibrary to the monaco using the following code

const carDisposable = monaco.languages.typescript.javascriptDefaults.addExtraLib("declare class Car{ 
start()
stop()
 }", "Car");

The code in the editor is as follows
const myCar = new Car();

Now I want to dispose the same and add again with the new methods

carDisposable.dispose();
monaco.languages.typescript.javascriptDefaults.addExtraLib("declare class Car {  
start() 
applyBreak()
accelerate()
 }", "Car");

Now in the editor if I am using myCar. then I am getting start() and stop(). Ideally I should get start(), applyBreak() and accelerate().

@paranoidjk
Copy link
Contributor

+1 same problem here

@wenshiqi0
Copy link

wenshiqi0 commented Jul 30, 2020

There is a time racing here.

addExtraLib(content: string, _filePath ?: string): IDisposable {
  let filePath: string;
  if (typeof _filePath === 'undefined') {
    filePath = `ts:extralib-${Math.random().toString(36).substring(2, 15)}`;
  } else {
    filePath = _filePath;
  }

  if (this._extraLibs[filePath] && this._extraLibs[filePath].content === content) {
    // no-op, there already exists an extra lib with this content
    return {
      dispose: () => { }
    };
  }

  let myVersion = 1;
  if (this._extraLibs[filePath]) {
    myVersion = this._extraLibs[filePath].version + 1;
  }

  this._extraLibs[filePath] = {
    content: content,
    version: myVersion,
  };
  this._fireOnDidExtraLibsChangeSoon();

  return {
    dispose: () => {
      let extraLib = this._extraLibs[filePath];
      if (!extraLib) {
        return;
      }
      if (extraLib.version !== myVersion) {
        return;
      }

      delete this._extraLibs[filePath];
      this._fireOnDidExtraLibsChangeSoon();
    }
  };
}

If you use the same specific filePath to add content, and not listen to the onDidExtraLibsChange event, you may update the version inside. Then you can not dispose this content by last dispose method.

@wenshiqi0
Copy link

And the code comes from monaco-typescript.

@alexdima
Copy link
Member

You could make the second call to use "Car2" or you can simply leave empty that argument. I think the problem is that removing + adding another lib will make it that there still is the lib at version 1. So the TS worker does not refresh the content of that lib.

@alexdima alexdima added the bug Issue identified by VS Code Team member as probable bug label Sep 10, 2020
@alexdima alexdima self-assigned this Sep 18, 2020
@alexdima alexdima added this to the August 2020 milestone Sep 18, 2020
pgherveou pushed a commit to pgherveou/monaco-typescript that referenced this issue Sep 21, 2020
pgherveou pushed a commit to pgherveou/monaco-typescript that referenced this issue Sep 21, 2020
* Format signature and parameter documentation as Markdown

* Add support for creating a custom webworker subclass

* Make a useful sample

* Tidy up

* Settle on customTSWorkerFactory

* Adds a vfs project

* Tightens the public API

* update deps

* update to typescript@4.0.2

* Add prettier

* Run prettier

* Use the global `monaco` only in the AMD case (see microsoft/monaco-editor#1974)

* Add deprecated diagnostic tag if a symbol is reported as deprecated

* Add deprecated tag to suggestion if entry is marked as deprecated

* Include tags in documentation string of suggestion items

* Align tag representation in suggestion items and hovers

* 4.0.0

* small style tweaks

* 4.0.1

* improve `.npmignore`

* Allows setting lib with shortnames

* Remove declare modifiers (microsoft/monaco-editor#2121)

* 4.0.2

* Update import TS

* Use typescript language for hover tooltip header

* update `package-lock.json`

* Adopt `async`

* Increase `printWidth`

* `var` -> `let`

* Fixes microsoft/monaco-editor#1937: Remove debugger statement

* Fixes microsoft/monaco-editor#1638: Protect against failing `getCodeFixesAtPosition` calls

* Fixes microsoft/monaco-editor#1998: make sure to always increase the version number

* Adopt latest `monaco-editor-core`, update to TS 4.0.3

* 4.1.0

* Add missing setWorkerOptions

* commit generated file

* update upstream and enable regex

Co-authored-by: Sebastian Pahnke <pahnke.sebastian@gmail.com>
Co-authored-by: Orta <git@orta.io>
Co-authored-by: Alex Dima <alexdima@microsoft.com>
Co-authored-by: Spencer <spencer@sf-n.com>
@vscodebot vscodebot bot locked and limited conversation to collaborators Nov 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug typescript
Projects
None yet
Development

No branches or pull requests

4 participants