diff --git a/charmcraft/parts/plugins/_uv.py b/charmcraft/parts/plugins/_uv.py index b2bcfe318..e3fa3cd35 100644 --- a/charmcraft/parts/plugins/_uv.py +++ b/charmcraft/parts/plugins/_uv.py @@ -61,6 +61,9 @@ def get_build_commands(self) -> list[str]: return [ *super().get_build_commands(), *utils.get_venv_cleanup_commands( - self._get_venv_directory(), keep_bins=False + self._get_venv_directory(), + keep_bins=False, + # craft-parts already uses `uv venv --relocatable` + make_relocatable=False, ), ] diff --git a/charmcraft/utils/parts.py b/charmcraft/utils/parts.py index 9ed7d9ce7..d105cad88 100644 --- a/charmcraft/utils/parts.py +++ b/charmcraft/utils/parts.py @@ -56,11 +56,14 @@ def get_charm_copy_commands( return commands -def get_venv_cleanup_commands(venv_path: pathlib.Path, *, keep_bins: bool) -> list[str]: +def get_venv_cleanup_commands( + venv_path: pathlib.Path, *, keep_bins: bool, make_relocatable: bool = True +) -> list[str]: """Get a script do Charmcraft-specific venv cleanup. :param venv_path: The path to the venv. :param keep_bins: Whether to keep the bin directory of the venv. + :param make_relocatable: Whether to make the activate script portable :returns: A shell script to do this, as a string. """ venv_bin = venv_path / "bin" @@ -74,12 +77,15 @@ def get_venv_cleanup_commands(venv_path: pathlib.Path, *, keep_bins: bool) -> li f"rm -rf {venv_bin}/!(activate)", "shopt -u extglob", ] - update_activate = [ - # Replace hard-coded path in `activate` with portable path - # "\&" is escape for sed - 'sed -i \'s#^VIRTUAL_ENV=.*$#VIRTUAL_ENV="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." \\&> /dev/null \\&\\& pwd )"#\' ' - + str(venv_bin / "activate"), - ] + if make_relocatable: + update_activate = [ + # Replace hard-coded path in `activate` with portable path + # "\&" is escape for sed + 'sed -i \'s#^VIRTUAL_ENV=.*$#VIRTUAL_ENV="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )/.." \\&> /dev/null \\&\\& pwd )"#\' ' + + str(venv_bin / "activate"), + ] + else: + update_activate = [] delete_lib64 = textwrap.dedent(f""" if [ -L '{venv_lib64}' ]; then rm -f '{venv_lib64}'