Missing module in Javascript but not in Typescript

Viewed 478

I'm coding a vscode extension following the guide and I came accross an import problem in my server.ts file. Here is the file tree:

test
 ┣ client
 ┃ ┣ out
 ┃ ┣ src
 ┃ ┃ ┗ index.ts
 ┃ ┣ package-lock.json
 ┃ ┣ package.json
 ┃ ┗ tsconfig.json
 ┣ server
 ┃ ┣ out
 ┃ ┣ src
 ┃ ┃ ┣ completions.ts
 ┃ ┃ ┣ parser.ts
 ┃ ┃ ┗ server.ts
 ┃ ┣ package-lock.json
 ┃ ┣ package.json
 ┃ ┣ tsconfig.json
 ┃ ┗ yarn.lock
 ┣ package.json
 ┗ tsconfig.json

Which is the same structrure as the extension sample provided by Microsoft.

However, in server.ts, I'm trying to import some elements from the vscode module:

//server.ts

import { workspace as Workspace } from 'vscode';

let sm_home: string = Workspace.getConfiguration("sourcepawnLanguageServer").get("sourcemod_home");
...

and in my package.json, I have:

  "dependencies": {
    "@types/glob": "^5.0.30",
    "@types/uuid": "^3.4.0",
    "@types/vscode": "^1.14.0",
    "glob": "^7.1.2",
    "uuid": "^3.1.0",
    "vscode-languageserver": "^7.0.0",
    "vscode-languageserver-textdocument": "^1.0.1",
    "vscode-test": "1.5.1",
    "vscode-uri": "^3.0.2"
  },

The whole project compiles fine, but when I start the extension, I get the following error:

Error: Cannot find module 'vscode'
Require stack:
- c:\Users\Charles\CloudStation\Documents\Perso\Dev\sourcepawn-vscode\server\out\server.js
    at Function._resolveFilename (internal/modules/cjs/loader.js:1019:15)
    at internal/modules/cjs/loader.js:895:27
    at Function._load (electron/js2c/asar_bundle.js:5:12738)
    at Module.require (internal/modules/cjs/loader.js:1079:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (c:\Users\Charles\CloudStation\Documents\Perso\Dev\sourcepawn-vscode\server\out\server.js:5:18)
    at Module._compile (internal/modules/cjs/loader.js:1199:30)
    at Object..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1039:32)
    at internal/modules/cjs/loader.js:932:14 {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'c:\\Users\\Charles\\CloudStation\\Documents\\Perso\\Dev\\sourcepawn-vscode\\server\\out\\server.js'
  ]
}

And the error goes away if I remove the import and the line using the import in the server.ts file. This import works fine in index.ts` Could this be due to the fact that server.tsis called inside ofindex.ts` ?

import { ExtensionContext, workspace as Workspace, window, commands } from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient/node';
import * as glob from 'glob';
import * as path from 'path';

export const sm_home: string = 'Workspace.getConfiguration("sourcepawnLanguageServer").get("sourcemod_home")';

export function activate(context: ExtensionContext) {
    let serverModule = context.asAbsolutePath(
        path.join('server', 'out', 'server.js')
...

This doesn't seem to bother the C/C++ extension.

Thanks in advance for the help!

2 Answers

The vscode package has been deprecated. It has now been split up into two packages which are @types/vscode and vscode-test. The reason for this refactoring can be found here.

You have included the @types/vscode dependency but not vscode-test.

Adding the vscode-test dependency should fix your issue i.e.

npm i vscode-test

Also ensure you have:

"engines": { "vscode": "^1.32.0" }

defined in your package.json file.

Found what's going on after a while:

Using vscode in a language server instance inside a vscode extension doesn't work because (from what I understand) the server doesn't run alongside vscode like the extension does.

If you need settings, workspace's uris etc, you should use connection.workspace..., but keep in mind that the requests will be asynchronous.

See an example of how to create a connection object here.

Related