Closed dandavison closed 2 years ago
I currently use vm.runInNewContext
. Here no access to the local scope or require is possible. I change it, but only within Trusted Workspaces.
Fantastic, thanks again for the quick and very helpful response.
I have integrated an OutputChannel that would output errors in the script area. Did you find the channel
Also, it would be possible to debug that script. For this I have created a howto for vscode-httpyac. The principle is the same. .
Hi @AnWeber, I don't think this is working for me. I've installed from a .vsix
built on master c7ed250a71ccfaa1f66f2613f0fa9801a16c7712, and I'm doing the following experiments:
"script": "console.log(vscode.workspace.isTrusted); statusBarItem.text = 'hello'"
I see true
in the developer tools JS console, and hello
appears in the status bar.
"script": "console.log(vscode.workspace.isTrusted); require('fs'); statusBarItem.text = 'hello'",
I still see true
in the dev tools console, but the status bar text is now whatever I have set in the "text"
field of the "statusbar_command.commands"
config entry.
The output channel is emitting lines that look like "empty JS object" syntax. Thanks for your debugging tips above; I haven't looked into any of this properly yet! I'll try to follow your suggestions above when I get a bit more time.
@dandavison There were still a few small errors in the code. I have now created a test case (read package version) and tested it.
{}
was an error because of the execution with runInContext
. Error Class is encapsulated in some kind of proxy and therefore instanceOf Error
does not work correctly require
is actually not directly available. I have now included the simple variant, but it resolves the require
in the context of my extension. Meaning own dependencies in the current workspace are not available.${workspaceFolder.fsPath}/package.json
) . {
"alignment": "left",
"command": "workbench.action.terminal.toggleTerminal",
"id": "sbc.packageVersion",
"text": "Package Version",
"scriptEvents": ["vscode.workspace.onDidChangeConfiguration", "vscode.window.onDidChangeActiveTextEditor"],
"script": "workspaceFolder && (statusBarItem.text = (JSON.parse(require('fs').readFileSync(`${workspaceFolder.fsPath}/package.json`, 'utf8'))).version)"
}
Thank you very much, this is working perfectly for me now.
I'd like to create a statusbar command that displays some text returned by a function in the node
fs
module. Specifically, I'm trying:However, from experimenting, I believe that
fs
is not available in the script context and thatrequire('fs')
doesn't work (although I failed to catch the error when I tried usingtry...catch
in the script).Is it possible to do what I'm trying to do here, or what would it take to make it possible?