diff --git a/mountaineer/client_builder/builder.py b/mountaineer/client_builder/builder.py index 00709790..30a9944c 100644 --- a/mountaineer/client_builder/builder.py +++ b/mountaineer/client_builder/builder.py @@ -3,7 +3,7 @@ from dataclasses import asdict, dataclass from json import dumps as json_dumps from pathlib import Path -from shutil import move as shutil_move, rmtree +from shutil import move as shutil_move, rmtree as shutil_rmtree from tempfile import TemporaryDirectory from time import monotonic_ns from typing import Any @@ -506,12 +506,17 @@ def move_build_artifacts_into_project(self): ssr_dir = self.view_root.get_managed_ssr_dir() for clear_dir in [static_dir, ssr_dir]: if clear_dir.exists(): - rmtree(clear_dir) - clear_dir.mkdir(parents=True) + shutil_rmtree(clear_dir) - # Final move - shutil_move(tmp_static_dir, self.view_root.get_managed_static_dir()) - shutil_move(tmp_ssr_dir, self.view_root.get_managed_ssr_dir()) + # Final move - shutil requires the destination directory to not exist, otherwise + # it will place the folder within the given folder. Since we just want a regular + # rename, we make sure to not create the destination directory + shutil_move( + tmp_static_dir, self.view_root.get_managed_static_dir(create_dir=False) + ) + shutil_move( + tmp_ssr_dir, self.view_root.get_managed_ssr_dir(create_dir=False) + ) def cache_is_outdated(self): """ diff --git a/mountaineer/paths.py b/mountaineer/paths.py index b9e06ab8..92398e50 100644 --- a/mountaineer/paths.py +++ b/mountaineer/paths.py @@ -87,34 +87,38 @@ def get_package_root_link(self): ) return self.package_root_link - def get_managed_code_dir(self): - return self.get_managed_dir_common("_server") + def get_managed_code_dir(self, create_dir: bool = True): + return self.get_managed_dir_common("_server", create_dir=create_dir) - def get_managed_static_dir(self, tmp_build: bool = False): + def get_managed_static_dir(self, tmp_build: bool = False, create_dir: bool = True): # Only root paths can have static directories if not self.is_root_link: raise ValueError( "Cannot get static directory from a non-root linked view path" ) - path = self.get_managed_dir_common("_static") + path = self.get_managed_dir_common("_static", create_dir=create_dir) if tmp_build: path = path / "tmp" path.mkdir(exist_ok=True) return path - def get_managed_ssr_dir(self, tmp_build: bool = False): + def get_managed_ssr_dir(self, tmp_build: bool = False, create_dir: bool = True): # Only root paths can have SSR directories if not self.is_root_link: raise ValueError( "Cannot get SSR directory from a non-root linked view path" ) - path = self.get_managed_dir_common("_ssr") + path = self.get_managed_dir_common("_ssr", create_dir=create_dir) if tmp_build: path = path / "tmp" path.mkdir(exist_ok=True) return path - def get_managed_dir_common(self, managed_dir: str): + def get_managed_dir_common( + self, + managed_dir: str, + create_dir: bool = True, + ): # If the path is to a file, we want to get the parent directory # so that we can create the managed code directory # We also create the managed code directory if it doesn't exist so all subsequent @@ -123,7 +127,8 @@ def get_managed_dir_common(self, managed_dir: str): if path.is_file(): path = path.parent managed_code_dir = path / managed_dir - managed_code_dir.mkdir(exist_ok=True) + if create_dir: + managed_code_dir.mkdir(exist_ok=True) return managed_code_dir def get_controller_view_path(self, controller: "ControllerBase"):