diff mbox series

[2/2] oe-setup-vscode: improve VSCode bitbake-setup compatibility

Message ID 20251224150805.3312684-3-adrian.freihofer@siemens.com
State New
Headers show
Series oe-setup-vscode: improve bitbake-setup compatibility | expand

Commit Message

AdrianF Dec. 24, 2025, 3:07 p.m. UTC
From: Adrian Freihofer <adrian.freihofer@siemens.com>

VSCode is started in the workspace folder where the .vscode directory
is located. With the new bitbake-setup based build layout, this starts
VSCode in the openembedded-core directory, which means only files in
that layer are visible. This is not usable for most projects which
span multiple layers.

Enhance the VSCode setup to work with the new bitbake-setup layout by
moving the .vscode directory location from
     poky-master/layers/oe-init-build-env-dir/.vscode
to
     poky-master/.vscode
bringing all layers back into the scope of VSCode.

The oe-init-build-env script now detects the build layout by checking
for the presence of a layers directory, and adjusts the .vscode
location accordingly.

Additionally, the file exclusion patterns have been refined to improve
VSCode performance:
- Separated files.exclude from search.exclude patterns for better control
- Added exclusions for oe-init-build-env-dir and oe-scripts directories
- Prevented indexing of build artifacts and temporary files

The configuration now also detects whether to use oe-init-build-env or
init-build-env based on the detected build mode.

Usage example:
1. bitbake-setup creates a directory structure like:
     poky-master/
       layers/
         openembedded-core/
         meta-yourlayer/
       build/
         init-build-env

2. Create the .vscode directory in poky-master by running:
   . build/init-build-env

   The settings.json contains:
    "bitbake.pathToBitbakeFolder": ".../poky-master/layers/bitbake",
    "bitbake.pathToBuildFolder": "${workspaceFolder}/build",
    "bitbake.pathToEnvScript": "${workspaceFolder}/build/init-build-env",
    "bitbake.workingDirectory": "${workspaceFolder}",

3. Start VSCode in poky-master

Note: Starting VSCode before the .vscode directory exists may cause
VSCode to index the entire workspace, leading to high CPU and memory
usage. Future improvements could include creating a minimal .vscode
directory during bitbake-setup initialization.

Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
---
 oe-init-build-env       | 16 ++++++++--
 scripts/oe-setup-vscode | 68 +++++++++++++++++++++++------------------
 2 files changed, 52 insertions(+), 32 deletions(-)
diff mbox series

Patch

diff --git a/oe-init-build-env b/oe-init-build-env
index 82382f2707..6547cdd297 100755
--- a/oe-init-build-env
+++ b/oe-init-build-env
@@ -49,8 +49,20 @@  export OEROOT
 }
 
 # Generate an initial configuration for VSCode and the yocto-bitbake plugin.
-if command -v code > /dev/null && [ ! -d "$OEROOT/.vscode" ]; then
-    oe-setup-vscode "$OEROOT" "$BUILDDIR"
+if command -v code > /dev/null ; then
+    # bitbake-setup mode
+    if [ -d "$BUILDDIR/../layers" ]; then
+        echo "bitbake-setup mode detected for VSCode setup."
+        if [ ! -d "$BUILDDIR/../.vscode" ]; then
+            oe-setup-vscode "$BUILDDIR/../" "$BUILDDIR"
+        fi
+    # poky mode
+    else
+        echo "poky mode detected for VSCode setup."
+        if [ ! -d "$OEROOT/.vscode" ]; then
+            oe-setup-vscode "$OEROOT" "$BUILDDIR"
+        fi
+    fi
 fi
 
 unset OEROOT
diff --git a/scripts/oe-setup-vscode b/scripts/oe-setup-vscode
index 6818641d42..0fea1c4808 100755
--- a/scripts/oe-setup-vscode
+++ b/scripts/oe-setup-vscode
@@ -20,47 +20,52 @@  if [ ! -d "$OEINIT" ] || [ ! -d "$BUILDDIR" ]; then
     exit 1
 fi
 
