Skip to content

Add file validity and existence check to create_file #827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/codegen/sdk/core/codebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Loading