Skip to content

Reject sparse-directory index entries in the projection parser - #2191

Merged
Tyrie Vella (tyrielv) merged 2 commits into
masterfrom
tyrielv/sparse-index/projection-reject-sparse-dirs
Nov 16, 2026
Merged

Tyrie Vella (tyrielv) merged 2 commits into
masterfrom
tyrielv/sparse-index/projection-reject-sparse-dirs

Conversation

@tyrielv

@tyrielv Tyrie Vella (tyrielv) commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem and Context

Git can write a sparse index (index.sparse=true). In a sparse index, one entry stands for a whole subtree. This sparse-directory entry has mode 040000, a tree ID, the skip-worktree bit, and a path that ends in /. VFS for Git builds its projection from the index, and the projection parser does not know this entry type.

The parser does not reject the entry. A reviewer can think that it does, because GitIndexParser has a switch that rejects unknown file types. But the parser reads the mode field only when SupportsFileMode is true, and it is false on Windows. So the parser accepts a 040000 entry as a file:

  • The projection gets a folder with an empty child name, from the text after the trailing /.
  • The projection does not contain any file from the collapsed subtree.
  • The mount reports that the repository is ready. About 2.7 seconds later, ProjFS enumeration fails with ArgumentException: fileName cannot be empty, and the mount process stops. The error does not identify the cause.

Other index parses have the same problem. The placeholder scan treats every placeholder under a collapsed folder as absent from the index, so it adds each one to ModifiedPaths and removes it from the placeholder list. gvfs health counts the collapsed folder as one folder.

The persisted projection index (.gvfs\GVFS_projection) is a copy of the index from the last projection build. If git collapsed the index while the repository was mounted, this copy is also sparse. When the projection is valid, the mount parses this copy and not .git\index.

This change does not add sparse index support. It makes the parser identify the entry, stop before any consumer uses it, and tell the user how to expand the index. This change targets master, so that released versions reject a sparse index with a clear message. Sparse index support will be added to vnext, not master.

Changes

  • Add FileType.Directory, and make FileTypeAndMode recognise mode 040000. When the platform parses modes, the parser requires zero permission bits for a directory entry.
  • Add GitIndexEntry.PathEndsInSlash. The guard uses the trailing /, because Windows does not parse the mode field. The guard also rejects an entry with FileType.Directory.
  • GitIndexParser.ParseIndex calls the new FailIfSparseDirectoryEntry for each entry, after it reads the path and before the entry callback. So the guard covers every parse: the projection build from .git\index or from the persisted projection index, the placeholder scan, ValidateIndex, and CountIndexFolders.
  • FailIfSparseDirectoryEntry throws the new SparseIndexNotSupportedException. It derives from Exception, not IOException, because the index parsing thread retries an IOException, and a retry cannot succeed for a sparse index.
  • The message names the entry and gives recovery steps that work with released versions of Microsoft Git. The paths come from the mount, so the steps are also correct for a worktree. Example output from gvfs mount:
    Error: The git index contains the sparse-directory entry 'src/'. VFS for Git does not support a sparse index (git config index.sparse). To expand the index to the full format, do these steps:
    1. If the enlistment is mounted, run 'gvfs unmount'.
    2. In 'C:\Repos\Example\src', run 'git -c core.hookspath= config --worktree index.sparse false'. If git reports that --worktree cannot be used with multiple working trees, run 'git -c core.hookspath= config index.sparse false' instead.
    3. In 'C:\Repos\Example\src', run 'git -c core.virtualfilesystem= -c core.hookspath= -c index.sparse=false update-index --force-write-index'.
    4. Delete 'C:\Repos\Example\.gvfs\GVFS_projection'.
    5. Run 'gvfs mount'.
    
    • Step 2 turns off the sparse index in the repository config. Without it, the next index write collapses the index again. --worktree writes to the worktree config when extensions.worktreeConfig is on, and to the repository config when it is off. Git rejects --worktree when the extension is off and the repository has another worktree, so the step gives the plain git config command for that case. Git reads no worktree config while the extension is off.
    • Step 3 rewrites only the index. It keeps all entries, skip-worktree bits, and staged changes, and it does not write to the working directory. The options turn off the virtual file system hook and the GVFS hooks, because they need a running mount. git sparse-checkout disable does not work here: with hooks on, the pre-command hook stops it, and with hooks off, it fails because it writes to the unmounted working directory.
    • Step 4 makes the next mount build the projection from the expanded index.
  • The message escapes control and format characters in the entry path as \uXXXX. A crafted path cannot add lines, steps, or terminal escape sequences to the message.
  • InProcessMount reports SparseIndexNotSupportedException with its message only. Other exceptions from TryStart still include the stack trace. GitIndexProjection.BuildProjection already writes the full exception to the mount log.
  • When a projection build fails, BuildProjection marks the projection invalid. This is existing behaviour, so the next mount copies .git\index again.
  • GitIndexProjection.AddMissingModifiedFiles now catches SparseIndexNotSupportedException, marks the projection invalid, and returns FatalError. The background task runner then stops the mount, and the next mount parses .git\index and shows the message.
  • RepairJob.TryParseIndex reports SparseIndexNotSupportedException as CantFix with the message. Before this change, any parse failure was Fixable, and gvfs repair --confirm replaced the index with one built from HEAD. That discards staged changes, and a sparse index is valid.
  • If git collapses the index while the repository is mounted, the mount process stops the next time that it parses the index. It does not use a projection or a placeholder list that omits the collapsed folders.
  • A non-sparse index is parsed as before. The parser adds one check of the last path byte and the parsed type for each entry.
  • Make MockPlatformFileSystem.SupportsFileMode settable, so that unit tests can parse with and without the mode field.
  • Move the version 4 index writer from EnlistmentHydrationSummaryTests to a shared test helper, GitIndexV4Writer, and give it the entry mode and object ID. EnlistmentHydrationSummaryTests and the new tests use it.
  • Add unit tests in SparseIndexParsingTests:
    • The tests check that FileTypeAndMode recognises mode 040000.
    • The tests check that ValidateIndex rejects a sparse-directory entry with and without mode parsing, and that the message contains each recovery step.
    • A test checks that the message escapes a newline, an ANSI escape, and a right-to-left override in the entry path.
    • The tests check that the parser rejects a 040000 entry without a trailing /, and a 040000 entry with permission bits.
    • The tests check that CountIndexFolders rejects a sparse index.
    • The tests build a projection from a full index with and without mode parsing, and check the projected folders, files, SHAs, and the executable mode. An entry without skip-worktree stays out of the projection.
    • The tests check that a projection build rejects a sparse index, and that the message contains the working directory and the persisted projection path of the enlistment.
    • A test writes a full .git\index and a sparse persisted projection index, and calls GitIndexProjection.Initialize. The test checks that Initialize throws, that the projection is marked invalid, and that .git\index does not change.
    • The tests run the placeholder scan on a sparse index with a strict placeholder mock, and check that the parser throws before it removes a placeholder.
    • A test initializes a projection from a full index, writes a sparse index, and calls AddMissingModifiedFiles. The test checks that the result is FatalError and that the projection is marked invalid.
    • A test checks that GitIndexRepairJob.HasIssue returns CantFix for a sparse index, with the recovery steps.
  • Add a functional test, SparseIndexMountTests. The test unmounts, sets index.sparse=true in the repository config, collapses every top-level folder, and copies the sparse index to the persisted projection index. The test checks that gvfs mount fails with the message and without a stack trace. The test then runs the two commands and deletes the file that the message names, and mounts. The test checks that index.sparse is false, that git ls-files -s matches the output from before the collapse, and that a file in a collapsed folder is projected.

Known gaps and deliberate decisions

  • No test for the live reparse path — no unit test drives ParseIndexThreadMain with a sparse index. It calls the same BuildProjection that the Initialize test covers, and its outer catch stops the process. No follow-up planned.
  • Metadata write retry — SetProjectionInvalid writes the repository metadata through FileBasedCollection, which retries a failed write without a limit. A persistent write failure on the new rejection path can delay the mount exit. The same hazard applies to every metadata write today. No follow-up planned in this change.

@tyrielv
Tyrie Vella (tyrielv) force-pushed the tyrielv/sparse-index/projection-reject-sparse-dirs branch from 4c52905 to d1ea3a9 Compare November 14, 2026 00:23
Git can write a sparse index (index.sparse). A sparse index stores a
collapsed subtree as one entry with mode 040000 and a path that ends in
'/'. The projection cannot use such an entry.

The parser checks the mode field only when SupportsFileMode is true, and
it is false on Windows. So the parser accepted the entry as a file. The
mount then reported that the repository was ready, and ProjFS enumeration
failed about 2.7 seconds later with "fileName cannot be empty". The
projection also omitted every file in the collapsed subtree.

Recognise mode 040000 as FileType.Directory. GitIndexParser.ParseIndex
now rejects a sparse-directory entry before any entry callback receives
it. The check uses the trailing '/', so it works when the parser skips
the mode field. Every index parse is covered: the projection build (from
.git\index or from the persisted GVFS_projection copy), the placeholder
scan, index validation, and the folder count for 'gvfs health'.

The new SparseIndexNotSupportedException names the entry and gives
recovery steps that work with released versions of Git: turn off
index.sparse in the repository config, expand the index, delete the
persisted projection index, and mount. Control characters in the entry
path are escaped in the message.

The mount reports only this message. The mount log keeps the stack trace.
When the placeholder scan finds a sparse index, the projection is marked
invalid, so that the next mount reports the message. 'gvfs repair'
reports the message and does not replace the index with one built from
HEAD, because that would discard staged changes.

Assisted-by: Claude Opus 5.5
Signed-off-by: Tyrie Vella <[email protected]>
@KeithIsSleeping

Copy link
Copy Markdown

[KeithBot]: ⚠️ 1 issue · iteration 1 (5fd6d82)

  1. 🟡 Medium Recovery step 2 fails when another worktree exists and worktreeConfig is off

Approach: Right layer: one guard in GitIndexParser.ParseIndex covers every index reader (projection, GVFS_projection copy, placeholder scan, health, repair) before any consumer sees an entry.

Validation

  • ❌ Step 2, 2 worktrees, extension off
  • ✅ Unit tests 2048 source, 2031 base
  • ✅ CI run 43929679260: 34/34 jobs
  • ✅ Real sparse indexes, every parse path
  • ✅ Live reparse thread stops cleanly
  • ✅ Mutants of the new code all killed
  • ✅ Recovery steps, single/linked worktree
  • ✅ Builds: Mount, FT compile, CmdLine

🤖 KeithBot · claude-opus-5.5, gpt-6.1-sol, gemini-4-argon + approach review · reply on a thread to dispute

Recovery step 2 runs 'git config --worktree index.sparse false'. Git
rejects --worktree when the repository has more than one worktree
(including a prunable one) and extensions.worktreeConfig is off. A
primary enlistment turns that extension on only when a linked worktree
has a config.worktree file, so the step can fail with
"--worktree cannot be used with multiple working trees".

Tell the user to run 'git config index.sparse false' in that case. Git
reads no config.worktree file while the extension is off, so the
repository config is the right place. The step keeps --worktree first,
because with the extension on, a value in config.worktree would hide a
value in the repository config.

Assisted-by: Claude Opus 5.5
Signed-off-by: Tyrie Vella <[email protected]>
@KeithIsSleeping

Copy link
Copy Markdown

[KeithBot]: ✅ Resolved · iteration 2 (8413eb8)

  • Recovery step 2 fails when another worktree exists and worktreeConfig is off: fixed
  • New changes since iteration 1: no new issues

Validation

  • ⚠️ CI run 43977239916, all jobs — partial
  • ✅ Step 2 fallback, 7 layouts, 3 gits
  • ✅ Positive control: iteration 1 text
  • ✅ CI unit tests (run 43977239916)
  • ✅ CI mounted FT, x64 + arm64

Approving.

🤖 KeithBot · recheck by claude-opus-5.5 · reply on a thread to dispute

@tyrielv
Tyrie Vella (tyrielv) merged commit 60bffb4 into master Nov 16, 2026
39 checks passed
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.

2 participants