Skip to content

fix(utils): make normalizePath idempotent by globally collapsing repeated slashes (#161) - #167

Open
deus-intus wants to merge 1 commit into
tscircuit:mainfrom
deus-intus:fix/normalize-path-idempotent
Open

deus-intus wants to merge 1 commit into
tscircuit:mainfrom
deus-intus:fix/normalize-path-idempotent

Conversation

@deus-intus

Copy link
Copy Markdown

Fixes #161

Problem

On main, normalizePath only collapsed the first repeated slash group because the regex lacked the global (g) flag:

  • normalizePath("a///b///c.txt") returned "a/b///c.txt" instead of "a/b/c.txt".
  • normalizePath("a//b//c//d") returned "a/b//c//d" instead of "a/b/c/d".
  • Applying normalizePath multiple times changed the result, causing non-idempotent behavior during database storage and lookups.

Solution

Added the global /g flag to the slash collapse regex in lib/utils/normalize-path.ts:

- let normalized = path.replace(/\\+/g, "/").replace(/\/\/+/, "/")
+ let normalized = path.replace(/\\+/g, "/").replace(/\/\/+/g, "/")

Verification

Added tests/normalize-path.test.ts verifying:

  1. Multiple repeated slash groups collapse cleanly.
  2. Idempotence across multiple passes over the same string.
  3. Mixed backslashes and Windows separators normalize properly.
  4. Empty string and root path edge cases.

Ran full test suite with Bun (bun test): all 33 tests across 10 files pass cleanly.

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.

normalizePath is not idempotent when a path has multiple repeated slash groups

2 participants