|
| 1 | +# Configuring Visual Studio Code |
| 2 | + |
| 3 | +Visual Studio is a free code editor, which runs on the macOS, Linux, and Windows operating systems. |
| 4 | + |
| 5 | +It has nice tooling for Python and C++ development, visual debugger, git integration, and many more |
| 6 | +useful features. It is a great editor to use for TensorFlow IO development, but it takes some effort |
| 7 | +to configure it properly. VSCode configuration is very flexible, it allows compiling project using |
| 8 | +bazel, and running code under Python and C++ debuggers. This manual is for Linux, other OSes |
| 9 | +might have specifics, but approach should be similar. |
| 10 | + |
| 11 | + |
| 12 | +## Extensions |
| 13 | +To install an extension click the extensions view icon (Extensions) on the Sidebar, or use the shortcut Ctrl+Shift+X. |
| 14 | +Then searh for keyword below. |
| 15 | + |
| 16 | +- [C/C++](https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools) - Official C++ extension from Microsoft |
| 17 | +- [Python](https://marketplace.visualstudio.com/items?itemName=ms-python.python) - Official Python extension from Microsoft |
| 18 | +- [Python Extension Pack](https://marketplace.visualstudio.com/items?itemName=donjayamanne.python-extension-pack) - another useful extension for Python development |
| 19 | + |
| 20 | +## Compiling projects |
| 21 | +TensorFlow IO is compiled using bazel build command: |
| 22 | + |
| 23 | +```sh |
| 24 | +bazel build -s --verbose_failures --compilation_mode dbg //tensorflow_io/... |
| 25 | +``` |
| 26 | + |
| 27 | +See project [README](https://github.com/tensorflow/io#ubuntu-18042004) file for details on how to setup development environment in Ubuntu. |
| 28 | +--compilation_mode dbg flag here indicates that produced binary should have debug symbols. |
| 29 | +Once you can compile project from command line, you can also configure VSCode to be able to invoke same command. |
| 30 | + |
| 31 | +Open View->Command Pallete (Ctrl+Shift+P) and start typing: "Tasks: Configure Build Task". |
| 32 | +If you are doing this for the first time, editor is going to suggest creating tasks.json file. |
| 33 | +Once you have it, paste following json: |
| 34 | + |
| 35 | +```jsonc |
| 36 | +{ |
| 37 | + "version": "2.0.0", |
| 38 | + "tasks": [ |
| 39 | + { |
| 40 | + "label": "Build TF.IO (Debug)", |
| 41 | + "type": "shell", |
| 42 | + "command": "bazel build -s --verbose_failures --compilation_mode dbg //tensorflow_io/...", |
| 43 | + "group": { |
| 44 | + "kind": "build", |
| 45 | + "isDefault": true |
| 46 | + }, |
| 47 | + "problemMatcher": [] |
| 48 | + } |
| 49 | + ] |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Now, you can press "Ctrl+Shift+B", and VSCode is going to use the command above to build the project. |
| 54 | +It uses its own terminal window, where all links are clickable. So when compilation error occurs, you can |
| 55 | +just click the link, and editor will open corresponding file and navigate to the line. |
| 56 | + |
| 57 | +## Debugging projects |
| 58 | +Debugging Python code is trivial, follow official documentation to figure out how to configure VSCode to enable that: https://code.visualstudio.com/docs/python/debugging |
| 59 | +Debugging C++ code requires GDB to be installed on your system. |
| 60 | +If you have a bq_sample_read.py python script that is using tensorflow-io library that is normally |
| 61 | +executed like: |
| 62 | +```sh |
| 63 | +python3 bq_sample_read.py --gcp_project_id=... |
| 64 | +``` |
| 65 | + |
| 66 | +In order to execute it under GDB, run following: |
| 67 | +```sh |
| 68 | +gdb -ex r --args python3 bq_sample_read.py --gcp_project_id=... |
| 69 | +``` |
| 70 | + |
| 71 | +If application crashes in C++ code, you can run ```backtrace``` in GDB console to get stacktrace. |
| 72 | + |
| 73 | +VSCode also has GDB debugger support, it allows adding breakpoints, see values of variables and step through the code. |
| 74 | +To add debug configuration press the Debug View icon (Debug) on the Sidebar, or use the shortcut Ctrl+Shift+D. Here press the little down arrow next to the play button and select "Add Configuration...". |
| 75 | +It will create launch.json file, add following config here: |
| 76 | + |
| 77 | +```jsonc |
| 78 | +{ |
| 79 | + "name": "(gdb) Launch", |
| 80 | + "type": "cppdbg", |
| 81 | + "request": "launch", |
| 82 | + "program": "/usr/bin/python3", |
| 83 | + "args": ["bq_sample_read.py", "--gcp_project_id=..."], |
| 84 | + "stopAtEntry": false, |
| 85 | + "cwd": "${workspaceFolder}", |
| 86 | + "environment": [ |
| 87 | + { |
| 88 | + /* path to your bazel-bin folder */ |
| 89 | + "name": "TFIO_DATAPATH", |
| 90 | + "value": "/usr/local/google/home/io/bazel-bin" |
| 91 | + }, |
| 92 | + { |
| 93 | + /* other env variables to use */ |
| 94 | + "name": "GOOGLE_APPLICATION_CREDENTIALS", |
| 95 | + "value": "..." |
| 96 | + } |
| 97 | + ], |
| 98 | + "externalConsole": false, |
| 99 | + "MIMode": "gdb", |
| 100 | + "setupCommands": [ |
| 101 | + { |
| 102 | + "description": "Enable pretty-printing for gdb", |
| 103 | + "text": "-enable-pretty-printing", |
| 104 | + "ignoreFailures": true |
| 105 | + } |
| 106 | + ] |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +If everything is configured correctly, you should be able to do Run -> Start Debugging (F5) or Run -> Run Without Debugging (Ctrl + F5). This will run your code under debugger: |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +One other thing worth doing to simplify debugging experience is configuting GDB to skip standard C++ libraries, so you don't step into code you don't care about. In order to do this, create ```~/.gdbinit``` file with following content: |
| 115 | +``` |
| 116 | +skip -gfi /usr/include/c++/*/*/* |
| 117 | +skip -gfi /usr/include/c++/*/* |
| 118 | +skip -gfi /usr/include/c++/* |
| 119 | +``` |
| 120 | + |
| 121 | +## Formatting files |
| 122 | +You can always reformat C++ or Python file by Right Click -> Format Document (Ctrl + Shift + I), but VSCode uses different style conention. Luckily it is easy to change. |
| 123 | + |
| 124 | +For Python formatting, see https://donjayamanne.github.io/pythonVSCodeDocs/docs/formatting/ |
| 125 | + |
| 126 | +To configure C++ formatter, do following: |
| 127 | + |
| 128 | +- Go Preferences -> Settings |
| 129 | +- Search C_Cpp.clang_format_fallbackStyle |
| 130 | +- Modify the file:setting.json directly |
| 131 | +- Add following |
| 132 | + |
| 133 | +``` |
| 134 | +"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google}" |
| 135 | +``` |
0 commit comments