Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 12, 2025

Fix: Relative Repo Paths Resolve from Config File Directory

Issue Summary

When a .pre-commit-config.yaml uses a relative repo path (e.g., ../hook-repo), the path was being resolved against the process' current working directory (CWD) instead of the directory containing the config file. This broke configs in subprojects that used relative paths to hook repositories.

Solution

Implemented path normalization that resolves relative repository paths from the directory containing the .pre-commit-config.yaml file, as expected by users.

Changes Made

  1. Added Project::resolve_repo_url method (src/workspace.rs:221-243)

    • Encapsulates the logic for resolving repository clone URLs
    • Takes a repo URL string and returns the resolved path
    • Only normalizes relative paths that resolve to existing directories
    • Returns Cow<str> for efficient borrowing when no normalization is needed
  2. Updated Project::init_repos (src/workspace.rs:258-305)

    • Uses resolve_repo_url to get the resolved clone URL
    • Creates a resolved RemoteRepo only when normalization occurs
  3. Updated Workspace::init_repos (src/workspace.rs:770-821)

    • Uses each project's resolve_repo_url method
    • Properly handles deduplication with resolved URLs

Design Improvements (addressing feedback)

  • Encapsulation: Path normalization logic is now cleanly encapsulated in Project::resolve_repo_url
  • Clear API: The method exposes a simple interface - pass in a repo URL, get back the resolved URL
  • Used in init_repos: The normalization logic is no longer inline; it's called via the exposed method

Test Coverage

All Tests Pass:

  • workspace (7/7 tests) including relative_repo_path_resolution
  • try_repo (6/6 tests) including existing relative path test
  • Clippy passes with no warnings

Key Design Decisions

Why check if path exists?

  • Ensures we only normalize actual local repository paths
  • Avoids breaking edge cases like try-repo which uses synthetic configs
  • URLs and non-existent paths are left as-is for git to handle
Original prompt

This section details on the original issue you should resolve

<issue_title>Relative repo paths in .pre-commit-config.yaml resolve from CWD instead of config file directory</issue_title>
<issue_description>### Summary

Problem: when a .pre-commit-config.yaml uses a relative repo, the path resolves against the process’ working directory. For configs living in subprojects, this breaks because the lookup isn’t relative to the config file itself.

Expected: resolve relative repo paths from the directory that contains the .pre-commit-config.yaml.

Platform

Darwin 24.5.0 arm64

Version

prek 0.2.13 (9c70b50 2025-11-04)

.pre-commit-config.yaml

No response

Log file

No response</issue_description>

<agent_instructions>Before cloing repo, normalize the relative path based on the file path of the .pre-commit-config.yaml. The clone entry point is in workspace.rs: Workspace::init_repos function.
</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix relative repo paths in .pre-commit-config.yaml Fix relative repo paths to resolve from config file directory Nov 12, 2025
Copilot AI requested a review from j178 November 12, 2025 09:57
Copilot finished work on behalf of j178 November 12, 2025 09:57
@j178
Copy link
Owner

j178 commented Nov 12, 2025

@copilot Try to encapsulate the repo path normalize logic into the Project or RemoteHook, expose an function to get resolved clone url, then use that function in init_hooks, instead implement this logic in init_hooks directory.

Copy link
Contributor Author

Copilot AI commented Nov 12, 2025

@copilot Try to encapsulate the repo path normalize logic into the Project or RemoteHook, expose an function to get resolved clone url, then use that function in init_hooks, instead implement this l...

Done! I've refactored the code to encapsulate the normalization logic in Project::resolve_repo_url() which returns a resolved clone URL (Cow<str>). Both Project::init_repos and Workspace::init_repos now use this method instead of having the logic inline. (commit f80591a)

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • mirrors.aliyun.com
    • Triggering command: /home/REDACTED/work/prek/prek/target/debug/prek run (dns block)
    • Triggering command: /home/REDACTED/work/prek/prek/target/debug/prek try-repo /home/REDACTED/.local/share/prek/tests/.tmpMpFmuX/home/test-repos/try-repo-specific-hook another-hook (dns block)
    • Triggering command: /home/REDACTED/work/prek/prek/target/debug/prek try-repo ../home/test-repos/try-repo-relative (dns block)
  • mirrors.cloud.tencent.com
    • Triggering command: /home/REDACTED/work/prek/prek/target/debug/prek run (dns block)
    • Triggering command: /home/REDACTED/work/prek/prek/target/debug/prek run project2/ (dns block)
    • Triggering command: /home/REDACTED/work/prek/prek/target/debug/prek run --skip project2/ (dns block)
  • pypi.tuna.tsinghua.edu.cn
    • Triggering command: /home/REDACTED/work/prek/prek/target/debug/prek run (dns block)
    • Triggering command: /home/REDACTED/work/prek/prek/target/debug/prek run --skip project2/ (dns block)
    • Triggering command: /home/REDACTED/work/prek/prek/target/debug/prek try-repo ../home/test-repos/try-repo-relative (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of j178 November 12, 2025 10:07
@j178 j178 marked this pull request as ready for review November 12, 2025 10:13
@codecov
Copy link

codecov bot commented Nov 12, 2025

Codecov Report

❌ Patch coverage is 71.73913% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.78%. Comparing base (86f4d60) to head (f80591a).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/workspace.rs 71.73% 13 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1066      +/-   ##
==========================================
- Coverage   89.82%   89.78%   -0.04%     
==========================================
  Files          72       72              
  Lines       13294    13327      +33     
==========================================
+ Hits        11941    11966      +25     
- Misses       1353     1361       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@j178 j178 marked this pull request as draft December 6, 2025 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Relative repo paths in .pre-commit-config.yaml resolve from CWD instead of config file directory

2 participants