Skip to content

fix: exception during exception-handling when ast/CodeBlockNode is not set - #12766

Closed
zeroflag0 wants to merge 1 commit into
mesonbuild:masterfrom
zeroflag0:patch-1
Closed

zeroflag0 wants to merge 1 commit into
mesonbuild:masterfrom
zeroflag0:patch-1

Conversation

@zeroflag0

Copy link
Copy Markdown
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\src\x\.venv\lib\site-packages\mesonbuild\mesonmain.py", line 184, in run
    return options.run_func(options)
  File "c:\src\x\.venv\lib\site-packages\mesonbuild\msetup.py", line 342, in run
    app.generate()
  File "c:\src\x\.venv\lib\site-packages\mesonbuild\msetup.py", line 171, in generate
    return self._generate(env, capture, vslite_ctx)
  File "c:\src\x\.venv\lib\site-packages\mesonbuild\msetup.py", line 193, in _generate
    intr = interpreter.Interpreter(b, user_defined_options=user_defined_options)
  File "c:\src\x\.venv\lib\site-packages\mesonbuild\interpreter\interpreter.py", line 289, in __init__
    self.load_root_meson_file()
  File "c:\src\x\.venv\lib\site-packages\mesonbuild\interpreterbase\interpreterbase.py", line 113, in load_root_meson_file
    self.handle_meson_version_from_ast()
  File "c:\src\x\.venv\lib\site-packages\mesonbuild\interpreter\interpreter.py", line 525, in handle_meson_version_from_ast
    if not self.ast.lines:
AttributeError: 'NoneType' object has no attribute 'lines'

ERROR: Unhandled python exception

@zeroflag0
zeroflag0 requested a review from jpakkane as a code owner January 23, 2024 12:43
@eli-schwartz

Copy link
Copy Markdown
Member

For context, can you show an example of a meson.build that triggers this issue?

Also: please apply fixups to previous commits using git commit --amend and force pushing rather than creating multiple commits. You can also use git rebase -I origin/master and change the "pick" line for the fixup commit from "pick" to "f" to amend it after the fact.

@zeroflag0

Copy link
Copy Markdown
Author

For context, can you show an example of a meson.build that triggers this issue?

Also: please apply fixups to previous commits using git commit --amend and force pushing rather than creating multiple commits. You can also use git rebase -I origin/master and change the "pick" line for the fixup commit from "pick" to "f" to amend it after the fact.

I think I had a malformed meson.build file with "invisible characters" that tripped up the lexer - and then this follow-up-exception occured in vscode.

@bruchar1

Copy link
Copy Markdown
Member

My feeling is that the bad character should be detected sooner, at the Lexer level.

@eli-schwartz

Copy link
Copy Markdown
Member

Exactly. I'd still like to see an actual example meson.build file that reproduces the problem with sufficient fidelity that it can be checked into the repository as a test case -- not "a malformed file that tripped up the lexer". :)

(In fact we should have a test case regardless of whether we fix it in the AST interpreter or the lexer.)

@zeroflag0

zeroflag0 commented Feb 1, 2024 •

Copy link
Copy Markdown
Author

Exactly. I'd still like to see an actual example meson.build file that reproduces the problem with sufficient fidelity that it can be checked into the repository as a test case -- not "a malformed file that tripped up the lexer". :)

(In fact we should have a test case regardless of whether we fix it in the AST interpreter or the lexer.)

I just recently figured out why python (in general, not just meson) had so much trouble on my machine.
system:

  • windows (any 10+ should work, possibly 7+)
  • vscode

steps to reproduce:

  • add this setting to vscode: "files.encoding": "utf8bom" (which python calls encoding="utf-8-sig")
  • edit a meson.build-file with vscode and safe it.
  • run meson on that file

Python will try to open the file with utf-8 by default and fail to parse the BOM-"header"(?), tripping up the lexer and then the errors cascade from there.

expected behavior:

  1. proper error-message from the lexer: finding invisible characters is near impossible if the tool doesn't help you.
    meson does not forward the full exception IIRC which makes this even harder. line-number and index would be really helpful.
  2. no secondary error when self.ast is None (which I was trying to fix)
  3. ideally: auto-attempting utf-8-sig import? maybe?

@bruchar1

bruchar1 commented Feb 1, 2024

Copy link
Copy Markdown
Member

Opening build files using utf-8-sig encoding seems reasonable to me. This will still work with conventional utf-8 files, but will allow to silently drop the BOM when it is present.

@dcbaker

dcbaker commented Feb 1, 2024

Copy link
Copy Markdown
Member

As an aside, I'm reworking some of the state handling in the Interpreter, and I've changed the ast to use a dummy CodeBlockNode, which should fix some of these errors.

Also, Either using utf-8-sig or specifying that the format must be utf-8 no-bom and then adding some exception handling code for that seems appropriate. We could always do that with a wrapper function that sets the encoding properly and just use that instead of the raw open command

@eli-schwartz

Copy link
Copy Markdown
Member

Python will try to open the file with utf-8 by default and fail to parse the BOM-"header"(?), tripping up the lexer and then the errors cascade from there.

Can you add a new test case:

test cases/failing/130 buildfile encoding/meson.build

containing this issue? That way the testsuite will detect this case and make sure it does not regress.

You can see how it is done in

test cases/failing/96 invalid option file/

for inspiration, but we want to check meson.build instead of meson_options.txt this time.

bruchar1 added a commit to bruchar1/meson that referenced this pull request Feb 14, 2024
Some text editors on Windows may use utf8bom encoding by default.
Prevent crash by stripping the bom before trying to interpret the build
files.

Fixing this at the Lexer level allows to do it at one place. Another
solution would be to open the build files with `utf-8-sig` encoding, but
that would require to do it at each places build files are opened.

Fixes mesonbuild#12766.
bruchar1 added a commit to bruchar1/meson that referenced this pull request Feb 14, 2024
Some text editors on Windows may use utf8bom encoding by default.
Prevent crash and properly report misencoded files.

Fixes mesonbuild#12766.
eli-schwartz pushed a commit that referenced this pull request Feb 23, 2024
Some text editors on Windows may use utf8bom encoding by default.
Prevent crash and properly report misencoded files.

Fixes #12766.
soumyaDghosh pushed a commit to soumyaDghosh/meson that referenced this pull request Jun 4, 2024
Some text editors on Windows may use utf8bom encoding by default.
Prevent crash and properly report misencoded files.

Fixes mesonbuild#12766.
Volker-Weissmann pushed a commit to Volker-Weissmann/meson that referenced this pull request May 1, 2025
Some text editors on Windows may use utf8bom encoding by default.
Prevent crash and properly report misencoded files.

Fixes mesonbuild#12766.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants