vscode-bazel
vscode-bazel copied to clipboard
Add option to "Run" from the Bazel Build Targets viewer
In addition to the current option to Build, it'd be great if there was an option to Run straight from the Bazel Build Targets viewer.
Even better, if we could configure some default arguments for the build/run options, but probably not critical as we can use the .bazelrc for now. (Reason for it is enable symlinks on Windows and spawn_strategy=standalone for OSX to enable debugging).
A big and cool feature would be to call Debug, or at least automatically generate the debug configuration file (launch.json) for visual studio code, including the source maps. The format is pretty simple so it might not be too difficult to create the json for it.
See https://shanee.io/blog/2019/05/28/bazel-with-visual-studio-code/
I have never worked on visual studio code extensions, but I tried to hack something that will work for a hardcoded project as proof of concept. I got a working function to build and start a debug session for a hardcoded project:
let taskDefinition: vscode.TaskDefinition = {
type: 'shell',
command: "bazel build :example -c dbg",
};
let task = new vscode.Task(taskDefinition,
vscode.TaskScope.Workspace,
"Compile Bazel Project",
"bazel",
new vscode.ShellExecution("bazel build :example -c dbg")
);
vscode.tasks.executeTask(task);
vscode.tasks.onDidEndTask(((e) => {
console.log('onDidEndTask: ' + e.execution.task.name);
if (e.execution.task === task) {
let config: DebugConfiguration = {
name: "Test",
type: "cppvsdbg",
request: "launch",
//cwd: vscode.workspace.rootPath + "/bazel-bin/example.exe.runfiles/__main__/",
cwd: vscode.workspace.rootPath + "/bazel-bin/",
program: vscode.workspace.rootPath + "/bazel-bin/example.exe",
externalConsole: false,
stopAtEntry: false,
sourceFileMap: {
"C:/Users/shane/_bazel_shane/rimt6fvp/execroot/__main__/": "${workspaceFolder}",
}
};
vscode.debug.startDebugging(undefined, config, undefined);
}
}));
The main difficulties I found are:
- I'm not sure how to get the path for the sourceFileMap correctly, so I had this hardcoded for the proof of concept.
- For some reason when running the build task from this command, bazel wouldn't generate the main folder under example.exe.runfiles. Might be a permission issue?
I feel like once these two are solved, getting it to work similarly to the current Build function shouldn't be too difficult
This feature is pretty interesting for me. I'm looking at moving a Pythin/JS/Go monorepo over to Bazel. Right now we'e heavily containerized and have (especially on the python side) a decent number of executable/scripts. IDE users are burdened with having to setup launch configs for each script they're working on, which is super annoying.
I was hoping that bazel could free us from that since the binaries are explicitly defined.
Can someone please tell me how to generate a launch.json for running a python file via bazel
+1 for this feature.
+1 please add "bazel run" option for binary targets. thanks!
This was implemented in #299
I would propose to continue the discussion around debugger integration in https://github.com/bazelbuild/vscode-bazel/issues/207#issuecomment-1973622632
Okay I'll close this.