Skip to content

Commit 8270d26

Browse files
authored
Better error information for signal scripts
1 parent f63c5d6 commit 8270d26

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

Source/Orts.Simulation/Common/Scripting/ScriptManager.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ public object Load(string[] pathArray, string name, string nameSpace = "ORTS.Scr
8181

8282
private static Assembly CompileScript(string[] path)
8383
{
84+
var scriptPath = path.Length > 1 ? Path.GetDirectoryName(path[0]) : path[0];
85+
var scriptName = Path.GetFileName(scriptPath);
8486
try
8587
{
86-
var scriptName = Path.GetFileName(path[0]);
87-
var syntaxTrees = path.Select(file => CSharpSyntaxTree.ParseText(File.ReadAllText(file)));
88+
var syntaxTrees = path.Select(file => CSharpSyntaxTree.ParseText(File.ReadAllText(file), null, file));
8889
var compilation = CSharpCompilation.Create(
8990
scriptName,
9091
syntaxTrees,
@@ -99,23 +100,26 @@ private static Assembly CompileScript(string[] path)
99100
// in netcore:
100101
//var script = AssemblyLoadContext.Default.LoadFromStream(ms);
101102
if (script == null)
102-
Trace.TraceWarning($"Script file {path} could not be loaded into the process.");
103+
Trace.TraceWarning($"Script {scriptPath} could not be loaded into the process.");
103104
return script;
104105
}
105106
else
106107
{
107108
var errors = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error);
108109

109110
var errorString = new StringBuilder();
110-
errorString.AppendFormat("Skipped script {0} with error:", path);
111+
errorString.AppendFormat("Skipped script {0} with error:", scriptPath);
111112
errorString.Append(Environment.NewLine);
112113
foreach (var error in errors)
113114
{
114115
var textSpan = error.Location.SourceSpan;
116+
var fileName = Path.GetFileName(error.Location.SourceTree.FilePath);
115117
var lineSpan = error.Location.SourceTree.GetLineSpan(textSpan);
116118
var line = lineSpan.StartLinePosition.Line + 1;
117119
var column = lineSpan.StartLinePosition.Character;
118-
errorString.AppendFormat("\t{0}: {1}, line: {2}, column: {3}", error.Id, error.GetMessage(), line, column);
120+
errorString.AppendFormat("\t{0}: {1}, ", error.Id, error.GetMessage());
121+
if (path.Length > 1) errorString.AppendFormat("file: {0}, ", fileName);
122+
errorString.AppendFormat("line: {0}, column: {1}", line, column);
119123
errorString.Append(Environment.NewLine);
120124
}
121125

@@ -125,18 +129,15 @@ private static Assembly CompileScript(string[] path)
125129
}
126130
catch (InvalidDataException error)
127131
{
128-
if (path.Length > 1)
129-
Trace.TraceWarning("Skipped script folder {0} with error: {1}", Path.GetDirectoryName(path[0]), error.Message);
130-
else
131-
Trace.TraceWarning("Skipped script {0} with error: {1}", path[0], error.Message);
132+
Trace.TraceWarning("Skipped script {0} with error: {1}", scriptPath, error.Message);
132133
return null;
133134
}
134135
catch (Exception error)
135136
{
136-
if (File.Exists(path[0]))
137-
Trace.WriteLine(new FileLoadException(path[0], error));
137+
if (File.Exists(scriptPath) || Directory.Exists(scriptPath))
138+
Trace.WriteLine(new FileLoadException(scriptPath, error));
138139
else
139-
Trace.TraceWarning("Ignored missing script file {0}", path[0]);
140+
Trace.TraceWarning("Ignored missing script {0}", scriptPath);
140141
return null;
141142
}
142143
}

0 commit comments

Comments
 (0)