-VSCODE_SETTINGS=$VSCODEDIR/settings.json
+mkdir -p "$VSCODEDIR"
+VSCODE_SETTINGS_TMP=$(mktemp -p "$VSCODEDIR")
 ws_builddir="$(echo "$BUILDDIR" | sed -e "s|$OEINIT|\${workspaceFolder}|g")"
+
+# poky mode or bitbake-setup mode
+ws_build_env="\${workspaceFolder}/oe-init-build-env"
+if [ -f "$BUILDDIR/init-build-env" ]; then
+    ws_build_env="${ws_builddir}/init-build-env"
+fi
+
 bb_dir="$(dirname "$(which bitbake)" | sed 's|/bin$||')"
 
+files_exclude="\"**/_build/**\": true,
+        \"**/.git/**\": true,
+        \"**/cache/**\": true,
+        \"**/downloads/**\": true,
+        \"**/node_modules/**\": true,
+        \"**/oe-init-build-env-dir/**\": true,
+        \"**/oe-scripts/**\": true,
+        \"**/sstate-cache/**\": true,
+        \"**/workspace/attic/**\": true"
+
+search_exclude="\"**/buildhistory/**\": true,
+        \"**/oe-logs/**\": true,
+        \"**/oe-workdir/**\": true,
+        \"**/tmp*/**\": true,
+        \"**/workspace/sources/**\": true"
+
 # If BUILDDIR is in scope of VSCode ensure VSCode does not try to index the build folder.
 # This would lead to a busy CPU and finally to an OOM exception.
-mkdir -p "$VSCODEDIR"
-cat <<EOMsettings > "$VSCODE_SETTINGS"
+cat <<EOMsettings > "$VSCODE_SETTINGS_TMP"
 {
     "bitbake.pathToBitbakeFolder": "$bb_dir",
-    "bitbake.pathToEnvScript": "\${workspaceFolder}/oe-init-build-env",
     "bitbake.pathToBuildFolder": "$ws_builddir",
-    "bitbake.commandWrapper": "",
+    "bitbake.pathToEnvScript": "$ws_build_env",
     "bitbake.workingDirectory": "\${workspaceFolder}",
     "files.exclude": {
-        "**/.git/**": true,
-        "**/_build/**": true,
-        "**/buildhistory/**": true,
-        "**/cache/**": true,
-        "**/downloads/**": true,
-        "**/node_modules/**": true,
-        "**/oe-logs/**": true,
-        "**/oe-workdir/**": true,
-        "**/sstate-cache/**": true,
-        "**/tmp*/**": true,
-        "**/workspace/attic/**": true,
-        "**/workspace/sources/**": true
+        $files_exclude
+    },
+    "search.exclude": {
+        $files_exclude,
+        $search_exclude
     },
     "files.watcherExclude": {
-        "**/.git/**": true,
-        "**/_build/**": true,
-        "**/buildhistory/**": true,
-        "**/cache/**": true,
-        "**/downloads/**": true,
-        "**/node_modules/**": true,
-        "**/oe-logs/**": true,
-        "**/oe-workdir/**": true,
-        "**/sstate-cache/**": true,
-        "**/tmp*/**": true,
-        "**/workspace/attic/**": true,
-        "**/workspace/sources/**": true
+        $files_exclude,
+        $search_exclude
     },
     "python.analysis.exclude": [
         "**/_build/**",
@@ -74,11 +79,14 @@  cat <<EOMsettings > "$VSCODE_SETTINGS"
         "**/sstate-cache/**",
         "**/tmp*/**",
         "**/workspace/attic/**",
-        "**/workspace/sources/**"
+        "**/workspace/sources/**",
+        "**/oe-init-build-env-dir/**",
+        "**/oe-scripts/**"
     ]
 }
 EOMsettings
 
+mv -f "$VSCODE_SETTINGS_TMP" "$VSCODEDIR/settings.json"
 
 # Ask the user if the yocto-bitbake extension should be installed
 VSCODE_EXTENSIONS=$VSCODEDIR/extensions.json