-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
VS Code integration #21119
Comments
For Kconfig, it's because Line 214 in 9d5d252
Would need to either include it manually where it's needed or tell VS Code about it somehow.
|
We'd need to generate it too, since it's a build artifact that depends on the selected board and defconfig. Same deal with DTS #defines, syscalls, bunch of other stuff. |
Another nice improvement would be to enable search across modules, which I haven't been able to get working so far (while it is somewhat documented). |
This should already be supported using the .editorconfig file we have in the tree. That works for me. Alternativly, you could set this directly: https://code.visualstudio.com/docs/getstarted/settings#_language-specific-editor-settings |
There is a closed issue #17645 with some configs for vscode. Maybe it helps. |
I would also add that some default configuration for https://marketplace.visualstudio.com/items?itemName=marus25.cortex-debug would be helpful for the people developing with ARM architecture. I'm pretty sure this is what platform.io uses with some improvements https://platformio.org/platformio-ide So having some support for
|
CC: @trond-snekvik Maybe you have some input here? |
I actually made a couple of extensions improve my workflow with Zephyr:
|
One more potentially useful repo with info by @bus710: |
I've had some good luck using VS Code and its support for Language Server to do intellisense stuffs by having CMake generate a compilation database (that VS Code then consumes automatically, assuming the build directory is in the project). The CMake parameter is
If you always want to generate the compilation database, you can also do:
|
Here is a helpful setting in my workspace c_cpp_properties.json:
I also found some helpful information here: In particular, the forced includes and defines to get IntelliSense to work properly:
|
Also add this to your CMakeLists.txt for your project:
|
CMake can be configured to generate a compile_commands.json. However, the file is for the application which leads to problems when you open a component of the Zephyr RTOS into vscode. For instance, if I open drivers/i2c/i2c_sam0.c I will see an error that there is no target for that file. This is true since the compilation of the OS is controlled by other CMake processes. Vscode opens the file and parses using the include paths and forced includes and defines specified in the config file. What would be useful here is to be able to generate the compile_commands.json file for all the other Zephyr RTOS related files. That way, vscode would be able to open and parse any of the OS files (drivers, kernel, subsystems, modules, etc). At the moment, I have to rely on the Include paths and forced includes and defines in my config. These could be eliminated all together if I could get the compile_commands.json for all the components of Zephyr. |
Here is an example of a task I use to invoke west to do a build:
|
I found another way to integrate Zephyr with vscode using the CMake-tools extension. I had been playing around with this extension a while back but really did not understand how to use it. The key to use it for Zephyr is the "configurationProvider" property in c_cpp_properties.json. Install the vector-of-bool (now microsoft) Cmake-tools extension. Then, edit your c_cpp_properties.json in your project's .vscode directory and add the following configuration:
Select the "Cmake" configuration that was added by clicking the configuration button in the bottom right portion of the status bar beside the "Tweet Feedback" button (it is typically set to "Mac" or "Win"). When you select the "Cmake" configuration (or whatever name you gave it), this automatically finds compile_commands.json according to the CMake extension settings (you can change the default if you need to). To get the Zephyr components to parse properly, edit the CMakeLists.txt files in each of the Zephyr directories that contains source files you want to be able to open with your project. Add the following line to each file:
In my case, I edited CMakeLists.txt in the following directories: Build your project. Inspect the project compile_commands.json file and now you will see a number of additional entries for all the Zephyr components. Now, when you open your Zephyr source files, they should be parsed properly by the c_cpp extension. |
Some other useful extensions: ARM, Dan Underwood |
There is one problem with using the compile_commands.json workflow with VSCode. When you apply the CMAKE_EXPORT_COMPILE_COMMANDS setting, it doesn't seem to propagate to all the required files. For instance, sam0_rtc_timer.c does not appear in my compile_commands.json. I have not determined why it is not being included. Does anyone know how to force CMake to propagate a setting to all of the descendants of a CMakeList.txt? |
@SebastianBoe Any idea why the compile commands would not be consistent with the ninja build file? |
I couldn't say why, but it seems that setting the variable from CMakeLists.txt is unofficially not supported. I would try setting it from CLI to ensure it is always set as a cache variable. |
@sslupsky Given the answer from Sebastian, try setting the compile command as was explained here: |
Awesome discussion all around! I can't help but thinking that we can capture this with some automation via a new VS Code extension. If some of the scaffolding and configuration was implemented in |
@joerchan Thank you for the tip. I can confirm that setting the CMAKE_EXPORT_COMPILE_COMMANDS from the command line works better than using the setting in the CMakeLists.txt file. Now all the zephyr components are included in the compile_commands.json file. Hooray! :-) One problem remains though. Some of the include paths do not resolve properly. For instance, #include <soc.h> comes up with a list of 76 definitions. This prevents anything that includes soc.h from linting properly with intellisense. Unfortunately, soc.h is quite a popular file to include ;-). I have raised an issue with the VSCode CMake Tools team about this problem since it appears to be specific to VSCode at the moment: Has anyone here been able to resolve this? |
For clarification for anyone reading this, I am using the Multi-root workspace in VSCode. This allows me to configure VSCode to work with projects that have multiple "sub projects". In my use case, I have root folders for: The multi root capability of VSCode is really quite helpful for Zephyr projects. Here is a copy of the .code-workspace file:
|
@SebastianBoe Thank you, the command line option resolved my issue with the compile_commands.json file not including all the zephyr targets. |
@beriberikix That is a great idea ... that would simplify the setup process enormously by eliminating the need for someone to learn about the details of configuring VSCode. I have grown to admire VSCode tremendously and it keeps getting better. I have one nagging issue related to git that I haven't been able to figure out. Seems like VSCode will occasionally max out the CPU in one of its' threads. The problem seems related to staging selected code changes but I am not completely certain that is the cause; rather it is likely just a coincidence. VSCode has a helpful command when this happens, "Developer: reload window". This is a lifesaver. You loose your undo history but pretty much everything else comes back to where you were minus the laggy responsiveness due to the high cpu usage. |
I published the Kconfig extension on the marketplace last week. It provides full language support for all Kconfig and .conf files out of the box, including code completion, go to definition, syntax highlighting and linting. It should improve the VS Code experience for Kconfig quite a bit for both application developers and stack maintainers. The extension depends on West v0.7.0 or newer. Older versions should work, but there may be some manual configuration required. Feel free to contact me on slack if you have any questions, issues or feature requests :) |
@trond-snekvik Thanks for this! I've been using it for the last few days and it's great. Hoping to see more zephyr support for VSCode in the future. |
@hongshui3000 Yes, that was supposed to be a feature but turned out to be an issue that hit users hard. We already have a workaround: if you use a manual toolchain installation we won't override the terminal environment. For a more complete solution we would like to introduce a separate terminal profile for NCS and leave the default one unmodified. Feel free to open issues on https://devzone.nordicsemi.com/ |
espressif has an open source implementation, but it does not seem to be based on zephyr.The idea is similar to that of nordic |
Might be interesting for people who are using WSL2 with vscode: |
Would the Nordic folks be so kind as to open-source their VSCode extensions? |
@ddavidebor Does the closed source nature of the extensions limits your usage in any way? |
@wbober not for Nordic products, but of course the extensions could be expanded to encompass a larger number of toolchains over time - acting as the starting point for full VSCode integration I totally understand if that's not viable for business reasons. |
I wish there was a zephyr community maintained vscode extension |
@ddavidebor There is no plan to open source the extension. We are open to providing a limited support for development on other platforms. If you are able to provide more concrete examples of things that are missing or limiting then I will take that feedback to the team. Perhaps there are ways to support by opening APIs for extension with plugins or alike. Can't make any promises but I'm open for a discussion. |
imports vsc |
I still see this issue: microsoft/vscode-cpptools#9187 E.g. whenever I use
|
@mbrunnen you can get around this by adding |
@trond-snekvik, I just tried that since I have the same issue, but I does not seem to have any effect, I still get the "too few arguments" warnings. My configuration looks something like this: {
"name": "app",
"cStandard": "c11",
"cppStandard": "c++20",
"intelliSenseMode": "linux-gcc-arm",
"compilerPath": "${env:zephyrCompilerPath}",
"compileCommands": "${workspaceFolder}/build/compile_commands.json",
"defines": [
"__CDT_PARSER__"
],
"forcedInclude": [
"${workspaceFolder}/build/autoconf.h"
]
}, |
@aunsbjerg according to the cpptools reference, compileCommands replaces all defines and includes, unfortunately. The extension only uses your custom defines and includes when it can't find the file you're editing in the compileCommands. I'm not sure the same applies to the forcedInclude though, so you might be able to achieve the same thing by creating a dummy header with this define and force include it. Without compileCommands, it works for me on the current main branch, because of this section: zephyr/include/zephyr/logging/log.h Lines 439 to 459 in 61b5566
For what it's worth, clangd's configuration system is a lot more flexible, as it allows you to add or remove defines and includes in a yaml config file. The syntax is a little hard to work out, but their extension has come a long way, and often works better than Microsoft's in my experience. |
Forced includes are also ignored when using compile_command.json. I played around with it, but couldn't really find a working solution. I'll give clangd a shot - thanks for the heads up and for taking the time to respond :) |
Hey @trond-snekvik and @aunsbjerg, thanks for the help. |
I started working on turning https://github.com/smrtos/Zephyr2VSC into a VSCode extension - "configurationProvider" - but oooooh boy things did not start smoothly! Sanity check: I am getting IntelliSense with "ms-vscode.cpptools" alone and simply defining {
"version": 4,
"configurations": [
{
"name": "zephyr-default",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
}
]
} This is nice, because I find cmake-tools cumbersome and the Nordic stuff completely unusable 😝. On the other hand, I'm sure that I haven't used this config enough to discover why it's insufficient! 😭 Looking forward to trying out the new offerings from Microsoft when I have time: https://marketplace.visualstudio.com/items?itemName=ms-vscode.vscode-embedded-tools Then again, maybe we should all just calm tf down before we turn VSCode into Eclipse... |
FYI @robotdad, who works on the Embedded Tools extension! |
@JPHutchins If you are interested in configurationProvider, you can also take a look at this extended method.Although it is not targeting zephyr https://github.com/Mossop/vscode-mozillacpp https://github.com/Mossop/vscode-mozillacpp/blob/main/src/provider.ts |
interestingly (and sadly) this also fails when running CodeQL against a Zephyr application :( |
Quite nice repository to get started with Zephyr debugging in vscode: |
You can also try mine: https://github.com/KozhinovAlexander/zephyr_vscode_workspace I am also open to support and commits. Just create an issue - I will be able to answer ASAP or support you. |
Note that there is a discussion regarding the same topic: |
Visual Studio Code is a really nice development environment, even someone such as myself who has stuck with vim for many years is considering switching over to it.
It would be cool if we could provide some documentation and/or VS Code extensions to make VS Code more zephyr-aware. This ticket can be a catch-all for integration tasks/ideas. So far I can think of:
Other ideas, please comment!
The text was updated successfully, but these errors were encountered: