Skip to content

Commit

Permalink
Normalize drive letters when resolving paths on Windows
Browse files Browse the repository at this point in the history
When it comes to resolving paths on Windows, even though the underlying
API expects drive letter prefixes to be uppercase, some sources (e.g.
environment variables like `=C:`) won't normalize components, instead
returning the value as-is. While this wouldn't be a problem normally as
NTFS is case-insensitive on Windows, this introduces duplicates in the
database when adding new entries via `zoxide add`:

```batchfile prompt
> zoxide query --list
D:\
d:\
D:\coding
d:\coding
D:\coding\.cloned
d:\coding\.cloned
```

This is a cherry-pick from ajeetdsouza#567; see also rust-lang/rust-analyzer#14683.

Signed-off-by: mataha <[email protected]>
  • Loading branch information
mataha committed Aug 16, 2023
1 parent 2856310 commit 7f7462d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Fixed

- Normalize drive letters when resolving paths on Windows.

## [0.9.2] - 2023-08-04

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ mod tests {
}

#[apply(opts)]
fn posix_shellcheck_(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) {
fn posix_shellcheck(cmd: Option<&str>, hook: InitHook, echo: bool, resolve_symlinks: bool) {
let opts = Opts { cmd, hook, echo, resolve_symlinks };
let source = Posix(&opts).render().unwrap();

Expand Down
61 changes: 46 additions & 15 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,44 +291,75 @@ pub fn resolve_path(path: impl AsRef<Path>) -> Result<PathBuf> {
}
}

fn get_drive_path(drive_letter: u8) -> PathBuf {
format!(r"{}:\", drive_letter as char).into()
fn get_drive_prefix_path(drive_letter: u8) -> PathBuf {
format!(r"{}:\", patch_drive_letter(drive_letter)).into()
}

fn get_drive_relative(drive_letter: u8) -> Result<PathBuf> {
fn get_drive_relative_path(drive_letter: u8) -> Result<PathBuf> {
let path = current_dir()?;
if Some(drive_letter) == get_drive_letter(&path) {
return Ok(path);
return Ok(patch_drive_prefix(path));
}

if let Some(path) = env::var_os(format!("={}:", drive_letter as char)) {
return Ok(path.into());
if let Some(path) = env::var_os(format!("={}:", patch_drive_letter(drive_letter))) {
return Ok(patch_drive_prefix(path.into()));
}

let path = get_drive_path(drive_letter);
let path = get_drive_prefix_path(drive_letter);
Ok(path)
}

fn patch_drive_letter(drive_letter: u8) -> char {
drive_letter.to_ascii_uppercase() as char
}

// https://github.com/rust-lang/rust-analyzer/pull/14689
fn patch_drive_prefix(path: PathBuf) -> PathBuf {
let mut components = path.components();

match components.next() {
Some(Component::Prefix(prefix)) => {
let prefix = match prefix.kind() {
Prefix::Disk(drive_letter) => {
format!(r"{}:", patch_drive_letter(drive_letter))
}
Prefix::VerbatimDisk(drive_letter) => {
format!(r"\\?\{}:", patch_drive_letter(drive_letter))
}
_ => return path,
};

let mut path = PathBuf::default();
path.push(prefix);
path.extend(components);
path
}
_ => path,
}
}

match components.peek() {
Some(Component::Prefix(prefix)) => match prefix.kind() {
Prefix::Disk(drive_letter) => {
let disk = components.next().unwrap();
components.next();
if components.peek() == Some(&Component::RootDir) {
let root = components.next().unwrap();
stack.push(disk);
stack.push(root);
components.next();
base_path = get_drive_prefix_path(drive_letter);
} else {
base_path = get_drive_relative(drive_letter)?;
stack.extend(base_path.components());
base_path = get_drive_relative_path(drive_letter)?;
}

stack.extend(base_path.components());
}
Prefix::VerbatimDisk(drive_letter) => {
components.next();
if components.peek() == Some(&Component::RootDir) {
components.next();
base_path = get_drive_prefix_path(drive_letter);
} else {
bail!("illegal path: {}", path.display());
}

base_path = get_drive_path(drive_letter);
stack.extend(base_path.components());
}
_ => bail!("invalid path: {}", path.display()),
Expand All @@ -340,7 +371,7 @@ pub fn resolve_path(path: impl AsRef<Path>) -> Result<PathBuf> {
let drive_letter = get_drive_letter(&current_dir).with_context(|| {
format!("could not get drive letter: {}", current_dir.display())
})?;
base_path = get_drive_path(drive_letter);
base_path = get_drive_prefix_path(drive_letter);
stack.extend(base_path.components());
}
_ => {
Expand Down

0 comments on commit 7f7462d

Please sign in to comment.