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.
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);
来源:https://stackoverflow.com/questions/51670505/vscode-extensions-location-variable