From b326d51ededb3c5298c283ce21ca613b84133ff4 Mon Sep 17 00:00:00 2001 From: Edward Li Date: Wed, 12 Mar 2025 16:45:37 -0700 Subject: [PATCH] Add file validity and existance check to `create_file` and `create_directory` --- src/codegen/sdk/core/codebase.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/codegen/sdk/core/codebase.py b/src/codegen/sdk/core/codebase.py index 9d6eaf2fd..87e131904 100644 --- a/src/codegen/sdk/core/codebase.py +++ b/src/codegen/sdk/core/codebase.py @@ -489,12 +489,9 @@ def create_file(self, filepath: str, content: str = "", sync: bool = True) -> TS ValueError: If the provided content cannot be parsed according to the file extension. """ # Check if file already exists - # TODO: These checks break parse tests ??? - # Look into this! - # if self.has_file(filepath): - # raise ValueError(f"File {filepath} already exists in codebase.") - # if os.path.exists(filepath): - # raise ValueError(f"File {filepath} already exists on disk.") + # NOTE: This check is also important to ensure the filepath is valid within the repo! + if self.has_file(filepath): + logger.warning(f"File {filepath} already exists in codebase. Overwriting...") file_exts = self.ctx.extensions # Create file as source file if it has a registered extension @@ -523,6 +520,11 @@ def create_directory(self, dir_path: str, exist_ok: bool = False, parents: bool Raises: FileExistsError: If the directory already exists and exist_ok is False. """ + # Check if directory already exists + # NOTE: This check is also important to ensure the filepath is valid within the repo! + if self.has_directory(dir_path): + logger.warning(f"Directory {dir_path} already exists in codebase. Overwriting...") + self.ctx.to_absolute(dir_path).mkdir(parents=parents, exist_ok=exist_ok) def has_file(self, filepath: str, ignore_case: bool = False) -> bool: