juju / charm-tools

Tools for charm authors and maintainers
Other
42 stars 64 forks source link

snap: git clone may fail when using python from the build environment #632

Closed fnordahl closed 1 year ago

fnordahl commented 1 year ago
:: Collecting charms.openstack                                                                                                                    
::   Cloning https://opendev.org/openstack/charms.openstack.git to /root/snap/charmcraft/common/pip-wheel-er66yl_h/charms.openstack               
::   Running command git clone -q https://opendev.org/openstack/charms.openstack.git /root/snap/charmcraft/common/pip-wheel-er66yl_h/charms.openstack                                                                                                                                                
::   warning: You appear to have cloned an empty repository.                                                                                      
::     ERROR: Command errored out with exit status 1:                                                                                             
::      command: /root/snap/charmcraft/common/tmpnokn6imj/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/root/snap/charmcraft/common/pip-wheel-er66yl_h/charms.openstack/setup.py'"'"'; __file__='"'"'/root/snap/charmcraft/common/pip-wheel-er66yl_h/charms.openstack/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /root/snap/charmcraft/common/pip-wheel-er66yl_h/charms.openstack/pip-egg-info                              
::          cwd: /root/snap/charmcraft/common/pip-wheel-er66yl_h/charms.openstack/                                                                
::     Complete output (5 lines):                                                                                                                 
::     Traceback (most recent call last):                                                                                                         
::       File "<string>", line 1, in <module>                                                                                                     
::       File "/usr/lib/python3.8/tokenize.py", line 392, in open                                                                                 
::         buffer = _builtin_open(filename, 'rb')                                                                                                 
::     FileNotFoundError: [Errno 2] No such file or directory: '/root/snap/charmcraft/common/pip-wheel-er66yl_h/charms.openstack/setup.py'        
::     ----------------------------------------                                                                                                   
:: ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.                                

It turns out that this is because the snap sets up environment variables assuming git will always be called from the snap, but in fact we are using it from the build system.

There are two approaches to this problem 1) We move the snap git binary into a folder in the snap where there are no python binaries, so that we can put that folder early in the system PATH 2) We extend the _get_env helper method to also prune the offending environment variables: https://github.com/juju/charm-tools/blob/82bf8975293907997fe46986803ad684a13f82ff/charmtools/build/tactics.py#L1066-L1083

The offending environment variables are defined here: https://github.com/juju/charm-tools/blob/82bf8975293907997fe46986803ad684a13f82ff/helpers/snap-wrappers/charm#L3-L8