I have this contribution:
{
"contributes": {
"configuration": {
"title": "mytool",
"properties": {
"args": {
"description": "Command line arguments, passed to mytool",
"type": "list",
"default": [],
"items": {
"type": "string"
}
}
}
}
}
Inside the extension code, I can grab the args like this:
const workspaceFolder = vscode.workspace.getWorkspaceFolder(document.uri);
const config = vscode.workspace.getConfiguration("mytool", workspaceFolder || null);
const args = config.get("args", []);
That works as expected when setting this via settings.json
{
"mytool.args": ["--foo"]
}
However, I noticed that VS Code does not recognize this as valid configuration. It is grayed out. Instead, it knows about top level args
, and shows the description for it.
{
"args": []
}
I tried several combinations to have it recognize it. The documentation suggests naming the property mytool.args
, in the package.json. However, if I do this, I am not able to pick up the config in my extension code anymore.
What is the proper way to configure this?
2
Answers
I was able to solve it by doing 2 things.
mytool.args
, in the package.json and set its scope to resource.Or alternatively, like this
Note how the
document.uri
is passed as scope.I had already the suspicion that it has to do work of my attempt to handle mutli-root workspaces. While my idea was ok, the execution was not 100% correct.
The proper way to do it is documented here. It took quite some time to find that page. https://github.com/Microsoft/vscode/wiki/Adopting-Multi-Root-Workspace-APIs#settings
In your
package.json
contribution, thetitle
field should match the namespace you want to use for your configuration keys. In your case, this should be"mytool"
. Here’s how the contribution in yourpackage.json
should look:Note: The
type
field should be"array"
instead of"list"
.Now, the setting
"mytool.args"
should be recognized in your settings.json and can be read in your extension as you’ve shown:Here,
getConfiguration("mytool")
is getting all configuration settings that start with"mytool."
, andget("args")
is getting the"args"
part of the"mytool.args"
setting.