dennisdoomen / dennisdoomen/pathy
[API Proposal]: EnumerateParents to walk up the directory tree
- Dominant language
- C#
- Stars
- 27
- Forks
- 5
- Avg merge
- 21h 29m
- Merged PRs (30d)
- 10
Description
### Background and motivation
`FindParentWithFileMatching` solves one specific version of "walk up until you find something". The underlying traversal is useful on its own: finding the nearest directory that satisfies an arbitrary condition, collecting the chain from a file up to the repository root, or computing how deep a path sits. Exposing the walk makes `FindParentWithFileMatching` a special case rather than the only supported question.
### API Proposal
```C#
namespace Pathy
{
public static class ChainablePathExtensions
{
public static IEnumerable EnumerateParents(this ChainablePath path, bool includeSelf = false);
}
}
```
The sequence is lazy and runs from the immediate parent up to the root.
### API Usage
```C#
var solutionDirectory = projectFile
.EnumerateParents()
.FirstOrDefault(x => (x / ".git").Exists);
var depth = projectFile.EnumerateParents().Count();
foreach (var directory in projectFile.EnumerateParents(includeSelf: true))
{
var settings = directory / ".editorconfig";
if (settings.Exists)
{
Apply(settings);
}
}
```
### Alternative Designs
Add more specialised finders, such as `FindParentWithDirectoryNamed`. Each one covers a narrow case, while a single enumeration plus LINQ covers all of them.
### Risks
The walk needs a defined stopping point for relative paths, which have no root to walk to. Laziness is important so that the file system is only touched for the directories a caller actually inspects.
Contributor guide
Research direction
Start at the ChainablePathExtensions entry point and compare the proposed API with FindParentWithFileMatching. Define the stopping behavior for relative paths and verify the sequence is lazy, supports includeSelf, and walks from the immediate parent to the root. Done means the API behavior is specified and the proposal's usage examples work as described.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100