Skip to contentSkip to conversation

[2.0] Appends discarded by expandVarref

bitbake Adam Blank 3 participants 6 messages Aug 29, 2024 – Sep 2, 2024

lore
Share & tools
lore

Adam Blank

author
#0 lore raw

Hi,

expandVarref is called during the parsing stage, to expand special purpose variables, in my case LAYERDIR.
Internally it calls setVar without letting it know that parsing is True, which removes any possible appends for the variable being processed by it. This has interesting side effects - I doubt they were intended, but fixing it shows how rife they are.

1. Real life example for BBMASK
- BBMASK:append:qemuarm64 = " meta-foo/recipes-bar"
- BBMASK += "${LAYERDIR}/recipes-kernel/linux/linux-yocto_5.15.bbappend"

When this is parsed in layer.conf, OVERRIDES have not been appended with 'qemuarm64' yet.
Before the change (self.setVar(key, referrervalue.replace(ref, value))):
The call to expandVarref ends up setting BBMASK to some appended value with LAYERDIR expanded, but removing the conditional append for qemuarm64. The qemuarm64 append although perfectly good syntax-wise, is never applied.
After the change (self.setVar(key, referrervalue.replace(ref, value), parsing=True)):
The call to expandVarref ends up setting BBMASK to some appended value with LAYERDIR expanded, the conditional append for qemuarm64 remains in place. As soon as the OVERRIDES start containing 'qemuarm64', the conditional append also starts being considered - as intended.
2. Real life example for BBFILES
- BBFILES:append = " ${LAYERDIR}/recipes-core/wpa-supplicant/wpa-supplicant_%.bbappend"

Before the change (self.setVar(key, referrervalue.replace(ref, value))):
The call to expandVarref ends up setting BBFILES to some appended value with LAYERDIR expanded, but removing the append. This time the effect is exactly as the author intended, but only accidentally - contrary to what the syntax would suggest. After the change (self.setVar(key, referrervalue.replace(ref, value), parsing=True)):
The call to expandVarref ends up setting BBFILES to some appended value with LAYERDIR expanded, the append remains in place. For every subsequently parsed layer, the BBFILES end up being appended with gibberish (based on its own LAYERDIR). Definitely not intended by the author.

So now I'm wondering what the situation is really. Are the above just examples of bad style? Perhaps it would be worth it to extend the documentation for LAYERDIR type variables, to warn about their special handling? My hunch is that this is a defect (at least the fact, that expandVarref is used truly during parsing, and setVar which it calls is not notified about it).
It can be easily circumvented by a simple indirection, i.e. placing the expressions with LAYERDIR in them in a separate variable, and then referencing this variable. But this feels like a hack.

Please let me know your opinions.
Thanks,
Adam

#1 lore raw
Quoted 23 lines · Adam Blank via lists.openembedded.org

expandVarref is called during the parsing stage, to expand special purpose variables, in my case LAYERDIR.
Internally it calls setVar without letting it know that parsing is True, which removes any possible appends for the variable being processed by it. This has interesting side effects - I doubt they were intended, but fixing it shows how rife they are.

1. Real life example for BBMASK
    - BBMASK:append:qemuarm64 = " meta-foo/recipes-bar"
    - BBMASK += "${LAYERDIR}/recipes-kernel/linux/linux-yocto_5.15.bbappend"

    When this is parsed in layer.conf, OVERRIDES have not been appended with 'qemuarm64' yet.
    Before the change (self.setVar(key, referrervalue.replace(ref, value))):
      The call to expandVarref ends up setting BBMASK to some appended value with LAYERDIR expanded, but removing the conditional append for qemuarm64. The qemuarm64 append although perfectly good syntax-wise, is never applied.
    After the change (self.setVar(key, referrervalue.replace(ref, value), parsing=True)):
      The call to expandVarref ends up setting BBMASK to some appended value with LAYERDIR expanded, the conditional append for qemuarm64 remains in place. As soon as the OVERRIDES start containing 'qemuarm64', the conditional append also starts being considered - as intended.
2. Real life example for BBFILES
    - BBFILES:append = " ${LAYERDIR}/recipes-core/wpa-supplicant/wpa-supplicant_%.bbappend"

    Before the change (self.setVar(key, referrervalue.replace(ref, value))):
      The call to expandVarref ends up setting BBFILES to some appended value with LAYERDIR expanded, but removing the append. This time the effect is exactly as the author intended, but only accidentally - contrary to what the syntax would suggest. After the change (self.setVar(key, referrervalue.replace(ref, value), parsing=True)):
      The call to expandVarref ends up setting BBFILES to some appended value with LAYERDIR expanded, the append remains in place.

Quoted 8 lines

      For every subsequently parsed layer, the BBFILES end up being appended with gibberish (based on its own LAYERDIR).
      Definitely not intended by the author.

So now I'm wondering what the situation is really. Are the above just examples of bad style? Perhaps it would be worth it to extend the documentation for LAYERDIR type variables, to warn about their special handling? My hunch is that this is a defect (at least the fact, that expandVarref is used truly during parsing, and setVar which it calls is not notified about it).
It can be easily circumvented by a simple indirection, i.e. placing the expressions with LAYERDIR in them in a separate variable, and then referencing this variable. But this feels like a hack.
Please let me know your opinions.

As you have realised, the way we expand LAYERDIR in layer.conf files has drawbacks. Ideally we'd find a different mechanism to do it with less side effects but nobody has been concerned enough to do that as yet. It probably is worth opening a bug for the issue.

As you've noted, we can't just add the parsing parameter as that just causes a different set of problems and you swap one set of problems for another.

To fix it we really need to avoid expanding any variables at all and just do a search/replace on the variable contents.

Or we drop this idea entirely and use a new convention for LAYERDIR, maybe changing the name to be layer specific.

Cheers,

Richard

#2 lore raw
Quoted 51 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Richard Purdie
Sent: den 29 augusti 2024 14:42
To: [email protected]; [email protected]
Subject: Re: [bitbake-devel] [bitbake][kirkstone][2.0] Appends discarded by expandVarref

Quoted 26 lines · Adam Blank via lists.openembedded.org

expandVarref is called during the parsing stage, to expand special purpose variables, in my case LAYERDIR.
Internally it calls setVar without letting it know that parsing is True, which removes any possible appends for the variable being processed by it. This has interesting side effects - I doubt they were intended, but fixing it shows how rife they are.

1. Real life example for BBMASK
    - BBMASK:append:qemuarm64 = " meta-foo/recipes-bar"
    - BBMASK += "${LAYERDIR}/recipes-kernel/linux/linux-yocto_5.15.bbappend"

    When this is parsed in layer.conf, OVERRIDES have not been appended with 'qemuarm64' yet.
    Before the change (self.setVar(key, referrervalue.replace(ref, value))):
      The call to expandVarref ends up setting BBMASK to some appended value with LAYERDIR expanded, but removing the conditional append for qemuarm64. The qemuarm64 append although perfectly good syntax-wise, is never applied.
    After the change (self.setVar(key, referrervalue.replace(ref, value), parsing=True)):
      The call to expandVarref ends up setting BBMASK to some appended value with LAYERDIR expanded, the conditional append for qemuarm64 remains in place. As soon as the OVERRIDES start containing 'qemuarm64', the conditional append also starts being considered - as intended.
2. Real life example for BBFILES
    - BBFILES:append = " ${LAYERDIR}/recipes-core/wpa-supplicant/wpa-supplicant_%.bbappend"

    Before the change (self.setVar(key, referrervalue.replace(ref, value))):
      The call to expandVarref ends up setting BBFILES to some appended value with LAYERDIR expanded, but removing the append. This time the effect is exactly as the author intended, but only accidentally - contrary to what the syntax would suggest. After the change (self.setVar(key, referrervalue.replace(ref, value), parsing=True)):
      The call to expandVarref ends up setting BBFILES to some appended value with LAYERDIR expanded, the append remains in place. For every subsequently parsed layer, the BBFILES end up being appended with gibberish (based on its own LAYERDIR). Definitely not intended by the author.

Quoted 5 lines

So now I'm wondering what the situation is really. Are the above just examples of bad style? Perhaps it would be worth it to extend the documentation for LAYERDIR type variables, to warn about their special handling? My hunch is that this is a defect (at least the fact, that expandVarref is used truly during parsing, and setVar which it calls is not notified about it).
It can be easily circumvented by a simple indirection, i.e. placing the expressions with LAYERDIR in them in a separate variable, and then referencing this variable. But this feels like a hack.

Please let me know your opinions.

As you have realised, the way we expand LAYERDIR in layer.conf files has drawbacks. Ideally we'd find a different mechanism to do it with less side effects but nobody has been concerned enough to do that as yet. It probably is worth opening a bug for the issue.

As you've noted, we can't just add the parsing parameter as that just causes a different set of problems and you swap one set of problems for another.

To fix it we really need to avoid expanding any variables at all and just do a search/replace on the variable contents.

Quoted 6 lines

Or we drop this idea entirely and use a new convention for LAYERDIR, maybe changing the name to be layer specific.

Cheers,

Richard

Regarding a layer specific LAYERDIR, we do this in layer.conf for all our layers:

BBFILE_COLLECTIONS += "foobar"
...
LAYERDIR_foobar := "${LAYERDIR}"
LAYERDIR_foobar[vardepvalue] = ""

This gives us a way to access files from one layer in another layer.

//Peter

author
#3 lore raw
In reply to Richard Purdie
Quoted 4 lines

To fix it we really need to avoid expanding any variables at all and just do a search/replace on the variable contents.

I'd like to propose something like that:

lib/bb/data_smart.py +0 -0
@@ -1007,9 +1007,34 @@ class DataSmart(MutableMapping):

ref = '${%s}' % variable
value = self.getVar(variable, False)
for key in keys:
-            referrervalue = self.getVar(key, False)
-            if referrervalue and isinstance(referrervalue, str) and ref in referrervalue:
-                self.setVar(key, referrervalue.replace(ref, value))
+            cow = False
+            if not key in self.dict:
+                local_var, _ = self._findVar(key)
+                cow = True
+            else:
+                local_var = self.dict[key]
+
+            for sKey in ["_content", "_defaultval", ":append", ":prepend", ":remove"]:
+                val = local_var[sKey] if sKey in local_var else None
+
+                if isinstance(val, str) and ref in val:
+                    val2 = val.replace(ref, value)
+                    if cow:
+                        self._makeShadowCopy(key)
+                        cow = False
+                    self.dict[key][sKey] = val2
+                elif isinstance(val, list):
+                    val2 = None
+                    for i, (r, o) in enumerate(val):
+                        if ref in r:
+                            if not val2:
+                                val2 = list(val)
+                            val2[i] = [r.replace(ref, value), o]
+                    if val2:
+                        if cow:
+                            self._makeShadowCopy(key)
+                            cow = False
+                        self.dict[key][sKey] = val2

Efficiency wise it does not do more that the original code, and it gets straight to the point.

Thanks,
Adam

#4 lore raw
Quoted 39 lines · Adam Blank via lists.openembedded.org
Quoted 3 lines

To fix it we really need to avoid expanding any variables at all and
just do a search/replace on the variable contents.

I'd like to propose something like that:

lib/bb/data_smart.py +0 -0
@@ -1007,9 +1007,34 @@ class DataSmart(MutableMapping):

         ref = '${%s}' % variable
         value = self.getVar(variable, False)
         for key in keys:
