Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
<!-- Add all new changes here. They will be moved under a version at release -->
* `FIX` Opening a document whose URI embeds a long payload (e.g. an AI diff virtual document) no longer freezes the server for minutes: `files.normalize` no longer runs a quadratic `..`-folding pattern on long separator-free strings, and `workspace.isIgnored` skips non-`file:` URIs before any path handling.

## 3.19.2
`2026-10-13`
Expand Down
22 changes: 14 additions & 8 deletions script/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -961,14 +961,20 @@ function m.normalize(path)
path = m.resolvePathPlaceholders(path)
path = util.expandPath(path)
path = path:gsub('^%.[/\\]+', '')
for _ = 1, 1000 do
if path:sub(1, 2) == '..' then
break
end
local count
path, count = path:gsub('[^/\\]+[/\\]+%.%.[/\\]', '/', 1)
if count == 0 then
break
-- The pattern below backtracks quadratically on long strings that contain
-- no path separator (e.g. a virtual document URI carrying an encoded file),
-- and it can never match when there is no '..' at all, so fold only when
-- it is both possible and plausible for a real path.
if #path <= 4096 and path:find('..', 1, true) then
for _ = 1, 1000 do
if path:sub(1, 2) == '..' then
break
end
local count
path, count = path:gsub('[^/\\]+[/\\]+%.%.[/\\]', '/', 1)
if count == 0 then
break
end
end
end
if platform.os == 'windows' then
Expand Down
7 changes: 7 additions & 0 deletions script/workspace/workspace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ end
--- 文件是否被忽略
---@param uri uri
function m.isIgnored(uri)
-- Non-file schemes (e.g. virtual diff documents) are not filesystem paths.
-- Matching their URI against path-based ignore rules is meaningless, and
-- very expensive when the URI embeds a whole encoded file.
local scheme = uri:match('^(%a[%w+.-]*):')
if scheme and #scheme > 1 and scheme ~= 'file' then
return false
end
local scp = scope.getScope(uri)
local path = m.getRelativePath(uri)
local ignore = m.getNativeMatcher(scp)
Expand Down
1 change: 1 addition & 0 deletions test/basic/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'basic.textmerger'
require 'basic.filewatch'
require 'basic.gitignore'
require 'basic.uri-normalize'
18 changes: 18 additions & 0 deletions test/basic/uri-normalize.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local files = require 'files'
local workspace = require 'workspace'

-- A virtual document may embed its whole content in the URI (some AI diff
-- views do), which produces a very long string without path separators.
local uri = 'genie-diff:promptbasic.lua?' .. string.rep('a', 100000)

local clock = os.clock()
local normalized = files.normalize(uri)
assert(os.clock() - clock < 2.0, 'files.normalize is too slow on a long URI')
assert(normalized == uri, 'a separator-free URI must be returned unchanged')

clock = os.clock()
assert(workspace.isIgnored(uri) == false)
assert(os.clock() - clock < 2.0, 'workspace.isIgnored is too slow on a non-file URI')

-- Ordinary paths still fold '..' segments
assert(not files.normalize('a/../b'):find('..', 1, true))
Loading