Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

visual studio code - VSCode extensions location variable

In an extension I am writing, I want to redefine an existing setting in the workspace to point at a script I am packaging with the extension. On a mac this script lives in ~/.vscode/extensions/publisher.name.version/script for example.

If I assume that this is where the extension lives then in my activate function I can update this value using

export async function activate(context: vscode.ExtensionContext) {
  const home = process.env.HOME;
  const execLocation = home + "/.vscode/extensions/publisher.name.version/script";
  ...

and then updating the workspace setting.

However - I would like to access the locally installed extensions location, together with the id and version of my extension - I cannot find the correct setting in VSCode to do this. I would be very grateful if someone knew the correct environment variable so I could access them.

I know it is possible to call code from the command line with the option --extensionHomePath - I am not sure how to access this variable programmatically.

Also I am not sure how to find the version, publisher and name from the context parameter - obviously I know these from the package.json file but it would be nice to be able to access them programmatically if possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can get that information by the asAbsolutePath() method of the ExtensionContext.
This method gets you the absolute path of a resource for a given relative path (regarding your project root).

Therefore I suggest you to change your code to the following:

export async function activate(context: vscode.ExtensionContext) {
    const execLocation = context.asAbsolutePath("script");
    console.log("Absolute exec location: " + execLocation);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...