Skip to content

Conversation

@mohammedahmed18
Copy link
Contributor

@mohammedahmed18 mohammedahmed18 commented Oct 2, 2025

PR Type

Bug fix, Enhancement


Description

  • Use Git root as project root in worktrees

  • Add worktree-aware project root resolution

  • Update args roots via new logic


Diagram Walkthrough

flowchart LR
  A["CLI args parsing"] -- "module/tests roots" --> B["project_root_from_module_root"]
  B -- "in worktree" --> C["git_root_dir()"]
  B -- "not in worktree" --> D["derive from module_root/pyproject"]
  C -- "set project_root & test_project_root" --> E["args updated"]
  D -- "set project_root & test_project_root" --> E
Loading

File Walkthrough

Relevant files
Bug fix
cli.py
Worktree-aware project root resolution via git root           

codeflash/cli_cmds/cli.py

  • Import git_root_dir from git_utils.
  • Pass args.worktree to project root resolver.
  • Use Git root when in worktree for project roots.
  • Keep legacy logic when not in worktree.
+6/-3     

@github-actions
Copy link

github-actions bot commented Oct 2, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Possible Issue

When in a worktree, both project_root and test_project_root are forced to git_root_dir(). If tests_root is outside the git root or differs from module_root context, this may incorrectly collapse distinct roots. Validate that using the same git root for tests doesn't break path resolution or discovery.

args.project_root = project_root_from_module_root(args.module_root, pyproject_file_path, args.worktree)
args.tests_root = Path(args.tests_root).resolve()
if args.benchmarks_root:
    args.benchmarks_root = Path(args.benchmarks_root).resolve()
args.test_project_root = project_root_from_module_root(args.tests_root, pyproject_file_path, args.worktree)
if is_LSP_enabled():
Error Handling

project_root_from_module_root returns git_root_dir() without handling exceptions or non-git directories when in_worktree=True. Ensure git_root_dir() errors are surfaced meaningfully or fallback behavior is defined.

def project_root_from_module_root(module_root: Path, pyproject_file_path: Path, in_worktree: bool = False) -> Path:  # noqa: FBT001, FBT002
    if in_worktree:
        return git_root_dir()
    if pyproject_file_path.parent == module_root:
        return module_root
    return module_root.parent.resolve()
API Contract

The signature of project_root_from_module_root changed by adding in_worktree flag. Confirm all call sites updated accordingly and default matches prior behavior to avoid unintended root changes.

def project_root_from_module_root(module_root: Path, pyproject_file_path: Path, in_worktree: bool = False) -> Path:  # noqa: FBT001, FBT002
    if in_worktree:
        return git_root_dir()
    if pyproject_file_path.parent == module_root:
        return module_root
    return module_root.parent.resolve()

@github-actions
Copy link

github-actions bot commented Oct 2, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Fallback when git root unavailable

Guard against git_root_dir() returning None or raising when not in a Git repo.
Fallback to the previous behavior to avoid crashes and preserve functionality
outside Git worktrees.

codeflash/cli_cmds/cli.py [237-242]

 def project_root_from_module_root(module_root: Path, pyproject_file_path: Path, in_worktree: bool = False) -> Path:  # noqa: FBT001, FBT002
     if in_worktree:
-        return git_root_dir()
+        try:
+            git_root = git_root_dir()
+            if git_root:
+                return git_root
+        except Exception:
+            pass
     if pyproject_file_path.parent == module_root:
         return module_root
     return module_root.parent.resolve()
Suggestion importance[1-10]: 7

__

Why: Sensible defensive change: if git_root_dir() fails or returns falsy, it preserves prior non-worktree behavior, reducing crash risk when --worktree is set outside a Git repo. The existing and improved code match the new hunk, and the modification is accurate and low-risk.

Medium

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Oct 2, 2025

⚡️ Codeflash found optimizations for this PR

📄 509% (5.09x) speedup for project_root_from_module_root in codeflash/cli_cmds/cli.py

⏱️ Runtime : 23.9 milliseconds 3.93 milliseconds (best of 239 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch fix/use-git-root-as-project-root-in-worktree).

@mohammedahmed18 mohammedahmed18 merged commit bb35a60 into main Oct 3, 2025
21 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants