Skip to content

fix: normalize path separators in CleanPath for cross-platform consistency#88

Open
spf13 wants to merge 1 commit intomainfrom
spf13/fix-windows-hardcoded-path-separators
Open

fix: normalize path separators in CleanPath for cross-platform consistency#88
spf13 wants to merge 1 commit intomainfrom
spf13/fix-windows-hardcoded-path-separators

Conversation

@spf13
Copy link
Copy Markdown
Owner

@spf13 spf13 commented Apr 23, 2026

CleanPath used filepath.Separator to split and join path components. On Windows that separator is \, so paths written with / (e.g. "foo/bar/baz") were never split -- the whole string was treated as a single filename component and the slashes were stripped as invalid characters. Fixes #87.

What changed

CleanPath now normalizes both \ and / to / before splitting, and always joins with /. This means:

  • "foo/bar/baz" -> "foo/bar/baz" on every platform (was broken on Windows)
  • "foo\bar\baz" -> "foo/bar/baz" on every platform (backslashes treated as separators, not invalid chars)
  • Output is always /-separated, matching the documented behavior in the README

The unused path/filepath import is removed.

One test case (several_dirs2) expected "foobarbaz" because it was written assuming backslashes would be stripped as invalid characters. That expectation is updated to "foo/bar/baz" to reflect the corrected, consistent behavior.

All 57 tests pass on Windows.

…tency

CleanPath was using filepath.Separator to split and join path components,
which is '\' on Windows. This caused two problems:
- Paths using '/' (e.g. 'foo/bar/baz') were not split on Windows
- Paths using '\' (e.g. 'foo\bar\baz') were incorrectly split on Windows
  but the backslashes were stripped as invalid chars on Unix

Fix: normalize both '\' and '/' to '/' before splitting, and always join
with '/'. This produces consistent output on all platforms regardless of
which separator style is used in the input.

Also update the 'several dirs2' test case expected value from 'foobarbaz'
to 'foo/bar/baz', since backslash-separated input now correctly splits into
path components rather than having the backslashes stripped as invalid chars.

Fixes #87

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes CleanPath so it treats both \ and / as path separators across platforms, preventing Windows from treating /-separated paths as a single filename segment and stripping slashes as invalid characters.

Changes:

  • Normalize \ to / before splitting path components in CleanPath.
  • Always join cleaned path components with / and remove the now-unused path/filepath import.
  • Update the several dirs2 test expectation to reflect the corrected separator behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pathological.go Normalizes separators before splitting and always rejoins with /, removing platform-dependent behavior.
pathological_test.go Updates a path test case to expect /-separated output for backslash-separated input.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

Test_paths fails on Windows: hardcoded forward-slash separators

2 participants