compute_relative_path

Function compute_relative_path 

Source
pub fn compute_relative_path(base: &Path, target: &Path) -> String
Expand description

Computes a relative path from a base directory to a target path.

This function handles paths both inside and outside the base directory, using ../ notation when the target is outside. Both paths should be absolute and canonicalized for correct results.

This is critical for lockfile portability - we must store manifest-relative paths even when they go outside the project with ../.

§Arguments

  • base - The base directory (should be absolute and canonicalized)
  • target - The target path (should be absolute and canonicalized)

§Returns

A relative path from base to target, using ../ notation if needed.

§Examples

use std::path::Path;
use agpm_cli::utils::compute_relative_path;

let base = Path::new("/project");
let target = Path::new("/project/agents/helper.md");
let relative = compute_relative_path(base, target);
// Returns: "agents/helper.md"

let target_outside = Path::new("/shared/utils.md");
let relative = compute_relative_path(base, target_outside);
// Returns: "../shared/utils.md"