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

[TSS plugin] How to dynamically load d.ts file #40503

Closed
CGQAQ opened this issue Sep 11, 2020 · 3 comments
Closed

[TSS plugin] How to dynamically load d.ts file #40503

CGQAQ opened this issue Sep 11, 2020 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@CGQAQ
Copy link

CGQAQ commented Sep 11, 2020

  • VSCode Version: 1.48.2
  • OS Version: Windows_NT x64 10.0.19041

I want to turn ON/OFF tss plugin by changing enable field in config, and when I need to add/remove d.ts files, I encountered this problem

languageServiceHost.getScriptFileNames = () => {
      let scriptFileNames = getScriptFileNames();

      this.logger.info(
        `getScriptFileNames: ${
          this.configurationManager.getPluginConfig()?.enable
        } ${scriptFileNames}`
      );

      if (!this.configurationManager.getPluginConfig()?.enable) {
        return scriptFileNames;
      }

      // Get typescript declaration File
      const dtsFiles = [
        getDenoDts(
          !!this.configurationManager.getProjectConfig().config.unstable
        ),
      ];

      const iterator = new Set(dtsFiles);
      scriptFileNames = [...iterator, ...scriptFileNames];

      this.logger.info(
        `getScriptFileNames--dtsFiles: ${JSON.stringify(
          dtsFiles
        )} getScriptFileNames--scriptFileNames: ${JSON.stringify(
          scriptFileNames
        )}`
      );

      return scriptFileNames;
    };

the this.configurationManager.getPluginConfig()?.enable turned to true, deno.d.ts added to scriptFileNames, but still don't get the types declared in deno.d.ts

I tried add this piece of code from #11810 (comment)

 languageServiceHost.getProjectVersion = () => {
      this.logger.info(`getProjectVersion: ${new Date().toUTCString()}`);
      return new Date().toUTCString();
    };

didn't work either

tsserver.log

@CGQAQ CGQAQ changed the title [TSS plugin] How to change file of program [TSS plugin] How to dynamically load d.ts file Sep 11, 2020
@mjbvz mjbvz transferred this issue from microsoft/vscode Sep 11, 2020
@mjbvz mjbvz removed their assignment Sep 11, 2020
@CGQAQ
Copy link
Author

CGQAQ commented Sep 13, 2020

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Sep 14, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

@hoovercj
Copy link
Member

hoovercj commented Feb 2, 2021

I stumbled across this bug because I was looking to do exactly the same thing so I thought I'd share my solution for posterity 😄

I figured out that calling project.markAsDirty() when the configuration changed caused the getScriptFileNames() implementation to be recalled.

Put together into a simple plugin implementation:

import typescript from "typescript/lib/tsserverlibrary";

module.exports = function init(): typescript.server.PluginModule {
  let path: string | null = null;
  let typescriptProject: typescript.server.Project;

  return {
    create(
      createInfo: typescript.server.PluginCreateInfo
    ): typescript.LanguageService {
      const { languageService, languageServiceHost, project, config } = createInfo;

      typescriptProject = project;

      // Read the initial config    
      path = config.path;

      // Bind the original implementation so it can be called from
      // the overriding implementation
      const getScriptFileNames = languageServiceHost.getScriptFileNames.bind(
        languageServiceHost
      );

      languageServiceHost.getScriptFileNames = () => {
        const scriptFileNames = getScriptFileNames();

        if (!!path) {
          scriptFileNames.push(path);
        }

        return scriptFileNames;
      };

      return languageService;
    },
    onConfigurationChanged(config: { path: string | null }) {
      if (config.path !== path) {
        path = config.path;
        typescriptProject.markAsDirty();
      }
    }
  };
};

Edit: Edited to read the initial config from createInfo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants