Skip to content

Commit 99e124e

Browse files
committed
don't scan huge directories
* if `rootUri` or `workspaceFolder` is set to `ROOT` or `HOME`, this extension will refuse to load these directories and show an error message. * show warning message when scanning more than 100,000 files.
1 parent c586a11 commit 99e124e

File tree

7 files changed

+42
-1
lines changed

7 files changed

+42
-1
lines changed

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
+ `workspace.supportScheme`: `["file", "untitled", "git"]`
66
+ `diagnostics.disableScheme`: `["git"]`
77
* `NEW` folding: support folding `---@alias`
8+
* `CHG` if `rootUri` or `workspaceFolder` is set to `ROOT` or `HOME`, this extension will refuse to load these directories and show an error message.
9+
* `CHG` show warning message when scanning more than 100,000 files.
810
* `FIX` hover: can not union `table` with other basic types
911
* `FIX` [#1125](https://github.com/sumneko/lua-language-server/issues/1125)
1012
* `FIX` [#1131](https://github.com/sumneko/lua-language-server/issues/1131)

locale/en-us/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ WORKSPACE_DIAGNOSTIC =
142142
'Diagnosing workspace'
143143
WORKSPACE_SKIP_HUGE_FILE =
144144
'For performance reasons, the parsing of this file has been stopped: {}'
145+
WORKSPACE_NOT_ALLOWED =
146+
'Your workspace is set to `{}`. Lua language server refused to load this directory. Please check your configuration.[learn more here](https://github.com/sumneko/lua-language-server/wiki/Why-scanning-home-folder)'
147+
WORKSPACE_SCAN_TOO_MUCH =
148+
'More than {} files have been scanned. The current scanned directory is `{}`. Please confirm whether the configuration is correct.'
145149

146150
PARSER_CRASH =
147151
'Parser crashed! Last words:{}'

locale/pt-br/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ WORKSPACE_DIAGNOSTIC =
142142
'Diagnóstico de espaço de trabalho.'
143143
WORKSPACE_SKIP_HUGE_FILE =
144144
'Por motivos de desempenho, a análise deste arquivo foi interrompida: {}'
145+
WORKSPACE_NOT_ALLOWED = -- TODO: need translate!
146+
'Your workspace is set to `{}`. Lua language server refused to load this directory. Please check your configuration.[learn more here](https://github.com/sumneko/lua-language-server/wiki/Why-scanning-home-folder)'
147+
WORKSPACE_SCAN_TOO_MUCH = -- TODO: need translate!
148+
'More than {} files have been scanned. The current scanned directory is `{}`. Please confirm whether the configuration is correct.'
145149

146150
PARSER_CRASH =
147151
'Parser quebrou! Últimas palavras: {}'

locale/zh-cn/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ WORKSPACE_DIAGNOSTIC =
142142
'正在对工作目录进行诊断'
143143
WORKSPACE_SKIP_HUGE_FILE =
144144
'出于性能考虑,已停止对此文件解析:{}'
145+
WORKSPACE_NOT_ALLOWED =
146+
'你的工作目录被设置为了 `{}`,Lua语言服务拒绝加载此目录,请检查你的配置。[了解更多](https://github.com/sumneko/lua-language-server/wiki/Why-scanning-home-folder)'
147+
WORKSPACE_SCAN_TOO_MUCH =
148+
'已扫描了超过 {} 个文件,当前扫描的目录为 `{}`,请确认配置是否正确。'
145149

146150
PARSER_CRASH =
147151
'语法解析崩溃了!遗言:{}'

locale/zh-tw/script.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ WORKSPACE_DIAGNOSTIC =
142142
'正在對工作目錄進行診斷'
143143
WORKSPACE_SKIP_HUGE_FILE =
144144
'出於效能考慮,已停止對此檔案解析:{}'
145+
WORKSPACE_NOT_ALLOWED = -- TODO: need translate!
146+
'Your workspace is set to `{}`. Lua language server refused to load this directory. Please check your configuration.[learn more here](https://github.com/sumneko/lua-language-server/wiki/Why-scanning-home-folder)'
147+
WORKSPACE_SCAN_TOO_MUCH = -- TODO: need translate!
148+
'More than {} files have been scanned. The current scanned directory is `{}`. Please confirm whether the configuration is correct.'
145149

146150
PARSER_CRASH =
147151
'語法解析崩潰了!遺言:{}'

script/glob/gitignore.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,9 @@ function mt:getRelativePath(path)
164164
end
165165

166166
---@param callback async fun(path: string)
167+
---@param hook? async fun(ev: string, ...)
167168
---@async
168-
function mt:scan(path, callback)
169+
function mt:scan(path, callback, hook)
169170
local files = {}
170171
if type(callback) ~= 'function' then
171172
callback = nil
@@ -203,6 +204,9 @@ function mt:scan(path, callback)
203204
break
204205
end
205206
list[#list] = nil
207+
if hook then
208+
hook('scan', current)
209+
end
206210
if not self:simpleMatch(current) then
207211
check(current)
208212
end

script/workspace/workspace.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ local fw = require 'filewatch'
1313
local scope = require 'workspace.scope'
1414
local loading = require 'workspace.loading'
1515
local inspect = require 'inspect'
16+
local lang = require 'language'
1617

1718
---@class workspace
1819
local m = {}
@@ -46,6 +47,11 @@ end
4647
--- 初始化工作区
4748
function m.create(uri)
4849
log.info('Workspace create: ', uri)
50+
if uri == furi.encode '/'
51+
or uri == furi.encode(os.getenv 'HOME') then
52+
client.showMessage('Error', lang.script('WORKSPACE_NOT_ALLOWED', furi.decode(uri)))
53+
return
54+
end
4955
local path = m.normalize(furi.decode(uri))
5056
fw.watch(path)
5157
local scp = scope.createFolder(uri)
@@ -283,22 +289,34 @@ function m.awaitPreload(scp)
283289

284290
if scp.uri then
285291
log.info('Scan files at:', scp:getName())
292+
local count = 0
286293
---@async
287294
native:scan(furi.decode(scp.uri), function (path)
288295
local uri = files.getRealUri(furi.encode(path))
289296
scp:get('cachedUris')[uri] = true
290297
ld:loadFile(uri)
298+
end, function () ---@async
299+
count = count + 1
300+
if count == 100000 then
301+
client.showMessage('Warning', lang.script('WORKSPACE_SCAN_TOO_MUCH', count, furi.decode(scp.uri)))
302+
end
291303
end)
292304
end
293305

294306
for _, libMatcher in ipairs(librarys) do
295307
log.info('Scan library at:', libMatcher.uri)
308+
local count = 0
296309
scp:addLink(libMatcher.uri)
297310
---@async
298311
libMatcher.matcher:scan(furi.decode(libMatcher.uri), function (path)
299312
local uri = files.getRealUri(furi.encode(path))
300313
scp:get('cachedUris')[uri] = true
301314
ld:loadFile(uri, libMatcher.uri)
315+
end, function () ---@async
316+
count = count + 1
317+
if count == 100000 then
318+
client.showMessage('Warning', lang.script('WORKSPACE_SCAN_TOO_MUCH', count, furi.decode(libMatcher.uri)))
319+
end
302320
end)
303321
scp:gc(fw.watch(furi.decode(libMatcher.uri)))
304322
end
@@ -353,6 +371,7 @@ function m.normalize(path)
353371
if platform.OS == 'Windows' then
354372
path = path:gsub('[/\\]+', '\\')
355373
:gsub('[/\\]+$', '')
374+
:gsub('^(%a:)$', '%1\\')
356375
else
357376
path = path:gsub('[/\\]+', '/')
358377
:gsub('[/\\]+$', '')

0 commit comments

Comments
 (0)