diff mbox series

[1/1] bitbake: event: Improve error message for eventhandler

Message ID 2fd4acbc082469c6c665dc5499ea471d9223b42c.1703133461.git.liezhi.yang@windriver.com
State New
Headers show
Series [1/1] bitbake: event: Improve error message for eventhandler | expand

Commit Message

Robert Yang Dec. 21, 2023, 4:40 a.m. UTC
From: Robert Yang <liezhi.yang@windriver.com>

* Before the erorr message was:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
  File "/path/to/poky/meta/classes-global/base.bbclass", line 4
    # SPDX-License-Identifier: MIT
           ^^^^^
SyntaxError: invalid syntax

This is hard to debug since the error line number 4 is incorrect, and nothing
is wrong with the code in line 4.

* Now the error message is:
ERROR: Unable to register event handler '/path/to/poky/meta/classes-global/base.bbclass':
  File "defaultbase_eventhandler", line 4
    an error line
       ^^^^^
SyntaxError: invalid syntax

This is much easier to debug.

The filename and code doesn't match since the file usually contains more code
than eventhandler, don't pass filename to compile() can fix the problem.

Signed-off-by: Robert Yang <liezhi.yang@windriver.com>
---
 bitbake/lib/bb/event.py | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

Comments

Richard Purdie Dec. 21, 2023, 9:56 a.m. UTC | #1
On Wed, 2023-12-20 at 20:40 -0800, Robert Yang via lists.openembedded.org wrote:
> From: Robert Yang <liezhi.yang@windriver.com>
> 
> * Before the erorr message was:
> ERROR: Unable to register event handler 'defaultbase_eventhandler':
>   File "/path/to/poky/meta/classes-global/base.bbclass", line 4
>     # SPDX-License-Identifier: MIT
>            ^^^^^
> SyntaxError: invalid syntax
> 
> This is hard to debug since the error line number 4 is incorrect, and nothing
> is wrong with the code in line 4.
> 
> * Now the error message is:
> ERROR: Unable to register event handler '/path/to/poky/meta/classes-global/base.bbclass':
>   File "defaultbase_eventhandler", line 4
>     an error line
>        ^^^^^
> SyntaxError: invalid syntax
> 
> This is much easier to debug.
> 
> The filename and code doesn't match since the file usually contains more code
> than eventhandler, don't pass filename to compile() can fix the problem.
> 

Sorry, but I really don't like this change. It is now giving a filename
as an event handler and an event handler name as a file. To some, that
is going to be even more confusing.

What really needs to happen here is that the line number needs to be
fixed with the offset in the file. You'll see the code already does:

ast.increment_lineno(code, lineno-1)

so before printing the traceback, the code probably needs to account
for that offset in the in the exception handling too.

Cheers,

Richard
Robert Yang Dec. 26, 2023, 12:45 p.m. UTC | #2
Hi RP,

On 12/21/23 17:56, Richard Purdie wrote:
> On Wed, 2023-12-20 at 20:40 -0800, Robert Yang via lists.openembedded.org wrote:
>> From: Robert Yang <liezhi.yang@windriver.com>
>>
>> * Before the erorr message was:
>> ERROR: Unable to register event handler 'defaultbase_eventhandler':
>>    File "/path/to/poky/meta/classes-global/base.bbclass", line 4
>>      # SPDX-License-Identifier: MIT
>>             ^^^^^
>> SyntaxError: invalid syntax
>>
>> This is hard to debug since the error line number 4 is incorrect, and nothing
>> is wrong with the code in line 4.
>>
>> * Now the error message is:
>> ERROR: Unable to register event handler '/path/to/poky/meta/classes-global/base.bbclass':
>>    File "defaultbase_eventhandler", line 4
>>      an error line
>>         ^^^^^
>> SyntaxError: invalid syntax
>>
>> This is much easier to debug.
>>
>> The filename and code doesn't match since the file usually contains more code
>> than eventhandler, don't pass filename to compile() can fix the problem.
>>
> 
> Sorry, but I really don't like this change. It is now giving a filename
> as an event handler and an event handler name as a file. To some, that
> is going to be even more confusing.
> 
> What really needs to happen here is that the line number needs to be
> fixed with the offset in the file. You'll see the code already does:
> 
> ast.increment_lineno(code, lineno-1)
> 
> so before printing the traceback, the code probably needs to account
> for that offset in the in the exception handling too.

After more investigations, the compile() or ast.parse() can't accept a lineno
argument as offset, and the ast.increment_lineno(code, lineno-1) can't work
since 'code' is None when there is an exception. And we can't fix 
traceback.tb_lineno since the tb_lineno is for event.py, not for 'code'
in compile(), it seems that we have to fix the error message in traceback.

And python 3.10.9 is worse than 3.8.10:

* In python 3.8.10, the error message is:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
   File "/path/to/poky/meta/classes-global/base.bbclass", line 4
     an error line
        ^
SyntaxError: invalid syntax

We can see the error code line (an error line)

* But in python3 3.10.9, we can't see the error code:
ERROR: Unable to register event handler 'defaultbase_eventhandler':
   File "/buildarea1/lyang1/poky/meta/classes-global/base.bbclass", line 4
     # SPDX-License-Identifier: MIT
            ^^^^^
SyntaxError: invalid syntax


I think that we can fix it in the following ways:
1) Don't pass file name to compile() to make it can report error code correctly 
in both python 3.8.10 and 3.10.9.

2) Fix the lineno offset and filename in traceback message.

Then the lineno and filename will be correct for both python versions:

ERROR: Unable to register event handler 'defaultbase_eventhandler':
File "/path/to/poky/meta/classes-global/base.bbclass", line 256
     an error line
        ^^^^^
SyntaxError: invalid syntax

I will send a V2 for it.

// Robert

> 
> Cheers,
> 
> Richard
>
diff mbox series

Patch

diff --git a/bitbake/lib/bb/event.py b/bitbake/lib/bb/event.py
index f8acacd80d1..2fbf1188d45 100644
--- a/bitbake/lib/bb/event.py
+++ b/bitbake/lib/bb/event.py
@@ -260,15 +260,17 @@  def register(name, handler, mask=None, filename=None, lineno=None, data=None):
             try:
                 code = bb.methodpool.compile_cache(tmp)
                 if not code:
-                    if filename is None:
-                        filename = "%s(e, d)" % name
-                    code = compile(tmp, filename, "exec", ast.PyCF_ONLY_AST)
+                    code = compile(tmp, name, "exec", ast.PyCF_ONLY_AST)
                     if lineno is not None:
                         ast.increment_lineno(code, lineno-1)
-                    code = compile(code, filename, "exec")
+                    code = compile(code, name, "exec")
                     bb.methodpool.compile_cache_add(tmp, code)
             except SyntaxError:
-                logger.error("Unable to register event handler '%s':\n%s", name,
+                if filename:
+                    err_msg= filename
+                else:
+                    err_msg = name
+                logger.error("Unable to register event handler '%s':\n%s", err_msg,
                              ''.join(traceback.format_exc(limit=0)))
                 _handlers[name] = noop
                 return