Skip to content

Commit cf3aff3

Browse files
author
Andrey Ovsiankin
committed
closes #1643 Загрузчик библиотек теперь разделяет ситуацию ненайденных библиотек и тех, которые не удается загрузить
1 parent 8c9206e commit cf3aff3

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

src/ScriptEngine.HostedScript/FileSystemDependencyResolver.cs

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ private enum ProcessingState
4242
Processed
4343
}
4444

45+
private enum LoadStatus
46+
{
47+
NotFound, // Каталог библиотеки не существует
48+
Empty, // Каталог найден, но библиотека не содержит исполняемых файлов
49+
Success // Библиотека успешно загружена
50+
}
51+
52+
private class LoadResult
53+
{
54+
public PackageInfo Package { get; set; }
55+
public LoadStatus Status { get; set; }
56+
57+
public static LoadResult NotFound() => new LoadResult { Status = LoadStatus.NotFound };
58+
public static LoadResult Empty() => new LoadResult { Status = LoadStatus.Empty };
59+
public static LoadResult Success(PackageInfo package) => new LoadResult
60+
{
61+
Status = LoadStatus.Success,
62+
Package = package
63+
};
64+
}
65+
4566
#endregion
4667

4768
public FileSystemDependencyResolver()
@@ -82,34 +103,46 @@ public PackageInfo Resolve(SourceCode module, string libraryName, IBslProcess pr
82103
{
83104
bool quoted = PrepareQuoted(ref libraryName);
84105

85-
var lib = quoted ?
106+
var result = quoted ?
86107
LoadByRelativePath(module, libraryName, process) :
87108
LoadByName(libraryName, process);
88109

89-
if (lib == null)
110+
if (result.Status == LoadStatus.NotFound)
111+
{
90112
throw new CompilerException($"Библиотека не найдена: '{libraryName}'");
113+
}
114+
else if (result.Status == LoadStatus.Empty)
115+
{
116+
throw new CompilerException(
117+
$"Библиотека '{libraryName}' найдена, но не содержит исполняемых файлов.\n" +
118+
"Для получения подробной информации установите переменную окружения OS_LIBRARY_LOADER_TRACE=1");
119+
}
91120

92-
return lib;
121+
return result.Package;
93122
}
94123

95-
private PackageInfo LoadByName(string libraryName, IBslProcess process)
124+
private LoadResult LoadByName(string libraryName, IBslProcess process)
96125
{
97126
foreach (var path in SearchDirectories)
98127
{
99128
if(!Directory.Exists(path))
100129
continue;
101130

102131
var libraryPath = Path.Combine(path, libraryName);
103-
var loadAttempt = LoadByPath(libraryPath, process);
104-
if (loadAttempt != null)
105-
return loadAttempt;
132+
var result = LoadByPath(libraryPath, process);
133+
134+
// Если библиотека найдена (успешно загружена или пуста), сразу возвращаем
135+
// Не ищем дальше, так как это более приоритетный путь
136+
if (result.Status != LoadStatus.NotFound)
137+
return result;
106138
}
107139

140+
// Если в SearchDirectories ничего не нашли, проверяем rootPath
108141
var rootPath = Path.Combine(LibraryRoot, libraryName);
109142
return LoadByPath(rootPath, process);
110143
}
111144

112-
private PackageInfo LoadByRelativePath(SourceCode module, string libraryPath, IBslProcess process)
145+
private LoadResult LoadByRelativePath(SourceCode module, string libraryPath, IBslProcess process)
113146
{
114147
string realPath;
115148

@@ -197,11 +230,16 @@ private bool PrepareQuoted(ref string value)
197230
return quoted;
198231
}
199232

200-
private PackageInfo LoadByPath(string libraryPath, IBslProcess process)
233+
private LoadResult LoadByPath(string libraryPath, IBslProcess process)
201234
{
202-
return Directory.Exists(libraryPath) ?
203-
LoadLibraryInternal(libraryPath, process) :
204-
null;
235+
if (!Directory.Exists(libraryPath))
236+
return LoadResult.NotFound();
237+
238+
var package = LoadLibraryInternal(libraryPath, process);
239+
240+
return package == null
241+
? LoadResult.Empty()
242+
: LoadResult.Success(package);
205243
}
206244

207245
private PackageInfo LoadLibraryInternal(string libraryPath, IBslProcess process)

src/ScriptEngine.HostedScript/LibraryLoader.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ private IExecutableModule CompileFile(string path, string ownerPackageId, IBslPr
279279
}
280280

281281
private static Lazy<bool> TraceEnabled =
282-
new Lazy<bool>(() => System.Environment.GetEnvironmentVariable("OS_LRE_TRACE") == "1");
282+
new Lazy<bool>(() =>
283+
System.Environment.GetEnvironmentVariable("OS_LIBRARY_LOADER_TRACE") == "1" ||
284+
System.Environment.GetEnvironmentVariable("OS_LRE_TRACE") == "1"); // для обратной совместимости
283285

284286
public static void TraceLoadLibrary(string message)
285287
{

0 commit comments

Comments
 (0)