zen-fs / core

A filesystem, anywhere.
https://zenfs.dev/core/
MIT License
134 stars 20 forks source link

Backslashes in index generated on Windows #85

Closed 1j01 closed 4 months ago

1j01 commented 4 months ago

On Windows, the make-index command outputs JSON with entries having backslashes in the keys, e.g.:

{
  "version": 1,
  "entries": {
    "/.gitattributes": { ... }
    "/.github\\workflows\\playwright.yml": { ... },
    "/.github\\workflows": { ... },
    ...
  },
}

When the generated index with FetchFS, it lists every file in the folder structure as if it were a file at the root, whose name contains backslashes.

For example, running ZenFS.fs.readdirSync("/") I got:

Array(4203) [ ".gitattributes", ".github\\workflows\\playwright.yml", ".github\\workflows", ".github", ".gitignore", "android-chrome-192x192.png", "android-chrome-256x256.png", "apple-touch-icon.png", "audio\\CHIMES.WAV", "audio\\CHORD.WAV", … ]

Where I expected:

Array(37) [ "programs", ".gitattributes", ".github", ".gitignore", "android-chrome-192x192.png", "android-chrome-256x256.png", "apple-touch-icon.png", "audio", "browserconfig.xml", "classic.css", … ]

A simple fix is to use fixSlash already in make-index.js each place where entries.set is called.

However, it may be best to also make the logic that consumes the index more robust by handling both types of path separators... just in case someone creates their own filesystem index, I suppose.

james-pre commented 4 months ago

I believe it would best to normalize the paths before placing them into the index. This is easily accomplished by using relative and resolve from node:path/posix instead of node:path.