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

How to use _typescript.configurePlugin? #153

Closed
wSedlacek opened this issue Apr 13, 2024 · 9 comments · Fixed by #149
Closed

How to use _typescript.configurePlugin? #153

wSedlacek opened this issue Apr 13, 2024 · 9 comments · Fixed by #149

Comments

@wSedlacek
Copy link

I am attempting to use the _typescript.configurePlugin command and I get logs in the tsserver.log which indicate that the command is received but the configuration does not seem to take. This is the short version of the command I am trying to run.

      tsclient.request("workspace/executeCommand", {
        command = "_typescript.configurePlugin",
        arguments = {
          "@monodon/typescript-nx-imports-plugin",
          { externalFiles = externalFiles },
        },
      }, function(err, res)
        if err then
          vim.print("Error: ", err)
          return
        end

        vim.print "Done!"
      end)

This is the logs I see.

Info 552  [11:32:12.600] response:
    {"seq":0,"type":"response","command":"configurePlugin","request_seq":5,"success":true}
Perf 553  [11:32:12.600] 5::configurePlugin: async elapsed time (in milliseconds) 0.2472

But I do not see the intended outcome of the plugin.

@yioneko
Copy link
Owner

yioneko commented Apr 14, 2024

Have you checked that the typescript plugin is actually in effect? https://github.com/yioneko/vtsls#typescript-plugin-not-activated
Try to set vtsls.autoUseWorkspaceTsdk to true.

@wSedlacek
Copy link
Author

wSedlacek commented Apr 14, 2024

Okay! So this line of thinking helped a lot and I do have something working, however it isn't quite configured how I want.

I needed to add the plugins to the tsconfig.json
ie

  "compilerOptions": {
    ...
    "plugins": [
      {
        "name": "@monodon/typescript-nx-imports-plugin",
        "externalFiles": []
      }
    ]
  }

I was hoping to use a configuration more like I was using with typescript-tools.nvim where I could specify the plugins in the lua configuration instead of in my projects. Specifically something like tsserver_plugins
https://github.com/pmizio/typescript-tools.nvim?tab=readme-ov-file#%EF%B8%8F-configuration

I attempted to do this with pluginPaths but it seems to not be the same thing.
Reading the source code from vscode and TypeScript this configuration is 1:1 with --pluginProbeLocations (defaults to node_modules from the tsserver execute path)
From what I can tell this pluginProbeLocations isn't actually a list of what plugins to use, but instead a list of possible locations they could be. (Correct me if I am wrong, I only gathered context clues based on the default values from reading the source code)

I believe what I actually need to configure is not --pluginProbeLocations but instead --globalPlugins
Looking at another issue, I see there is a new option being worked on for adding vtsls.tsserver.globalPlugins
#148 (comment)

When using this 0.2.2-alpha.1 I believe my plugin makes it to where this workspace.getConfiguration("vtsls").get("tsserver.globalPlugins") is used, however I believe the resolvePluginPath() is not resolving my plugin in /Users/wsedlacek/Library/Application Support/fnm/node-versions/v20.11.1/installation/lib/node_modules and thus when I look at the logs I am not seeing --globalPlugins configured.

More investigation is required, but this is the right track.

I had my configuration incorrect for vtsls.tsserver.globalPlugins, I needed to nest the value with an object and name.
My plugin is now loading how I want.

  settings = {
    vtsls = {
      tsserver = {
        globalPlugins = {
          { name = "@monodon/typescript-nx-imports-plugin" },
        },
      },
    },

I am interested in knowing when the 0.2.2-alpha.1 will be released.

@wSedlacek
Copy link
Author

Thank you for all your hard work!

@Tantol
Copy link

Tantol commented May 17, 2024

@wSedlacek Did you manage to make vtsls work with nx monorepos similar to typescript-tools with custon NxInit function? If yes, would you share configuration please?

@wSedlacek
Copy link
Author

I think this works.

  local lspClients = vim.lsp.get_active_clients()
  local tsclient
  for _, client in ipairs(lspClients) do
    if client.name == "vtsls" then
      tsclient = client
    end
  end
  ...

      tsclient.request("workspace/executeCommand", {
        command = "_typescript.configurePlugin",
        arguments = {
          "@monodon/typescript-nx-imports-plugin",
          { externalFiles = externalFiles },
        },
      }, function(err, res)
        if err then
          vim.print("Error: ", err)
          return
        end

        vim.print "Done!"
      end)

I haven't used it in a bit since I stopped using the solution pattern with Nx

@Tantol
Copy link

Tantol commented May 21, 2024

@wSedlacek Thank you.
For any one who want's to make it work, remember to:

  • Install @monodon/typescript-nx-imports-plugin in project (global install is not enough)
  • Add configuration in tsconfig.json:
  "compilerOptions": {
    "plugins": [
      {
        "name": "@monodon/typescript-nx-imports-plugin",
        "externalFiles": []
      }
    ]
  }

For me without these steps above setup is not working with vtsls.

Setup with typescript-tools (described in this thread) doesn't require these two extra steps.

@wSedlacek
Copy link
Author

@Tantol Oh! Right! I forgot, you can configure these to avoid needing to change your tsconfig.json.

  settings = {
    vtsls = {
      tsserver = {
        globalPlugins = {
          { name = "@monodon/typescript-nx-imports-plugin" },
        },
      },
    },

In full it looks like this.

lspconfig.vtsls.setup {
  on_init = on_init,
  on_attach = on_attach,
  capabilities = capabilities,
  root_dir = util.root_pattern ".git",
  filetypes = { "angular", "typescript", "typescriptreact", "javascript", "javascriptreact" },
  settings = {
    vtsls = {
      autoUseWorkspaceTsdk = true,
      experimental = {
        completion = {
          enableServerSideFuzzyMatch = true,
        },
      },
      tsserver = {
        globalPlugins = {
          { name = "@monodon/typescript-nx-imports-plugin" },
        },
      },
    },
    typescript = {
      prefrences = {
        quoteStyle = "auto",
      },
    },
  },
}

@Tantol
Copy link

Tantol commented May 22, 2024

@wSedlacek That is wonderful. Do you know how to avoid installing @monodon/typescript-nx-imports-plugin as devDependencie per workspace? Or should it work with globally installed @monodon/typescript-nx-imports-plugin and I'm missing something?

@wSedlacek
Copy link
Author

@Tantol If you are using vtls installed with Mason then you can install it in that directory ie ~/.local/share/nvim/mason/packages/vtsls

I haven't figured out how to make it resolve to a global installation though. I haven't looked too much into it. There probably is a way to write the lua so that the name is an absolute path to the module and make it work.

Perhaps if you had vtsls installed globally instead of through mason that would also work? I haven't tried that either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants