diff mbox series

ast: Better variable history for builtin fragments

Message ID 20251013145033.2903356-1-yoann.congal@smile.fr
State New
Headers show
Series ast: Better variable history for builtin fragments | expand

Commit Message

Yoann Congal Oct. 13, 2025, 2:50 p.m. UTC
From: Yoann Congal <yoann.congal@smile.fr>

As of now, the variable history for builtin fragments looks like this
(edited for clarity):
   $ bitbake-getvar MACHINE
  #
  # $MACHINE [2 operations]
  #   set ast.py:368 [check_and_set_builtin_fragment]
  #     "qemux86-64"
  # pre-expansion value:
  #   "qemux86-64"
  MACHINE="qemux86-64"
User can't know where MACHINE was set, this is bad.

This patch tries to reconstruct a MACHINE history from OE_FRAGMENTS
history.
With this patch, history looks like this (for a simple case):
   $ bitbake-getvar MACHINE
  NOTE: Starting bitbake server...
  #
  # $MACHINE [2 operations]
  #   set .../auto.conf:2
  #     "qemux86-64 (OE_FRAGMENTS contains "machine/qemux86-64")"
  # pre-expansion value:
  #   "qemux86-64"
  MACHINE="qemux86-64"
The path where the "machine/qemux86-64" fragment was added to
OE_FRAGMENTS is displayed, this is definitely better.

Fixes [YOCTO #15939]

Signed-off-by: Yoann Congal <yoann.congal@smile.fr>
---
This was previously sent as RFC:
[RFC PATCH] ast: Better variable history for builtin fragments
https://lists.openembedded.org/g/bitbake-devel/topic/rfc_patch_ast_better/115674103

But, with:
[PATCH v3] ast: Warn on multiple builtin config fragments for the same variable
https://lists.openembedded.org/g/bitbake-devel/topic/patch_v3_ast_warn_on/115686012

I think it is ready to be merged.
---
 lib/bb/parse/ast.py | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/lib/bb/parse/ast.py b/lib/bb/parse/ast.py
index cb06e8917..27adbd860 100644
--- a/lib/bb/parse/ast.py
+++ b/lib/bb/parse/ast.py
@@ -364,8 +364,16 @@  class AddFragmentsNode(AstNode):
         def check_and_set_builtin_fragment(fragment, data, builtin_fragments):
             prefix, value = fragment.split('/', 1)
             if prefix in builtin_fragments.keys():
+                fragment_history = data.varhistory.variable(self.fragments_variable)
+                loginfo={}
+                for fh in fragment_history[::-1]:
+                    if fh['op'] in ("set", "append") and fragment in fh["detail"]:
+                        loginfo["file"]   = fh["file"]
+                        loginfo["line"]   = fh["line"]
+                        loginfo["detail"] = f"{value} ({self.fragments_variable} contains \"{fragment}\")"
+                        break
                 # parsing=True since we want to emulate X=Y and allow X:override=Z to continue to exist
-                data.setVar(builtin_fragments[prefix], value, parsing=True)
+                data.setVar(builtin_fragments[prefix], value, parsing=True, **loginfo)
                 return True
             return False