mesonbuild / meson

The Meson Build System
http://mesonbuild.com
Apache License 2.0
5.65k stars 1.64k forks source link

Invoking methods on submodules inside project() causes unhandled Python exception #11393

Closed amcnulty-fermat closed 2 months ago

amcnulty-fermat commented 1 year ago

Describe the bug Calling import('python').find_installation('python3') in the body of project() causes an unhandled Python exception to be raised.

To Reproduce The following meson.build definition can reproduce the exception on version 1.0.0

project('unhandled-python-exception', 'c',
  version: run_command(
    import('python').find_installation('python3')
  )
)

Running meson setup build on this results in the following output:

The Meson build system
Version: 1.0.0
Source dir: /home/amcnulty/python-test
Build dir: /home/amcnulty/python-test/build
Build type: native build
Traceback (most recent call last):
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/mesonmain.py", line 193, in run
    return options.run_func(options)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/msetup.py", line 309, in run
    app.generate()
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/msetup.py", line 186, in generate
    self._generate(env)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/msetup.py", line 208, in _generate
    intr = interpreter.Interpreter(b, user_defined_options=user_defined_options)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreter/interpreter.py", line 328, in __init__
    self.parse_project()
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 119, in parse_project
    self.evaluate_codeblock(self.ast, end=1)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 174, in evaluate_codeblock
    raise e
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 167, in evaluate_codeblock
    self.evaluate_statement(cur)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 180, in evaluate_statement
    return self.function_call(cur)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 451, in function_call
    (h_posargs, h_kwargs) = self.reduce_arguments(node.args)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 541, in reduce_arguments
    reduced_val = self.evaluate_statement(val)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 180, in evaluate_statement
    return self.function_call(cur)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 451, in function_call
    (h_posargs, h_kwargs) = self.reduce_arguments(node.args)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 534, in reduce_arguments
    reduced_pos = [self.evaluate_statement(arg) for arg in args.arguments]
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 534, in <listcomp>
    reduced_pos = [self.evaluate_statement(arg) for arg in args.arguments]
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 184, in evaluate_statement
    return self.method_call(cur)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreterbase/interpreterbase.py", line 491, in method_call
    res = obj.method_call(method_name, args, kwargs)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/interpreter/interpreterobjects.py", line 740, in method_call
    state = ModuleState(self.interpreter)
  File "/home/amcnulty/.local/lib/python3.10/site-packages/mesonbuild/modules/__init__.py", line 57, in __init__
    self.project_version = interpreter.build.dep_manifest[interpreter.active_projectname].version
KeyError: ''

meson.build:1:0: ERROR: Unhandled python exception

    This is a Meson bug and should be reported!

It's the invocation of the find_installation() method that results in the exception being raised. If I remove this invocation, then execute meson setup build, meson produces the following output, which seems correct to me:

The Meson build system
Version: 1.0.0
Source dir: /home/amcnulty/python-test
Build dir: /home/amcnulty/python-test/build
Build type: native build

meson.build:1:0: ERROR: run_command argument 1 was of type "PythonModule" but should have been one of: "Executable", "ExternalProgram", "Compiler", "File", "str"

A full log can be found at /home/amcnulty/python-test/build/meson-logs/meson-log.txt

Expected behavior Either Meson would correctly invoke the method on the Python module, or it would print an error message stating that invoking methods on submodules is not possible in this context.

System Parameters

dnicolodi commented 1 year ago

Meson should not react like this, but you don't need import('python').find_installation('python3') to run a Python script. Using find_program('python3') is enough and should work fine in a project() call.

eli-schwartz commented 2 months ago

See also #11393 -- the linked PR should tag that issue as well.

eli-schwartz commented 2 months ago

There are various other issues that can cause an unhandled python exception, though they are crazier than importing a module and assuming it should work.

For example:

project('cargo lock', version: custom_target(output:'foo', command: ['echo']))

You might think this would be a type error since a custom_tgt isn't a valid input for this function. Instead you get AttributeError: 'NoneType' object has no attribute 'name' because it first tries to create the target itself, and fails.

eli-schwartz commented 2 months ago

"obviously" I meant #5134 rather than linking back to this very ticket. :p sorry