ignore: fix dangling backslash error for escaped trailing space with extra spaces - #3515
Open
HarnageaGabriel wants to merge 1 commit into
Open
Conversation
…extra spaces Git's gitignore trailing-whitespace trimming only strips unescaped trailing spaces, preserving one escaped space (backslash+space) even when followed by further plain trailing spaces. ripgrep's previous check only handled a line ending in exactly one "\ " and otherwise did a blind trim_right(), which stripped the escaped space too, leaving a dangling backslash and a parse error. Add trim_trailing_unescaped_spaces(), a byte-safe port of git's trim_trailing_spaces() (dir.c), and use it in GitignoreBuilder::add_line. Fixes BurntSushi#3477
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3477.
Problem
Git's gitignore trailing-whitespace handling only strips unescaped trailing spaces. An escaped trailing space (
\at the end of a pattern) is preserved even if followed by additional plain trailing spaces, e.g. a line consisting offoo+ backslash + three spaces still keeps one escaped space after trimming (matchesgit check-ignore's behavior offoo\-> matchesfoo).ripgrep's
GitignoreBuilder::add_lineonly special-cased a line ending in exactly one\(backslash immediately followed by a single space):When there are extra unescaped spaces after the escaped one, the line no longer ends with
\(it ends with plain spaces), so it falls through to an unconditionaltrim_right(), which strips the escaped space too and leaves a dangling backslash — causing a parse error instead of ripgrep silently ignoring the pattern like git does.Repro (from the issue)
Real git accepts this and ignores a file named
foo(with trailing space).Fix
Added
trim_trailing_unescaped_spaces, a small byte-safe port of git's owntrim_trailing_spaces()(dir.c): scan the line, treat\+ next byte as a consumed escaped pair (resetting any pending trailing-space run), track the start of a run of plain trailing spaces, and truncate at that point at the end of the scan (preserving a dangling backslash — with nothing to escape — as-is, matching git's early-return behavior for that case). This replaces the oldends_with("\ ")special case.Verified against the reporter's exact repro plus manual testing with a real
rgbuild:foois now ignored whilefoo(no trailing space) andfoo(extra unescaped trailing spaces) are not, matchinggit check-ignoreoutput.Testing
crates/ignore/src/gitignore.rs: the reporter's exact multi-space repro, a plain-trailing-space-only regression check, and the pre-existing single escaped-space case.cargo test -p ignore: 191 passed.cargo test --workspace: all passing except two pre-existing, unrelated Windows-only symlink-privilege test failures (misc::symlink_nofollow,regression::r1389_bad_symlinks_no_biscuit) caused by the local environment lacking the symlink-creation privilege — unrelated to this change.rgbinary confirms git-matching behavior.Note: a previous attempt at this fix (#3485) was opened and later closed by its own author; this is a fresh implementation using git's actual trimming algorithm as the reference rather than a heuristic patch.