Tutorial: Bazel Build with Visual Studio Code for C++

1,366 views
Skip to first unread message

sha...@shanee.io

unread,
May 28, 2019, 7:16:03 PM5/28/19
to bazel-discuss
I made a small blog post detailing how to get Bazel to work with Visual Studio Code for C++, including build process and debugging:

https://shanee.io/blog/2019/05/28/bazel-with-visual-studio-code/

I hope this helps someone.

(Note: Linux instructions may be off, I didn't have access to my Linux box to test and worked out of memory. Please let me know if so)

Marcel Hlopko

unread,
May 29, 2019, 12:58:49 AM5/29/19
to sha...@shanee.io, bazel-discuss
This is fantastic! Thanks for writing this!

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/26b538cd-9002-42a9-a0fc-e90d06042473%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Marcel Hlopko | Software Engineer | hlo...@google.com | 

Google Germany GmbH | Erika-Mann-Str. 33  | 80636 München | Germany | Geschäftsführer: Geschäftsführer: Paul Manicle, Halimah DeLaine Prado | Registergericht und -nummer: Hamburg, HRB 86891

László Csomor

unread,
May 29, 2019, 4:11:47 AM5/29/19
to Marcel Hlopko, sha...@shanee.io, bazel-discuss
Great! Thanks for writing it and letting us know!

As the Windows team lead, I'd love to hear more about your experiences
installing (and using) Bazel on Windows. How could we make
installation and usage easier?


--
László Csomor | Software Engineer | laszlo...@google.com

Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
> To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAFuL9GnXMu2rLf6kPKpsFh%3DWRMC%3DzjpFUocZKzEQyVb0aOYhiA%40mail.gmail.com.

sha...@shanee.io

unread,
May 29, 2019, 2:33:45 PM5/29/19
to bazel-discuss
Hey László, thanks for the message!

IMO the main two points for improvement in windows would be:

1. Simplifying the getting started process / user experience.
2. Providing symlinks by default.


In my experience most Windows users are less proficient with terminal and setting environment variables. Also the instructions on the site are somewhat confusing.

Windows users could benefit a lot from a more automated process. An installer that will fetch dependencies and set the PATH and visual studio environment variables will go a long way.

Or maybe at least set the environment variables / PATH, but update the documentation to more clearly state all the requirements upfront, as right now it's spread over two pages and people glace through that often.

Right now the user experience is a bit messy, something like this:

1. Go to the bazel website, go to Get Bazel
2. Read the requirements:
2.a. Download the Visual C++ Redistributable
2.b Get confused whether you need MSYS2 or not (my understanding is if you want to call bazel run you still need it, and most regular users will probably want to call bazel run). Also note some people I helped skipped that because it didn't look to them as if it's in the requirements section.
3. Go to github and get the right exe.
4. On Windows 10 for some reason quite a few users had an issue downloading and renaming / moving the .exe file. Possibly some Windows defender interference? The Windows File Explorer would freeze and crash often.
5. Set all the PATH variables manually.
5.a. "Wait, do I need python and java?"
6. Ok done.
7. C++ isn't working.
8. Find this other link (Using Bazel on Windows) and find the C++ section
9. Set the PATH
Note that the 2017 BAZEL_VC path on the site was incorrect for a few users I helped with as they were using the community edition:
BAZEL_VC = C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC
10. Get the Windows SDK.

Also note that when updating the bazel.exe for some reason often the file explorer will freeze and crash when deleting/renaming the old one and moving in the new one to replace it (similar to point 4). I'm still not sure what's causing it as I tried to disable Windows Defender and all. I saw it on at least 10 different machines. (Similarly, the Chrome download will be stuck on 100% and just wait for something)


As for symlinks, the --experimental_enable_runfiles is super helpful, but it's always a bit of a gotcha. Any chance to put it as default for Windows? I'm sure it'll save people a lot of headache. Maybe output a warning about it so people can read more, but most will probably want the functionality and parity with other platforms, and not have to use an extra library/manifest to access their files.

Finally, not windows specific, something to handle the Visual Studio Code sourceFileMap path could be nice. Right now users have to set it for every project, and if they have cross project dependencies it becomes even messier. If there was a way to get it to work more nicely with Visual Studio Code that would be great. An alternative would be to figure out a way to get the correct path automatically instead of debugging once, copying the path, etc.


These are the main things really. Otherwise Bazel is finally quite functional on Windows! Perhaps making things like selecting STD version easier would be nice, instead of writing an entire crosstool to do this single thing. A common solution I've seen was to have a .bazelrc with the following:
build --cxxopt="/std:c++latest"
build --cxxopt="--std=c++17"

But building on OSX/Linux will generate a lot of warnings because of the /std:c++latest. If it could forward the --std=c++17 as if it was /std:c++17 for Visual Studio it might be cleaner.

Seems like most teams I met only wanted to set the STD version and either used the .bazelrc or had exactly one person who kinda managed to get through the crosstool tutorials enough to do this exactly one thing (set the std version) and was still quite confused about it. Seems like a lot of work for so little.


Hope this helps!

Shanee Nishry

unread,
Jun 4, 2019, 6:57:11 PM6/4/19
to bazel-discuss
Hey László, thanks for updating the instructions for Windows. They are *much* better! :)

You received this message because you are subscribed to a topic in the Google Groups "bazel-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bazel-discuss/K_UWMd4_RR4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/00ab7873-a94a-44ce-aef9-997857710e99%40googlegroups.com.

László Csomor

unread,
Jun 7, 2019, 8:01:09 AM6/7/19
to Shanee Nishry, bazel-discuss
Hi Shanee,

A belated but much earned thank you for your detailed, candid feedback!

I started addressing the issues by rewriting the install instructions.
That's already online:
https://docs.bazel.build/versions/master/install-windows.html

Next I wrote scripts to create an .msi installer for Bazel. That PR
(https://github.com/bazelbuild/bazel/pull/8579) is not yet merged nor
approved, but I'm really excited about it, I like how the installer
looks and works, and I believe it'll make Bazel more accessible and
more attractive.

Thanks again for your feedback, it's incredibly helpful to know what
users really need from Bazel.

Cheers!


--
László Csomor | Software Engineer | laszlo...@google.com

Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
> To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAG-S%2BnaoCQRW5G%2B2MiOJcu5bNBgZhs4XCk4f7kgakMce50hnyg%40mail.gmail.com.

mason....@gmail.com

unread,
Jun 7, 2019, 6:39:56 PM6/7/19
to bazel-discuss

One terrible trick that I often recommend because most developers have most of the other tools already (and are hostile to msys) is set `BAZEL_SH` to the bash included with git for windows (`C:\Program Files\Git\usr\bin\bash.exe`). This lets me advertise it as a standalone tool still.

sha...@shanee.io

unread,
Jun 12, 2019, 12:19:14 AM6/12/19
to bazel-discuss
Hey László, this will be super awesome! Thanks for the effort :)

Any thoughts on making enable symlinks the default?

László Csomor

unread,
Jun 24, 2019, 4:18:21 AM6/24/19
to Shanee Nishry, bazel-discuss
mason....@gmail.com: could you say more about "hostile to msys"?
This workaround is risky, see
https://github.com/bazelbuild/bazel/issues/5751

Shanee: you're welcome!
I think we could do that. Filed https://github.com/bazelbuild/bazel/issues/8704


--
László Csomor | Software Engineer | laszlo...@google.com

Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

> To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/4cc4a94e-f2db-430b-8576-6577f8eb2a2d%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages