Skip to contentSkip to conversation

bitbake-getvar: Do not output anything with --value and undefined variable

PATCH In review Peter Kjellerstedt 4 participants 10 messages Sep 25, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bitbake-getvar: Do not output anything with --value and undefined variable

Rather than outputting the string "None" when an unknown variable is used together with --value, output nothing. This is consistent with the no --value case where only a comment stating that the variable is not known is output.

This also means it is now possible to differentiate between an undefined variable and an empty variable since the empty variable will be output as a linefeed.

---
 bitbake/bin/bitbake-getvar | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
bitbake/bin/bitbake-getvar +5 -2
index 4a9eb4f311..806bbd8e4e 100755
@@ -43,9 +43,12 @@ if __name__ == "__main__":
         else:
             tinfoil.prepare(quiet=2, config_only=True)
             d = tinfoil.config_data
+        value = None
         if args.flag:
-            print(str(d.getVarFlag(args.variable, args.flag, expand=(not args.unexpand))))
+            value = d.getVarFlag(args.variable, args.flag, expand=(not args.unexpand))
         elif args.value:
-            print(str(d.getVar(args.variable, expand=(not args.unexpand))))
+            value = d.getVar(args.variable, expand=(not args.unexpand))
         else:
             bb.data.emit_var(args.variable, d=d, all=True)
+        if value is not None:
+            print(str(value))
#1 lore raw
Quoted 23 lines · Peter Kjellerstedt

Rather than outputting the string "None" when an unknown variable is used together with --value, output nothing. This is consistent with the no --value case where only a comment stating that the variable is not known is output.

This also means it is now possible to differentiate between an undefined variable and an empty variable since the empty variable will be output as a linefeed.

---
 bitbake/bin/bitbake-getvar | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
bitbake/bin/bitbake-getvar +1 -0
index 4a9eb4f311..806bbd8e4e 100755
@@ -43,9 +43,12 @@ if __name__ == "__main__":
         else:
             tinfoil.prepare(quiet=2, config_only=True)
             d = tinfoil.config_data
+        value = None

bitbake/bin/bitbake-getvar if __name__ == "__main__": · line 54

Quoted 10 lines
bitbake/bin/bitbake-getvar +4 -2
         if args.flag:
-            print(str(d.getVarFlag(args.variable, args.flag, expand=(not args.unexpand))))
+            value = d.getVarFlag(args.variable, args.flag, expand=(not args.unexpand))
         elif args.value:
-            print(str(d.getVar(args.variable, expand=(not args.unexpand))))
+            value = d.getVar(args.variable, expand=(not args.unexpand))
         else:
             bb.data.emit_var(args.variable, d=d, all=True)
+        if value is not None:
+            print(str(value))

Whilst I can see where you're going with this, I'm worried users will be confused by it.

getVar within bitbake returns None for things which do not exist. Not many users will notice the difference between linefeed output and no linefeed. It might be better to do something more obvious like an error exit code but that also has challenges. Variables not existing when the user expects them to is something we need to make really clear.

It was issues like this which stopped us implementing bitbake-getvar for a lot of years too :/.

Cheers,

Richard

#2 lore raw
Quoted 35 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 11:22
To: Peter Kjellerstedt <[email protected]>; [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 28 lines · Peter Kjellerstedt

Rather than outputting the string "None" when an unknown variable is used together with --value, output nothing. This is consistent with the no --value case where only a comment stating that the variable is not known is output.

This also means it is now possible to differentiate between an undefined variable and an empty variable since the empty variable will be output as a linefeed.

---
 bitbake/bin/bitbake-getvar | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
bitbake/bin/bitbake-getvar +2 -2
index 4a9eb4f311..806bbd8e4e 100755
@@ -43,9 +43,12 @@ if __name__ == "__main__":
         else:
             tinfoil.prepare(quiet=2, config_only=True)
             d = tinfoil.config_data
+        value = None
         if args.flag:
-            print(str(d.getVarFlag(args.variable, args.flag, expand=(not args.unexpand))))
+            value = d.getVarFlag(args.variable, args.flag, expand=(not args.unexpand))
         elif args.value:
-            print(str(d.getVar(args.variable, expand=(not args.unexpand))))
Quoted 8 lines

bitbake/bin/bitbake-getvar if __name__ == "__main__": · line 54

Quoted 5 lines
bitbake/bin/bitbake-getvar +3 -0
+            value = d.getVar(args.variable, expand=(not args.unexpand))
         else:
             bb.data.emit_var(args.variable, d=d, all=True)
+        if value is not None:
+            print(str(value))

Whilst I can see where you're going with this, I'm worried users will be confused by it.

Too be honest, I expect them to be much more confused if they get the string "None" back when asking for the value of a variable, which happens to not be defined.

Quoted 7 lines

getVar within bitbake returns None for things which do not exist. Not many users will notice the difference between linefeed output and no linefeed. It might be better to do something more obvious like an error exit code but that also has challenges. Variables not existing when the user expects them to is something we need to make really clear.

Quoted 6 lines

It was issues like this which stopped us implementing bitbake-getvar for a lot of years too :/.

Cheers,

Richard

My goal here was not really to be able to determine if the variable is defined or not, but to not get the string "None" back as a result of asking for an undefined variable, because that is really problematic when bitbake-getvar --value is used from a script or similar. This is because there is no way to determine (from the value) whether the variable is undefined, or if it is defined and has the actual value "None".

My assumption is that for most people the difference will not matter and the reason for that is that since if you use bitbake-getvar --value in a script or similar, you typically strip the linefeed that is output after the variable so you get the actual value, and then there is no difference in the result whether there was a linefeed (if the variable was defined, but empty) or there was no linefeed (if the variable was not defined). In both cases the result will be an empty string.

The reason I stumbled upon this was that I was working on a script that uses bitbake-getvar --value to get a variable with a path to a file, and then my script is expected to read that file. And I was a bit surprised when I tested it and I got an error stating that it failed to open the file "None"...

Now of course I can write something like:

	myfile=$(bitbake-getvar --value MYFILE)
	if [ "$myfile" ] && [ "$myfile" != None ]; then

but it feels much more natural (shell wise) to be able to write:

	myfile=$(bitbake-getvar --value MYFILE)
	if [ "$myfile" ]; then

as that matches the expectations (that at least I have) for all CLI commands that are supposed to return a value that may not exist.

//Peter

#3 lore raw
Quoted 58 lines · Peter Kjellerstedt
Quoted 35 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 11:22
To: Peter Kjellerstedt <[email protected]>; [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 28 lines · Peter Kjellerstedt

Rather than outputting the string "None" when an unknown variable is used together with --value, output nothing. This is consistent with the no --value case where only a comment stating that the variable is not known is output.

This also means it is now possible to differentiate between an undefined variable and an empty variable since the empty variable will be output as a linefeed.

---
 bitbake/bin/bitbake-getvar | 7 +++++--

 1 file changed, 5 insertions(+), 2 deletions(-)
bitbake/bin/bitbake-getvar +0 -0
index 4a9eb4f311..806bbd8e4e 100755
@@ -43,9 +43,12 @@ if __name__ == "__main__":

         else:
             tinfoil.prepare(quiet=2, config_only=True) d = tinfoil.config_data
+ value = None
         if args.flag:

bitbake/bin/bitbake-getvar +1 -1
-            print(str(d.getVarFlag(args.variable, args.flag, expand=(not args.unexpand))))
+            value = d.getVarFlag(args.variable, args.flag, expand=(not args.unexpand))

         elif args.value:
- print(str(d.getVar(args.variable, expand=(not args.unexpand))))

Quoted 8 lines

bitbake/bin/bitbake-getvar if __name__ == "__main__": · line 54

Quoted 5 lines

+ value = d.getVar(args.variable, expand=(not args.unexpand))
         else:
             bb.data.emit_var(args.variable, d=d, all=True)

bitbake/bin/bitbake-getvar +2 -0
+        if value is not None:
+            print(str(value))

Whilst I can see where you're going with this, I'm worried users will be confused by it.

Too be honest, I expect them to be much more confused if they get the string "None" back when asking for the value of a variable, which happens to not be defined.

Quoted 10 lines

getVar within bitbake returns None for things which do not exist. Not many users will notice the difference between linefeed output and no linefeed. It might be better to do something more obvious like an error exit code but that also has challenges. Variables not existing when the user expects them to is something we need to make really clear.

It was issues like this which stopped us implementing bitbake-getvar for a lot of years too :/.

Quoted 6 lines
Quoted 3 lines

Cheers,

Richard

My goal here was not really to be able to determine if the variable is defined or not

I know, but that is an important detail and whilst it may not matter to you, it does matter a lot.

Quoted 3 lines

, but to not get the string "None" back as a result of asking for an undefined variable, because that is really problematic when bitbake-getvar --value is used from a script or similar.

I do agree on that. I just don't agree we should hide "undefined" as we're replacing one bad problem with equally bad one.

Quoted 4 lines

This
is because there is no way to determine (from the value) whether the variable is undefined, or if it is defined and has the actual value "None".

FWIW I'm not aware of any variable having "None" as a valid value.

Quoted 7 lines

My assumption is that for most people the difference will not matter and the reason for that is that since if you use bitbake-getvar --value in a script or similar, you typically strip the linefeed that is output after the variable so you get the actual value, and then there is no difference in the result whether there was a linefeed (if the variable was defined, but empty) or there was no linefeed (if the variable was not defined). In both cases the result will be an empty string.

This is a problem as users likely should care if the variable is not set at all, it suggests bigger problems.

Cheers,

Richard

#4 lore raw
Quoted 67 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 12:30
To: Peter Kjellerstedt <[email protected]>; [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 60 lines · Peter Kjellerstedt
Quoted 35 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 11:22
To: Peter Kjellerstedt <[email protected]>; [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 28 lines · Peter Kjellerstedt

Rather than outputting the string "None" when an unknown variable is used together with --value, output nothing. This is consistent with the no --value case where only a comment stating that the variable is not known is output.

This also means it is now possible to differentiate between an undefined variable and an empty variable since the empty variable will be output as a linefeed.

---
 bitbake/bin/bitbake-getvar | 7 +++++--

 1 file changed, 5 insertions(+), 2 deletions(-)
bitbake/bin/bitbake-getvar +0 -0
index 4a9eb4f311..806bbd8e4e 100755
@@ -43,9 +43,12 @@ if __name__ == "__main__":

         else:
             tinfoil.prepare(quiet=2, config_only=True) d = tinfoil.config_data
+ value = None
         if args.flag:

bitbake/bin/bitbake-getvar +1 -1
-            print(str(d.getVarFlag(args.variable, args.flag, expand=(not args.unexpand))))
+            value = d.getVarFlag(args.variable, args.flag, expand=(not args.unexpand))

         elif args.value:
- print(str(d.getVar(args.variable, expand=(not args.unexpand))))

Quoted 8 lines

bitbake/bin/bitbake-getvar if __name__ == "__main__": · line 54

Quoted 5 lines

+ value = d.getVar(args.variable, expand=(not args.unexpand))
         else:
             bb.data.emit_var(args.variable, d=d, all=True)

bitbake/bin/bitbake-getvar +2 -0
+        if value is not None:
+            print(str(value))

Whilst I can see where you're going with this, I'm worried users will be confused by it.

Too be honest, I expect them to be much more confused if they get the string "None" back when asking for the value of a variable, which happens to not be defined.

Quoted 12 lines

getVar within bitbake returns None for things which do not exist. Not many users will notice the difference between linefeed output and no linefeed. It might be better to do something more obvious like an error exit code but that also has challenges. Variables not existing when the user expects them to is something we need to make really clear.

It was issues like this which stopped us implementing bitbake-getvar for a lot of years too :/.

Cheers,

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Richard

My goal here was not really to be able to determine if the variable is defined or not

I know, but that is an important detail and whilst it may not matter to you, it does matter a lot.

Well, as I see it, with my change it _is_ now possible to make the distinction if you really care, where before you could only assume that a returned value of "None" actually meant that the variable is not defined.

And for the common case where you just want the value of a variable and do not care if it is not defined or if it is defined as the empty string, things just work. Compare the behavior when you write something like echo "$VAR" in a shell script, where most of the time you do not make any distinction on whether the variable VAR is defined or not. This is the same expectation I have for a command line tool that exposes an API to return the value of a variable. I.e., it should return the value and nothing else (and definitely not logs as my other patch series took care of).

Quoted 7 lines
Quoted 4 lines

, but to not get the string "None" back as a result of asking for an undefined variable, because that is really problematic when bitbake-getvar --value is used from a script or similar.

I do agree on that. I just don't agree we should hide "undefined" as we're replacing one bad problem with equally bad one.

Would an update to the help output from bitbake-getvar --help to make the distinction documented make you more comfortable about this change?

Quoted 7 lines
Quoted 5 lines

This
is because there is no way to determine (from the value) whether the variable is undefined, or if it is defined and has the actual value "None".

FWIW I'm not aware of any variable having "None" as a valid value.

Neither do I. But having to explicitly test for it everywhere you use bitbake-getvar --value is awkward.

Quoted 11 lines
Quoted 8 lines

My assumption is that for most people the difference will not matter and the reason for that is that since if you use bitbake-getvar --value in a script or similar, you typically strip the linefeed that is output after the variable so you get the actual value, and then there is no difference in the result whether there was a linefeed (if the variable was defined, but empty) or there was no linefeed (if the variable was not defined). In both cases the result will be an empty string.

This is a problem as users likely should care if the variable is not set at all, it suggests bigger problems.

Well, in my experience, in most cases it is not necessary to make the distinction. Compare to how shell variables, and, in most cases, bitbake variables are used. Yes, there can definitely be cases where you need to make a distinction, and then you want it to be definitive, i.e., you do not want to assume that a value of "None" actually means that the variable is not defined, while technically it would be a legal value for the variable.

Quoted 4 lines

Cheers,

Richard

//Peter

#5 lore raw
Quoted 16 lines · Peter Kjellerstedt
Quoted 16 lines

From: Richard Purdie <[email protected]>

Quoted 13 lines · Peter Kjellerstedt
Quoted 9 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 11:22
To: Peter Kjellerstedt <[email protected]>; [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

My goal here was not really to be able to determine if the variable is
defined or not

Quoted 8 lines
Quoted 3 lines

I know, but that is an important detail and whilst it may not matter to
you, it does matter a lot.

Well, as I see it, with my change it _is_ now possible to make the distinction if you really care, where before you could only assume that a returned value of "None" actually meant that the variable is not defined.

As things currently stand it is clear something went wrong through the process you yourself mentioned as how noticed. Once your proposed change merges, it becomes very unclear what is happening.

I'm not disagreeing that it needs to be fixed somehow we agree on that and I've already said as much. What we disagree on is how to fix it. I disagree on this way of addressing it.

I'm not taking this patch as it stands.

Cheers,

Richard

#6 lore raw
Quoted 29 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 16:07
To: Peter Kjellerstedt <[email protected]>; bitbake- [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 20 lines · Peter Kjellerstedt
Quoted 19 lines

From: Richard Purdie <[email protected]>

Quoted 10 lines · Peter Kjellerstedt
Quoted 9 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 11:22
To: Peter Kjellerstedt <[email protected]>; [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 3 lines

My goal here was not really to be able to determine if the variable is
defined or not

I know, but that is an important detail and whilst it may not matter to
you, it does matter a lot.

Quoted 8 lines
Quoted 4 lines

Well, as I see it, with my change it _is_ now possible to make the distinction if you really care, where before you could only assume that a returned value of "None" actually meant that the variable is not defined.

As things currently stand it is clear something went wrong through the process you yourself mentioned as how noticed. Once your proposed change merges, it becomes very unclear what is happening.

The documentation (i.e., bitbake-getvar -h) for --value states "Only report the value, no history and no variable name". When I read that, I instinctly assume that it will not output anything if I ask for a variable that does not exist, not that it will output something that looks like a valid value (i.e., "None"), but isn't.

I would be fine with an error message to stderr, but then I would assume the same result if bitbake-getvar is called without --value for an undefined variable. I would also prefer if it is possible to silence the error for the case where you do not care if the variable is undefined.

Whether bitbake-getvar should actually fail with an error in case the variable does not exist is more debatable. I can see use cases both ways.

Quoted 3 lines

I'm not disagreeing that it needs to be fixed somehow we agree on that and I've already said as much. What we disagree on is how to fix it. I disagree on this way of addressing it.

Based on my comments above, here is my suggestion:

When an undefined variable is requested, give an error message, e.g.:

$ bitbake-getvar UNDEFINED_VAR
The variable 'UNDEFINED_VAR' is not defined! [on stderr]

and set the exit code to 1. And the same result with --value:

$ bitbake-getvar --value UNDEFINED_VAR
The variable 'UNDEFINED_VAR' is not defined! [on stderr]

If -s or --no-messages (borrowed from grep) is also specified, then no error message is output and the exit code is 0.

Quoted 5 lines

I'm not taking this patch as it stands.

Cheers,

Richard

//Peter

#7 lore raw
Quoted 42 lines · Peter Kjellerstedt
Quoted 37 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 16:07
To: Peter Kjellerstedt <[email protected]>; bitbake- [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 20 lines · Peter Kjellerstedt
Quoted 12 lines

From: Richard Purdie <[email protected]>

Quoted 10 lines · Peter Kjellerstedt
Quoted 9 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 11:22
To: Peter Kjellerstedt <[email protected]>; [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 7 lines
Quoted 3 lines

My goal here was not really to be able to determine if the variable is
defined or not

I know, but that is an important detail and whilst it may not matter to
you, it does matter a lot.

Quoted 7 lines

Well, as I see it, with my change it _is_ now possible to make the
distinction if you really care, where before you could only assume
that a returned value of "None" actually meant that the variable is
not defined.

Quoted 4 lines

As things currently stand it is clear something went wrong through the
process you yourself mentioned as how noticed. Once your proposed change merges, it becomes very unclear what is happening.

Quoted 5 lines

The documentation (i.e., bitbake-getvar -h) for --value states "Only report the value, no history and no variable name". When I read that, I instinctly assume that it will not output anything if I ask for a variable that does not exist, not that it will output something that looks like a valid value (i.e., "None"), but isn't.

Please stop trying to convince me the current behaviour is wrong. I've said multiple times, I agree. What I don't agree with is how you're fixing it.

Quoted 3 lines

I would be fine with an error message to stderr, but then I would assume the same result if bitbake-getvar is called without --value for an undefined variable.

I agree with that.

Quoted 2 lines

I would also prefer if it is possible to silence the error for the
case where you do not care if the variable is undefined.

I only agree with this if the user specifically asks for that behaviour as I think it is a perfect way to end up with obscure bugs otherwise.

Quoted 3 lines

Whether bitbake-getvar should actually fail with an error in case the variable does not exist is more debatable. I can see use cases both ways.

I can see the use case both ways but I think the user has to specifically ask for undefined to result in "".

Quoted 10 lines
Quoted 5 lines

I'm not disagreeing that it needs to be fixed somehow we agree on that
and I've already said as much. What we disagree on is how to fix it. I
disagree on this way of addressing it.

Based on my comments above, here is my suggestion:

When an undefined variable is requested, give an error message, e.g.:

Quoted 7 lines
$ bitbake-getvar UNDEFINED_VAR
The variable 'UNDEFINED_VAR' is not defined! [on stderr]

and set the exit code to 1. And the same result with --value:

$ bitbake-getvar --value UNDEFINED_VAR
The variable 'UNDEFINED_VAR' is not defined! [on stderr]

That seems reasonable to me, without the "!" though.

Quoted 2 lines

If -s or --no-messages (borrowed from grep) is also specified, then no error message is output and the exit code is 0.

I'd suggest only a long option --ignore-undefined. That way when you see it in a shell script, you know exactly what risk is being taken.

Cheers,

Richard

#8 lore raw

Hi,

Quoted 90 lines · On Mon, 25 Sep 2023 17:17:53 +0100 "Richard Purdie"…
Quoted 42 lines · Peter Kjellerstedt
Quoted 37 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 16:07
To: Peter Kjellerstedt <[email protected]>; bitbake- [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 20 lines · Peter Kjellerstedt
Quoted 12 lines

From: Richard Purdie <[email protected]>

Quoted 10 lines · Peter Kjellerstedt
Quoted 9 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 11:22
To: Peter Kjellerstedt <[email protected]>; [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 7 lines
Quoted 3 lines

My goal here was not really to be able to determine if the variable is
defined or not

I know, but that is an important detail and whilst it may not matter to
you, it does matter a lot.

Quoted 7 lines

Well, as I see it, with my change it _is_ now possible to make the
distinction if you really care, where before you could only assume
that a returned value of "None" actually meant that the variable is
not defined.

Quoted 4 lines

As things currently stand it is clear something went wrong through the
process you yourself mentioned as how noticed. Once your proposed change merges, it becomes very unclear what is happening.

Quoted 5 lines

The documentation (i.e., bitbake-getvar -h) for --value states "Only report the value, no history and no variable name". When I read that, I instinctly assume that it will not output anything if I ask for a variable that does not exist, not that it will output something that looks like a valid value (i.e., "None"), but isn't.

Please stop trying to convince me the current behaviour is wrong. I've said multiple times, I agree. What I don't agree with is how you're fixing it.

Quoted 3 lines

I would be fine with an error message to stderr, but then I would assume the same result if bitbake-getvar is called without --value for an undefined variable.

I agree with that.

Quoted 2 lines

I would also prefer if it is possible to silence the error for the
case where you do not care if the variable is undefined.

I only agree with this if the user specifically asks for that behaviour as I think it is a perfect way to end up with obscure bugs otherwise.

Quoted 3 lines

Whether bitbake-getvar should actually fail with an error in case the variable does not exist is more debatable. I can see use cases both ways.

I can see the use case both ways but I think the user has to specifically ask for undefined to result in "".

Quoted 10 lines
Quoted 5 lines

I'm not disagreeing that it needs to be fixed somehow we agree on that
and I've already said as much. What we disagree on is how to fix it. I
disagree on this way of addressing it.

Based on my comments above, here is my suggestion:

When an undefined variable is requested, give an error message, e.g.:

Quoted 7 lines
$ bitbake-getvar UNDEFINED_VAR
The variable 'UNDEFINED_VAR' is not defined! [on stderr]

and set the exit code to 1. And the same result with --value:

$ bitbake-getvar --value UNDEFINED_VAR
The variable 'UNDEFINED_VAR' is not defined! [on stderr]
Quoted 7 lines

That seems reasonable to me, without the "!" though.

Quoted 2 lines

If -s or --no-messages (borrowed from grep) is also specified, then no error message is output and the exit code is 0.

I'd suggest only a long option --ignore-undefined. That way when you see it in a shell script, you know exactly what risk is being taken.

How about providing a --default parameter? The behavior of bitbake-getvar would then be:

* If the requested variable is defined, print its value and exit zero (in this case --default is irrelevant).

* If the requested variable is not defined and --default <value> is provided, print <value> and exit zero.

* If the requested variable is not defined and --default is not provided, exit non-zero.

Rationale:

* The proposed behavior/API allows for:

* Detecting undefined variables.

* Controlling when errors should be raised.

* Using custom fallback values for undefined variables.

All the best.
Mario

Signature · 1 line
http://parenteses.org/mario
#9 lore raw
Quoted 7 lines

-----Original Message-----
From: Mario Domenech Goulart <[email protected]>
Sent: den 25 september 2023 20:39
To: Richard Purdie <[email protected]>
Cc: Peter Kjellerstedt <[email protected]>; [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 44 lines

Hi,

Quoted 40 lines · On Mon, 25 Sep 2023 17:17:53 +0100 "Richard Purdie"…
Quoted 30 lines · Peter Kjellerstedt
Quoted 21 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 16:07
To: Peter Kjellerstedt <[email protected]>; [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

Quoted 14 lines · Peter Kjellerstedt
Quoted 13 lines

From: Richard Purdie <[email protected]>

Quoted 8 lines · Peter Kjellerstedt
Quoted 5 lines

-----Original Message-----
From: Richard Purdie <[email protected]>
Sent: den 25 september 2023 11:22
To: Peter Kjellerstedt <[email protected]>; [email protected]
Subject: Re: [bitbake-devel] [PATCH] bitbake-getvar: Do not output anything with --value and undefined variable

My goal here was not really to be able to determine if the variable is defined or not

I know, but that is an important detail and whilst it may not matter to you, it does matter a lot.

Quoted 8 lines
Quoted 4 lines

Well, as I see it, with my change it _is_ now possible to make the distinction if you really care, where before you could only assume that a returned value of "None" actually meant that the variable is not defined.

As things currently stand it is clear something went wrong through the process you yourself mentioned as how noticed. Once your proposed change merges, it becomes very unclear what is happening.

Quoted 5 lines

The documentation (i.e., bitbake-getvar -h) for --value states "Only report the value, no history and no variable name". When I read that, I instinctly assume that it will not output anything if I ask for a variable that does not exist, not that it will output something that looks like a valid value (i.e., "None"), but isn't.

Please stop trying to convince me the current behaviour is wrong. I've said multiple times, I agree. What I don't agree with is how you're fixing it.

Sorry. I was probably still in "why doesn't he see it my way" mode when I wrote that, before I turned to "ok, we can find a solution for this" mode.

Quoted 59 lines
Quoted 35 lines
Quoted 3 lines

I would be fine with an error message to stderr, but then I would assume the same result if bitbake-getvar is called without --value for an undefined variable.

I agree with that.

Quoted 2 lines

I would also prefer if it is possible to silence the error for the
case where you do not care if the variable is undefined.

I only agree with this if the user specifically asks for that behaviour as I think it is a perfect way to end up with obscure bugs otherwise.

Quoted 3 lines

Whether bitbake-getvar should actually fail with an error in case the variable does not exist is more debatable. I can see use cases both ways.

I can see the use case both ways but I think the user has to specifically ask for undefined to result in "".

Quoted 8 lines
Quoted 3 lines

I'm not disagreeing that it needs to be fixed somehow we agree on that and I've already said as much. What we disagree on is how to fix it. I disagree on this way of addressing it.

Based on my comments above, here is my suggestion:

When an undefined variable is requested, give an error message, e.g.:

Quoted 7 lines
$ bitbake-getvar UNDEFINED_VAR
The variable 'UNDEFINED_VAR' is not defined! [on stderr]

and set the exit code to 1. And the same result with --value:

$ bitbake-getvar --value UNDEFINED_VAR
The variable 'UNDEFINED_VAR' is not defined! [on stderr]
Quoted 7 lines

That seems reasonable to me, without the "!" though.

Quoted 2 lines

If -s or --no-messages (borrowed from grep) is also specified, then no error message is output and the exit code is 0.

I'd suggest only a long option --ignore-undefined. That way when you see it in a shell script, you know exactly what risk is being taken.

How about providing a --default parameter? The behavior of bitbake-getvar would then be:

* If the requested variable is defined, print its value and exit zero (in this case --default is irrelevant).

* If the requested variable is not defined and --default <value> is provided, print <value> and exit zero.

* If the requested variable is not defined and --default is not provided, exit non-zero.

Rationale:

* The proposed behavior/API allows for:

Quoted 8 lines

* Detecting undefined variables.

* Controlling when errors should be raised.

* Using custom fallback values for undefined variables.

All the best.
Mario

I liked this idea and I have implemented it (patch to follow shortly).

There is one caveat though. For the case when --value is not used and the requested variable is undefined, the code will actually have to inject the default value using d.setVar() to make sure the output is consistent.

And just in case RP does not like this solution, I have an implementation of the original proposal with --ignore-undefined as well. But let's try this one first.

//Peter