From c8ecbba98dfe13d24b23e393cf60e98d12fbcde9 Mon Sep 17 00:00:00 2001 From: xuxy <137970895@qq.com> Date: Mon, 16 Nov 2026 23:41:37 +0800 Subject: [PATCH] fix: avoid quadratic path normalization on long virtual URIs Opening a document whose URI embeds a long payload (e.g. an AI diff virtual document) made files.normalize scan the whole string from every start position, because the '..' folding pattern requires a path separator that such a URI does not contain. The default 'Lua.diagnostics.enableScheme' check did not help since it runs after ws.isIgnored(). Only fold when the path can contain '..' and is short enough to be a real path, and skip path handling entirely for non-file URIs in ws.isIgnored(). --- changelog.md | 1 + script/files.lua | 22 ++++++++++++++-------- script/workspace/workspace.lua | 7 +++++++ test/basic/init.lua | 1 + test/basic/uri-normalize.lua | 18 ++++++++++++++++++ 5 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 test/basic/uri-normalize.lua diff --git a/changelog.md b/changelog.md index daefdb5ae..62fd1dde7 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ ## Unreleased +* `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` diff --git a/script/files.lua b/script/files.lua index e2535c4c7..a6dd6accc 100644 --- a/script/files.lua +++ b/script/files.lua @@ -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 diff --git a/script/workspace/workspace.lua b/script/workspace/workspace.lua index 9420ef908..e9aa133c9 100644 --- a/script/workspace/workspace.lua +++ b/script/workspace/workspace.lua @@ -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) diff --git a/test/basic/init.lua b/test/basic/init.lua index 12ff244ee..1c02b143e 100644 --- a/test/basic/init.lua +++ b/test/basic/init.lua @@ -1,3 +1,4 @@ require 'basic.textmerger' require 'basic.filewatch' require 'basic.gitignore' +require 'basic.uri-normalize' diff --git a/test/basic/uri-normalize.lua b/test/basic/uri-normalize.lua new file mode 100644 index 000000000..d07ef1396 --- /dev/null +++ b/test/basic/uri-normalize.lua @@ -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))