diff --git a/changelog.md b/changelog.md index 9f42bd85f..f712d4813 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ ## Unreleased +* `FIX` A `.gitignore` line containing only whitespace no longer ignores the entire workspace [#3458](https://github.com/LuaLS/lua-language-server/issues/3458) * `FIX` `need-check-nil` diagnostic is no longer reported on safe navigation access (e.g. `x?.field`, `f?.()`, `t?.[key]`), since the optional access itself already handles the nil check. Note that a non-safe access chained after a safe one (e.g. `x.upper()?.field`) still reports, because the safe access only protects its own result. ## 3.19.1 diff --git a/script/glob/matcher.lua b/script/glob/matcher.lua index af43e0879..5778d86f1 100644 --- a/script/glob/matcher.lua +++ b/script/glob/matcher.lua @@ -122,6 +122,9 @@ function mt:slash(_, state, index) end function mt:pattern(state) + if not state[1] then + return nil + end if state.root then local after = self:exp(state, 1) if after then diff --git a/test/basic/gitignore.lua b/test/basic/gitignore.lua new file mode 100644 index 000000000..ffa59633a --- /dev/null +++ b/test/basic/gitignore.lua @@ -0,0 +1,30 @@ +local glob = require 'glob' + +local function ignored(patterns, path) + return glob.gitignore(patterns)(path) +end + +-- #3458: a whitespace-only gitignore pattern must not match every path +assert(ignored({ ' ' }, 'a.lua') == false) +assert(ignored({ ' ' }, 'foo/bar.lua') == false) +assert(ignored({ '\t' }, 'a.lua') == false) +assert(ignored({ '' }, 'a.lua') == false) + +-- Real patterns in the same list still match; the blank line does not take over +assert(ignored({ ' ', '*.log' }, 'a.lua') == false) +assert(ignored({ ' ', '*.log' }, 'a.log') == true) + +-- Intentional match-all and ordinary names are unchanged +assert(ignored({ '*' }, 'a.lua') == true) +assert(ignored({ 'foo' }, 'foo') == true) +assert(ignored({ 'foo' }, 'bar') == false) + +-- Quoted trailing space is a real pattern (gitignore spec), not a blank line +assert(ignored({ '\\ ' }, 'a.lua') == false) +assert(ignored({ '\\ ' }, ' ') == true) + +-- glob.glob shares the same matcher; empty patterns must not match everything +assert(glob.glob({ ' ' })('a.lua') == false) +assert(glob.glob({ ' ', 'foo' })('foo') == true) +assert(glob.glob({ ' ', 'foo' })('bar') == false) +assert(glob.glob({ '*' })('a.lua') == true) diff --git a/test/basic/init.lua b/test/basic/init.lua index 9030cf6dd..12ff244ee 100644 --- a/test/basic/init.lua +++ b/test/basic/init.lua @@ -1,2 +1,3 @@ require 'basic.textmerger' require 'basic.filewatch' +require 'basic.gitignore'