Background and motivation
ToAbsolute() resolves a relative path against the current directory or a given base. There is no way to simply clean up a path that already contains . or .. segments without changing whether it is relative or absolute, and without consulting the current working directory. This matters when you are producing a path for display, for a manifest, or for a system that will resolve it later in a different context.
API Proposal
namespace Pathy
{
public readonly struct ChainablePath
{
public ChainablePath Normalize();
}
}
The operation is purely textual. It does not touch the file system and does not resolve symbolic links.
API Usage
ChainablePath.From("src/../docs/./readme.md").Normalize();
// docs/readme.md, still relative
ChainablePath.From("c:/work/repo/src/../docs").Normalize();
// c:/work/repo/docs
ChainablePath.From("../../shared").Normalize();
// ../../shared, leading parent segments are kept because they cannot be resolved
Alternative Designs
Make ToString() normalise by default. That would be a behavioural break for anyone relying on the current output, and it removes the ability to keep a path exactly as the user typed it.
Risks
The behaviour of leading .. segments in a relative path has to be documented, as does the behaviour of .. at the root of an absolute path. Callers must understand that this is not the same as resolving through symbolic links, so the result can differ from what the operating system would resolve.
Background and motivation
ToAbsolute()resolves a relative path against the current directory or a given base. There is no way to simply clean up a path that already contains.or..segments without changing whether it is relative or absolute, and without consulting the current working directory. This matters when you are producing a path for display, for a manifest, or for a system that will resolve it later in a different context.API Proposal
The operation is purely textual. It does not touch the file system and does not resolve symbolic links.
API Usage
Alternative Designs
Make
ToString()normalise by default. That would be a behavioural break for anyone relying on the current output, and it removes the ability to keep a path exactly as the user typed it.Risks
The behaviour of leading
..segments in a relative path has to be documented, as does the behaviour of..at the root of an absolute path. Callers must understand that this is not the same as resolving through symbolic links, so the result can differ from what the operating system would resolve.