Skip to content

fix(path): ignore anything trailing the file name in set_extension - #68

Merged
chipsenkbeil merged 1 commit into
mainfrom
fix/set-extension-trailing-separators
Aug 28, 2026
Merged

chipsenkbeil merged 1 commit into
mainfrom
fix/set-extension-trailing-separators

Conversation

@chipsenkbeil

Copy link
Copy Markdown
Owner

Fixes #67.

The bug

_set_extension assumed the old extension sat at the very end of the raw buffer:

self.inner.truncate(self.inner.len() - old_ext_len);

That holds only when the file name is the last thing in the buffer. With a trailing
separator the truncation lands in the middle of the file name instead:

input .with_extension("z") before std::path / after
a.bc/ a.b.z a.z
a.bc///// a.bc///.z a.z
test.reallylongext/ + "a" test.r.a test.a

The fix

Truncate at the end of the file stem, which is what std::path::PathBuf::set_extension
does. The stem is a subslice of the buffer, so its end offset is the correct cut point,
and everything past it — the old extension plus any trailing separators or a trailing
. segment — is dropped.

Two behavior changes, both toward std::path parity:

  • Anything trailing the file name is discarded: a.bc/ + z → a.z, foo.txt/.// + z → foo.z.
  • An empty extension now removes a trailing .: foo. + "" → foo (previously left as foo.).

Both PathBuf and Utf8PathBuf are fixed, which covers Path::with_extension,
Utf8Path::with_extension, and the Typed* wrappers since they all delegate here.

Verification

Every expectation in the new tests was checked against std::path::Path::with_extension
on the same input first, and the reproduction from the issue now matches std exactly:

Utf8WindowsPath a.bc/ -> a.z
Utf8UnixPath a.bc/ -> a.z
Utf8UnixPath a.bc///// -> a.z
Utf8UnixPathBuf test.reallylongext/ -> test.a
std::path::Path "a.bc/" -> "a.z"
std::path::Path "test.reallylongext/" -> "test.a"

cargo test --all-features passes (202 unit tests, 369 doctests); cargo fmt --check
is clean; cargo clippy reports only pre-existing findings in untouched files
(common/non_utf8/iter.rs, unix/constants.rs) from a newer clippy than CI pins.

Note

src/common/utf8/pathbuf.rs had a #[cfg(all(test, feature = "serde"))] mod tests
holding one test; it is merged into a single #[cfg(test)] mod tests with the serde
test gated individually, so the new tests run without the serde feature.

https://claude.ai/code/session_01A5uCQTmXn119ATyfP27hfe

`set_extension` assumed the old extension sat at the very end of the raw
buffer, so a path with a trailing separator had bytes chopped off the wrong
place: `a.bc/` with extension `z` became `a.b.z`, and `a.bc/////` became
`a.bc///.z`.

Truncate at the end of the file stem instead, which is where `std::path`
truncates. Anything trailing the file name -- separators, a current
directory segment -- is discarded along with the old extension, so
`a.bc/` now becomes `a.z`. As a side effect, an empty extension now
removes a trailing `.` (`foo.` becomes `foo`), also matching `std::path`.

Fixes #67

Claude-Session: https://claude.ai/code/session_01A5uCQTmXn119ATyfP27hfe
@chipsenkbeil
chipsenkbeil force-pushed the fix/set-extension-trailing-separators branch from 057ed9e to 518f42a Compare August 28, 2026 04:56
@chipsenkbeil
chipsenkbeil merged commit a0d2886 into main Aug 28, 2026
11 of 17 checks passed
@chipsenkbeil
chipsenkbeil deleted the fix/set-extension-trailing-separators branch August 28, 2026 15:56
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.

with_extension misbehaves for paths with trailing separators

1 participant