Home > other >  Referencing code outside the VS Code extension root folder
Referencing code outside the VS Code extension root folder

Time:03-28

I have a monorepo with my VS Code extension code located in the vs-code-extension folder:

.
├── website
├── vs-code-extension
└── shared

I am trying to access code from the shared folder inside the code in vs-code-extension. The extension code is mostly the default that is auto-generated when the project is initially built. The only notable change that I have made is the addition of an import statement to a file from the shared folder inside the extension.ts and the addition of a reference to the tsconfig.json file as follows:

"references": [
  { "path": "../shared" }
],

As a result, the extension starts just fine, however upon trying to execute the helloWorld command, I get the following error:

command 'my-project-name.helloWorld' not found

Upon removing the import statement to the shared folder's file in extension.ts - this error disappears.

My question here is if it is possible to reference code from outside the extension directory, and if so what would be the way to do it.

CodePudding user response:

I was able to solve this by importing the .js version of the code from the shared folder instead of .ts.

Originally, whilst the vscode intellisense and tsc build-time showed no problems with importing .ts files from the shared folder, after the extension code was compiled into .js (and placed in the out folder by default) I found that this code was still attempting to import the .ts from the shared folder which obviously caused the rest of the code to stop from working.

The returned error by vscode was too broad to identify this problem and hopefully this solution helps anyone facing similar issues when working with monorepos.

  • Related