-            referrervalue = self.getVar(key, False)
-            if referrervalue and isinstance(referrervalue, str) and ref in referrervalue:
-                self.setVar(key, referrervalue.replace(ref, value))
+            cow = False
+            if not key in self.dict:
+                local_var, _ = self._findVar(key)
+                cow = True
+            else:
+                local_var = self.dict[key]
+
+            for sKey in ["_content", "_defaultval", ":append", ":prepend", ":remove"]:
+                val = local_var[sKey] if sKey in local_var else None +
+                if isinstance(val, str) and ref in val:
+                    val2 = val.replace(ref, value)
+                    if cow:
+                        self._makeShadowCopy(key)
+                        cow = False
+                    self.dict[key][sKey] = val2
+                elif isinstance(val, list):
+                    val2 = None
+                    for i, (r, o) in enumerate(val):
+                        if ref in r:
+                            if not val2:
+                                val2 = list(val)
+                            val2[i] = [r.replace(ref, value), o]

Quoted 8 lines

+                    if val2:
+                        if cow:
+                            self._makeShadowCopy(key)
+                            cow = False
+                        self.dict[key][sKey] = val2

Efficiency wise it does not do more that the original code, and it gets straight to the point.

The downside of this is that firstly, it is using a lot of internal knowledge of the data store which I worry may make changes more fragile in that code.

Secondly, it does break some usages, for example:

ABC = "${@d.getVar('LAYERDIR')}"

would no longer work after that change (as a simple example).

I'm torn on this but I would note that you're sending the patch for kirkstone and we definitely can't make a breaking change like that in kirkstone at this point.

Cheers,

Richard

author
#5 lore raw

Allright, I get the idea and share the sense of the associated risk. For the sake of formality though, I don't think that

ABC = "${@d.getVar('LAYERDIR')}"

has any point of contact with my proposal :-) Both before and after the change, there's this line:

ref = '${%s}' % variable

which by the way renders this d.getVar() expression unusable for LAYERDIR (always yields None).

Thanks for the discussion!
Adam

Quoted 68 lines · Richard Purdie
Quoted 45 lines · Adam Blank via lists.openembedded.org
Quoted 3 lines

To fix it we really need to avoid expanding any variables at all and
just do a search/replace on the variable contents.

I'd like to propose something like that:

lib/bb/data_smart.py +0 -2
@@ -1007,9 +1007,34 @@ class DataSmart(MutableMapping):
         ref = '${%s}' % variable
         value = self.getVar(variable, False)
         for key in keys:
-            referrervalue = self.getVar(key, False)
-            if referrervalue and isinstance(referrervalue, str) and

ref in referrervalue:

lib/bb/data_smart.py +8 -1
-                self.setVar(key, referrervalue.replace(ref, value))
+            cow = False
+            if not key in self.dict:
+                local_var, _ = self._findVar(key)
+                cow = True
+            else:
+                local_var = self.dict[key]
+
+            for sKey in ["_content", "_defaultval", ":append",

":prepend", ":remove"]:

lib/bb/data_smart.py +20 -0
+                val = local_var[sKey] if sKey in local_var else None
+
+                if isinstance(val, str) and ref in val:
+                    val2 = val.replace(ref, value)
+                    if cow:
+                        self._makeShadowCopy(key)
+                        cow = False
+                    self.dict[key][sKey] = val2
+                elif isinstance(val, list):
+                    val2 = None
+                    for i, (r, o) in enumerate(val):
+                        if ref in r:
+                            if not val2:
+                                val2 = list(val)
+                            val2[i] = [r.replace(ref, value), o]
+                    if val2:
+                        if cow:
+                            self._makeShadowCopy(key)
+                            cow = False
+                        self.dict[key][sKey] = val2
Quoted 2 lines

Efficiency wise it does not do more that the original code, and it gets straight to the point.

The downside of this is that firstly, it is using a lot of internal knowledge of the data store which I worry may make changes more fragile in that code.

Secondly, it does break some usages, for example:

ABC = "${@d.getVar('LAYERDIR')}"

would no longer work after that change (as a simple example).

I'm torn on this but I would note that you're sending the patch for kirkstone and we definitely can't make a breaking change like that in kirkstone at this point.

Cheers,

Richard