From e6538bdec7f71035ee26c6429bc1977fb1814110 Mon Sep 17 00:00:00 2001 From: Mohamed El mouhib Date: Thu, 3 Dec 2026 17:11:42 +0000 Subject: [PATCH 1/2] fix: respect gitignore exceptions during workspace scans Fixes #3503. Regression tests cover directory negation and actual workspace loading. AI assistance: ChatGPT. --- changelog.md | 1 + script/glob/gitignore.lua | 54 +++++++++------- script/glob/matcher.lua | 76 +++++++++++++---------- script/workspace/workspace.lua | 2 +- test/basic/gitignore.lua | 72 +++++++++++++++++++++ test/tclient/init.lua | 1 + test/tclient/tests/gitignore-negation.lua | 37 +++++++++++ 7 files changed, 185 insertions(+), 58 deletions(-) create mode 100644 test/tclient/tests/gitignore-negation.lua diff --git a/changelog.md b/changelog.md index daefdb5ae..8984d8240 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ ## Unreleased +* `FIX` Respect `.gitignore` exceptions when scanning workspace files, including match-all patterns and directory exceptions. [#3503](https://github.com/LuaLS/lua-language-server/issues/3503) ## 3.19.2 `2026-10-13` diff --git a/script/glob/gitignore.lua b/script/glob/gitignore.lua index 7362e41d7..13d331107 100644 --- a/script/glob/gitignore.lua +++ b/script/glob/gitignore.lua @@ -71,7 +71,7 @@ function mt:addPattern(pat) return end for _, state in ipairs(states) do - self.matcher[#self.matcher+1] = matcher(state) + self.matcher[#self.matcher+1] = matcher(state, { fullMatch = true }) end end @@ -100,28 +100,27 @@ function mt:hasInterface(name) return self.interface[name] ~= nil end -function mt:checkDirectory(catch, path, matcher0) +function mt:checkDirectory(path, matcher0) if not self:hasInterface 'type' then return true end if not matcher0:isNeedDirectory() then return true end - if #catch < #path then - -- if path is 'a/b/c' and catch is 'a/b' - -- then the catch must be a directory - return true - else - return self:callInterface('type', path) == 'directory' - end + return self:callInterface('type', path) == 'directory' end -function mt:simpleMatch(path) - path = self:getRelativePath(path) +function mt:matchPath(relative, path) + -- Patterns describe entries within the root, not the root itself. + if relative == '' then + return nil + end + if self.options.ignoreCase then + relative = relative:lower() + end for i = #self.matcher, 1, -1 do local matcher0 = self.matcher[i] - local catch = matcher0(path) - if catch and self:checkDirectory(catch, path, matcher0) then + if matcher0(relative) and self:checkDirectory(path, matcher0) then if matcher0:isNegative() then return false else @@ -132,6 +131,10 @@ function mt:simpleMatch(path) return nil end +function mt:simpleMatch(path) + return self:matchPath(self:getRelativePath(path), path) +end + function mt:finishMatch(path) local paths = {} for filename in path:gmatch '[^/\\]+' do @@ -139,11 +142,11 @@ function mt:finishMatch(path) end for i = 1, #paths do local newPath = table.concat(paths, '/', 1, i) - local passed = self:simpleMatch(newPath) + local root = self.options.root + local fullPath = root and root:gsub('[/\\]+$', '') .. '/' .. newPath or newPath + local passed = self:matchPath(newPath, fullPath) if passed == true then return true - elseif passed == false then - return false end end return false @@ -151,15 +154,18 @@ end function mt:getRelativePath(path) local root = self.options.root or '' + path = path:gsub('^[/\\]+', ''):gsub('[/\\]+', '/'):gsub('/$', '') + root = root:gsub('^[/\\]+', ''):gsub('[/\\]+', '/'):gsub('/$', '') + local comparedPath, comparedRoot = path, root if self.options.ignoreCase then - path = path:lower() - root = root:lower() + comparedPath = path:lower() + comparedRoot = root:lower() + end + if comparedPath == comparedRoot then + return '' end - path = path:gsub('^[/\\]+', ''):gsub('[/\\]+', '/') - root = root:gsub('^[/\\]+', ''):gsub('[/\\]+', '/') - if path:sub(1, #root) == root then - path = path:sub(#root + 1) - path = path:gsub('^[/\\]+', '') + if comparedPath:sub(1, #root + 1) == comparedRoot .. '/' then + return path:sub(#root + 2) end return path end @@ -193,7 +199,7 @@ function mt:scan(path, callback, hook) end end end - if not self:simpleMatch(path) then + if not self(path) then check(path) end while #list > 0 do diff --git a/script/glob/matcher.lua b/script/glob/matcher.lua index 5778d86f1..194d92b59 100644 --- a/script/glob/matcher.lua +++ b/script/glob/matcher.lua @@ -5,6 +5,8 @@ local Symbol = m.S',{}[]*?/\\' local Char = 1 - Symbol local Path = (1 - m.S[[\/*?"<>|]])^1 * Slash local NoWord = #(m.P(-1) + Symbol) +local EntryChar = 1 - m.S('/\\') +local EntryPath = EntryChar ^ 1 * Slash local function whatHappened() return m.Cmt(m.P(1)^1, function (...) print(...) @@ -15,85 +17,89 @@ local mt = {} mt.__index = mt mt.__name = 'matcher' -function mt:exp(state, index) +function mt:exp(state, index, ending) local exp = state[index] if not exp then return end if exp.type == 'word' then - return self:word(exp, state, index + 1) + return self:word(exp, state, index + 1, ending) elseif exp.type == 'char' then - return self:char(exp, state, index + 1) + return self:char(exp, state, index + 1, ending) elseif exp.type == '**' then - return self:anyPath(exp, state, index + 1) + return self:anyPath(exp, state, index + 1, ending) elseif exp.type == '*' then - return self:anyChar(exp, state, index + 1) + return self:anyChar(exp, state, index + 1, ending) elseif exp.type == '?' then - return self:oneChar(exp, state, index + 1) + return self:oneChar(exp, state, index + 1, ending) elseif exp.type == '[]' then - return self:range(exp, state, index + 1) + return self:range(exp, state, index + 1, ending) elseif exp.type == '/' then - return self:slash(exp, state, index + 1) + return self:slash(exp, state, index + 1, ending) end end -function mt:word(exp, state, index) - local current = self:exp(exp.value, 1) - local after = self:exp(state, index) +function mt:word(exp, state, index, ending) + local after = self:exp(state, index, ending) if after then + local current = self:exp(exp.value, 1, ending and #Slash) return current * Slash * after else - return current + return self:exp(exp.value, 1, ending) end end -function mt:char(exp, state, index) +function mt:char(exp, state, index, ending) local current = m.P(exp.value) - local after = self:exp(state, index) + local after = self:exp(state, index, ending) if after then return current * after * NoWord else - return current * NoWord + return current * (ending or NoWord) end end -function mt:anyPath(_, state, index) - local after = self:exp(state, index) +function mt:anyPath(_, state, index, ending) + local after = self:exp(state, index, ending) if after then return m.P { 'Main', Main = after - + Path * m.V'Main' + + (ending and EntryPath or Path) * m.V'Main' } + elseif ending then + return m.P(1) ^ 0 * ending else return Path^0 end end -function mt:anyChar(_, state, index) - local after = self:exp(state, index) +function mt:anyChar(_, state, index, ending) + local after = self:exp(state, index, ending) + local char = ending and EntryChar or Char if after then return m.P { 'Main', Main = after - + Char * m.V'Main' + + char * m.V'Main' } else - return Char^0 + return char ^ 0 * (ending or m.P(true)) end end -function mt:oneChar(_, state, index) - local after = self:exp(state, index) +function mt:oneChar(_, state, index, ending) + local after = self:exp(state, index, ending) + local char = ending and EntryChar or Char if after then - return Char * after + return char * after else - return Char + return char * (ending or m.P(true)) end end -function mt:range(exp, state, index) - local after = self:exp(state, index) +function mt:range(exp, state, index, ending) + local after = self:exp(state, index, ending) local ranges = {} local selects = {} for _, range in ipairs(exp.value) do @@ -107,12 +113,12 @@ function mt:range(exp, state, index) if after then return current * after else - return current + return current * (ending or m.P(true)) end end -function mt:slash(_, state, index) - local after = self:exp(state, index) +function mt:slash(_, state, index, ending) + local after = self:exp(state, index, ending) if after then return after else @@ -125,15 +131,19 @@ function mt:pattern(state) if not state[1] then return nil end + -- gitignore checks each entry independently before descending into it. + -- Keep the end assertion inside wildcard alternatives so they can try + -- a later occurrence of a pattern rather than committing to a prefix. + local ending = self.options and self.options.fullMatch and m.P(-1) or nil if state.root then - local after = self:exp(state, 1) + local after = self:exp(state, 1, ending) if after then return m.C(after) else return nil end else - return m.C(self:anyPath(nil, state, 1)) + return m.C(self:anyPath(nil, state, 1, ending)) end end diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index 9420ef908..3755438da 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -111,7 +111,7 @@ local globInteferFace = { data[path] = 'file' return nil end - data[path] = true + data[path] = 'directory' local paths = {} pcall(function () for fullpath, status in fs.pairs(fullPath) do diff --git a/test/basic/gitignore.lua b/test/basic/gitignore.lua index ffa59633a..5a385b171 100644 --- a/test/basic/gitignore.lua +++ b/test/basic/gitignore.lua @@ -28,3 +28,75 @@ assert(glob.glob({ ' ' })('a.lua') == false) assert(glob.glob({ ' ', 'foo' })('foo') == true) assert(glob.glob({ ' ', 'foo' })('bar') == false) assert(glob.glob({ '*' })('a.lua') == true) + +-- #3503: scan from the workspace, not from the process working directory. +-- The root itself is not a candidate for a gitignore pattern, and a directory +-- exception must not also exempt the files inside that directory. +local function checkWorkspace(root, ignoreCase) + local function normalize(path) + path = path:gsub('\\', '/') + return ignoreCase and path:lower() or path + end + local dirs = { + [normalize(root)] = { root .. '/b.lua', root .. '/b.txt', root .. '/sub', root .. '/blocked' }, + [normalize(root .. '/sub')] = { root .. '/sub/a.lua', root .. '/sub/a.txt', root .. '/sub/deep' }, + [normalize(root .. '/sub/deep')] = { root .. '/sub/deep/c.lua', root .. '/sub/deep/c.txt' }, + [normalize(root .. '/blocked')] = { root .. '/blocked/d.lua' }, + } + local function newMatcher(patterns) + return glob.gitignore(patterns, { + root = root, + ignoreCase = ignoreCase, + }, { + type = function (path) + return dirs[normalize(path)] and 'directory' or 'file' + end, + list = function (path) + return dirs[normalize(path)] + end, + }) + end + local matcher = newMatcher { '*', '!*/', '!*.lua', 'blocked/' } + local scanned = matcher:scan(root) + table.sort(scanned) + assert(table.concat(scanned, '\n') == table.concat({ + root .. '/b.lua', root .. '/sub/a.lua', root .. '/sub/deep/c.lua', + }, '\n'), 'scan must include Lua files only and respect excluded parents') + + assert(not matcher(root)) + assert(not matcher('sub/a.lua')) + assert(not matcher(root .. '/sub/deep/c.lua')) + assert(matcher('sub/a.txt')) + assert(matcher(root .. '/sub/deep/c.txt')) + assert(matcher('blocked/d.lua')) + if ignoreCase then + assert(not matcher('SUB\\DEEP\\C.LUA')) + assert(matcher('SUB\\DEEP\\C.TXT')) + end + + -- A later exception for a directory permits traversal; it does not + -- override a later rule that excludes a file in that directory. + local ordered = newMatcher { '*', '!*/', '!*.lua', 'a.lua' } + assert(ordered('sub/a.lua')) + assert(not ordered('sub/deep/c.lua')) + + -- Re-including a child cannot make an excluded parent traversable. + local excludedParent = newMatcher { '*', '!*.lua' } + assert(excludedParent('sub/a.lua')) + assert(not excludedParent('b.lua')) + assert(#excludedParent:scan(root .. '/sub') == 0) +end + +checkWorkspace('/workspace/project', false) +checkWorkspace('C:\\Work\\Project', true) + +assert(ignored({ 'foo*' }, 'foo[bar]')) +assert(ignored({ '*.lua' }, 'dir/test[1].lua')) +assert(ignored({ '/sub/**/drop.lua' }, 'sub/drop.lua')) +assert(ignored({ '/sub/**/drop.lua' }, 'sub/other/drop.lua')) +assert(not ignored({ '/sub/**/drop.lua' }, 'elsewhere/sub/drop.lua')) +assert(ignored({ '/sub/**' }, 'sub/keep.lua')) +assert(not ignored({ '/sub/**' }, 'sub')) +local prefixed = glob.gitignore({ '/file.lua' }, { root = '/workspace/project' }) +assert(prefixed('/workspace/project/file.lua')) +assert(not prefixed('/workspace/project-other/file.lua')) diff --git a/test/tclient/init.lua b/test/tclient/init.lua index f945d570e..ea7783780 100644 --- a/test/tclient/init.lua +++ b/test/tclient/init.lua @@ -11,6 +11,7 @@ require 'tclient.tests.jump-source' require 'tclient.tests.load-relative-library' require 'tclient.tests.hover-set-local' require 'tclient.tests.same-prefix' +require 'tclient.tests.gitignore-negation' require 'tclient.tests.recursive-runner' require 'tclient.tests.modify-luarc' diff --git a/test/tclient/tests/gitignore-negation.lua b/test/tclient/tests/gitignore-negation.lua new file mode 100644 index 000000000..60bf01b39 --- /dev/null +++ b/test/tclient/tests/gitignore-negation.lua @@ -0,0 +1,37 @@ +local lclient = require 'lclient' +local fs = require 'bee.filesystem' +local util = require 'utility' +local furi = require 'file-uri' +local ws = require 'workspace' +local files = require 'files' + +local rootPath = LOGPATH .. '/gitignore-negation' +local rootUri = furi.encode(rootPath) + +fs.create_directories(fs.path(rootPath .. '/sub/deep')) +fs.create_directories(fs.path(rootPath .. '/blocked')) +util.saveFile(rootPath .. '/.gitignore', '*\n!*/\n!*.lua\nblocked/\n') +util.saveFile(rootPath .. '/root.lua', 'return 1') +util.saveFile(rootPath .. '/sub/deep/keep.lua', 'return 2') +util.saveFile(rootPath .. '/sub/drop.txt', '') +util.saveFile(rootPath .. '/blocked/drop.lua', 'return 3') + +---@async +lclient():start(function (client) + client:registerFakers() + client:initialize { rootUri = rootUri } + ws.awaitReady(rootUri) + + -- Both the initial scan and subsequent ignored-file checks must agree, + -- even after directory types have been cached by the scan. + for _, path in ipairs { '/root.lua', '/sub/deep/keep.lua' } do + local uri = rootUri .. path + assert(files.exists(uri)) + assert(not ws.isIgnored(uri)) + end + for _, path in ipairs { '/sub/drop.txt', '/blocked/drop.lua' } do + local uri = rootUri .. path + assert(not files.exists(uri)) + assert(ws.isIgnored(uri)) + end +end) From df1b69517f8358cb395f7e80eef17e6a862ab194 Mon Sep 17 00:00:00 2001 From: Mohamed El mouhib Date: Thu, 3 Dec 2026 18:08:02 +0000 Subject: [PATCH 2/2] fix: preserve literal punctuation in gitignore patterns --- changelog.md | 2 +- script/glob/gitignore.lua | 12 ++++++++-- test/basic/gitignore.lua | 13 +++++++++++ test/tclient/tests/gitignore-negation.lua | 27 +++++++++++++++-------- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/changelog.md b/changelog.md index 8984d8240..cc41a1270 100644 --- a/changelog.md +++ b/changelog.md @@ -2,7 +2,7 @@ ## Unreleased -* `FIX` Respect `.gitignore` exceptions when scanning workspace files, including match-all patterns and directory exceptions. [#3503](https://github.com/LuaLS/lua-language-server/issues/3503) +* `FIX` Respect `.gitignore` exceptions when scanning workspace files, including match-all patterns and directory exceptions, and match literal commas and braces in standalone patterns. [#3503](https://github.com/LuaLS/lua-language-server/issues/3503) ## 3.19.2 `2026-10-13` diff --git a/script/glob/gitignore.lua b/script/glob/gitignore.lua index 13d331107..85c28ec30 100644 --- a/script/glob/gitignore.lua +++ b/script/glob/gitignore.lua @@ -16,7 +16,7 @@ local function expect(p, err) return p + m.T(err) end -local parser = m.P { +local grammar = { 'Main', ['Sp'] = m.S(' \t')^0, ['Slash'] = m.S('/')^1, @@ -43,6 +43,13 @@ local parser = m.P { ['RangeUnit'] = m.Ct(- m.P']' * m.C(m.P(1)) * (m.P'-' * - m.P']' * m.C(m.P(1)))^-1), } +local groupedParser = m.P(grammar) +-- A standalone pattern can contain literal commas and braces. Only the +-- explicit brace-group syntax uses them as delimiters between patterns. +grammar.Main = m.Ct(m.V'Pattern') + m.T'Main Failed' +grammar.SimpleChar = m.P(1) - m.S'[]*?/' +local parser = m.P(grammar) + ---@class gitignore ---@field pattern string[] ---@field options table @@ -62,7 +69,8 @@ function mt:addPattern(pat) if self.options.ignoreCase then pat = pat:lower() end - local states, err = parser:match(pat) + local selectedParser = pat:match('^[ \t]*{') and groupedParser or parser + local states, err = selectedParser:match(pat) if not states then self.errors[#self.errors+1] = { pattern = pat, diff --git a/test/basic/gitignore.lua b/test/basic/gitignore.lua index 5a385b171..3517b33c2 100644 --- a/test/basic/gitignore.lua +++ b/test/basic/gitignore.lua @@ -100,3 +100,16 @@ assert(not ignored({ '/sub/**' }, 'sub')) local prefixed = glob.gitignore({ '/file.lua' }, { root = '/workspace/project' }) assert(prefixed('/workspace/project/file.lua')) assert(not prefixed('/workspace/project-other/file.lua')) + +-- Commas and braces outside an explicit pattern group are filename characters. +assert(ignored({ 'foo{a,b}' }, 'foo{a,b}')) +assert(not ignored({ 'foo{a,b}' }, 'foo')) +assert(ignored({ 'foo,bar' }, 'foo,bar')) +assert(not ignored({ 'foo,bar' }, 'foo')) +assert(ignored({ '/sub/foo{a,b}' }, 'sub/foo{a,b}')) +assert(not ignored({ '/sub/foo{a,b}' }, 'elsewhere/sub/foo{a,b}')) +-- Keep the existing grouped-pattern syntax for workspace configuration. +assert(ignored({ '{foo,bar}' }, 'foo')) +assert(ignored({ '{foo,bar}' }, 'bar')) +assert(not ignored({ '{foo,bar}' }, 'foo,bar')) +assert(ignored({ '{foo\\,bar,baz}' }, 'foo,bar')) diff --git a/test/tclient/tests/gitignore-negation.lua b/test/tclient/tests/gitignore-negation.lua index 60bf01b39..bdb922c01 100644 --- a/test/tclient/tests/gitignore-negation.lua +++ b/test/tclient/tests/gitignore-negation.lua @@ -10,9 +10,13 @@ local rootUri = furi.encode(rootPath) fs.create_directories(fs.path(rootPath .. '/sub/deep')) fs.create_directories(fs.path(rootPath .. '/blocked')) -util.saveFile(rootPath .. '/.gitignore', '*\n!*/\n!*.lua\nblocked/\n') +util.saveFile(rootPath .. '/.gitignore', '*\n!*/\n!*.lua\nblocked/\n' + .. 'literal{a,b}.lua\nliteral,comma.lua\n') util.saveFile(rootPath .. '/root.lua', 'return 1') util.saveFile(rootPath .. '/sub/deep/keep.lua', 'return 2') +util.saveFile(rootPath .. '/sub/deep/literal.lua', 'return 2') +util.saveFile(rootPath .. '/sub/deep/literal{a,b}.lua', 'return 2') +util.saveFile(rootPath .. '/sub/deep/literal,comma.lua', 'return 2') util.saveFile(rootPath .. '/sub/drop.txt', '') util.saveFile(rootPath .. '/blocked/drop.lua', 'return 3') @@ -24,14 +28,19 @@ lclient():start(function (client) -- Both the initial scan and subsequent ignored-file checks must agree, -- even after directory types have been cached by the scan. - for _, path in ipairs { '/root.lua', '/sub/deep/keep.lua' } do - local uri = rootUri .. path - assert(files.exists(uri)) - assert(not ws.isIgnored(uri)) + for _, path in ipairs { '/root.lua', '/sub/deep/keep.lua', '/sub/deep/literal.lua' } do + local uri = furi.encode(rootPath .. path) + assert(files.exists(uri), path) + assert(not ws.isIgnored(uri), path) end - for _, path in ipairs { '/sub/drop.txt', '/blocked/drop.lua' } do - local uri = rootUri .. path - assert(not files.exists(uri)) - assert(ws.isIgnored(uri)) + for _, path in ipairs { + '/sub/drop.txt', + '/blocked/drop.lua', + '/sub/deep/literal{a,b}.lua', + '/sub/deep/literal,comma.lua', + } do + local uri = furi.encode(rootPath .. path) + assert(not files.exists(uri), path) + assert(ws.isIgnored(uri), path) end end)