canonical / charmcraft

Collaborate, build and publish charmed operators for Kubernetes, Linux and Windows.
Apache License 2.0
65 stars 69 forks source link

v2.3 fails to parse a valid python file #1102

Open WRFitch opened 1 year ago

WRFitch commented 1 year ago

When building the opensearch charm on charmcraft 2.3, it now fails to parse a file named helper_charm.py.

Here's the interesting part of the stacktrace:

2023-05-10 15:09:31.249 :: 2023-05-10 14:09:30.900 Packing the charm.
2023-05-10 15:09:31.249 :: 2023-05-10 14:09:30.900 Reading '/root/project/metadata.yaml'
2023-05-10 15:09:31.249 :: 2023-05-10 14:09:30.903 Validating metadata format
2023-05-10 15:09:31.249 :: 2023-05-10 14:09:30.909 Building for 'bases[0]' as host matches 'build-on[0]'.
2023-05-10 15:09:31.249 :: 2023-05-10 14:09:30.909 Building for 'bases[0][0]'.
2023-05-10 15:09:31.249 :: 2023-05-10 14:09:30.909 Building charm in '/root'
2023-05-10 15:09:31.249 :: 2023-05-10 14:09:30.926 Failed to parse Python library 'lib/charms/opensearch/v0/helper_charm.py'
2023-05-10 15:09:40.008 Failed to build charm for bases index '0'.
2023-05-10 15:09:40.011 Traceback (most recent call last):
2023-05-10 15:09:40.011   File "/snap/charmcraft/1349/lib/charmcraft/commands/build.py", line 388, in pack_charm_in_instance
2023-05-10 15:09:40.011     instance.execute_run(cmd, check=True, cwd=instance_output_dir)
2023-05-10 15:09:40.011   File "/snap/charmcraft/1349/lib/craft_providers/lxd/lxd_instance.py", line 289, in execute_run
2023-05-10 15:09:40.011     return self.lxc.exec(
2023-05-10 15:09:40.011   File "/snap/charmcraft/1349/lib/craft_providers/lxd/lxc.py", line 371, in exec
2023-05-10 15:09:40.011     return runner(final_cmd, **kwargs)  # pylint: disable=subprocess-run-check
2023-05-10 15:09:40.011   File "/snap/charmcraft/1349/usr/lib/python3.8/subprocess.py", line 516, in run
2023-05-10 15:09:40.011     raise CalledProcessError(retcode, process.args,
2023-05-10 15:09:40.011 subprocess.CalledProcessError: Command '['lxc', '--project', 'charmcraft', 'exec', 'local:charmcraft-opensearch-35655756-0-0-amd64', '--cwd', '/root/project', '--', 'env', 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin', 'CHARMCRAFT_MANAGED_MODE=1', 'charmcraft', 'pack', '--bases-index', '0', '--verbosity=brief']' returned non-zero exit status 1.

To reproduce, just build the main branch of the opensearch charm with charmcraft v2.3.

tigarmo commented 1 year ago

An update here is that we did some investigating and the issue is with the match / case construct in the "problem" file. Charmcraft uses the ast module to parse the file, and because the charmcraft snap uses Python 3.8 it can only parse 3.8 code (afaik the module is not "forward compatible"). The workaround is to replace the match/case with if/elses, but we should look into bumping the charmcraft snap to core22/python3.10.