fix(scripting): #1056 refuse a {{equip}} token in a verbatim/raw/interpolated path literal — editor diagnostic + deploy-gate refusal
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Core.Scripting;
|
||||
|
||||
/// <summary>
|
||||
/// Finds <c>ctx.GetTag(…)</c> / <c>ctx.SetVirtualTag(…)</c> calls whose path argument carries the
|
||||
/// <c>{{equip}}</c> token in a literal form the substitution does NOT rewrite (lmxopcua#1056).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// <c>EquipmentScriptPaths.SubstituteEquipmentToken</c> rewrites the token only inside a REGULAR <c>"…"</c>
|
||||
/// path literal. A verbatim (<c>@"…"</c>), raw (<c>"""…"""</c>) or interpolated (<c>$"…"</c>) literal used to pass
|
||||
/// the editor and the deploy gate and then reach the runtime with the token still in the path — the tag read
|
||||
/// Bad for as long as it was deployed, with no error anywhere. Both the editor
|
||||
/// (<c>OTSCRIPT_EQUIPLITERAL</c>) and the deploy gate (<see cref="RoslynDeployScriptChecker"/>) now run this
|
||||
/// one scan and refuse such a script, so "the editor accepts ⇔ publish accepts" holds.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Same receiver guard as <see cref="DependencyExtractor"/> (a member access on the identifier <c>ctx</c>), so
|
||||
/// comments, other receivers and a token in a string that is not a path argument are never flagged.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static class EquipTokenLiteralScanner
|
||||
{
|
||||
private const string EquipToken = "{{equip}}";
|
||||
|
||||
/// <summary>One offending path literal.</summary>
|
||||
/// <param name="Span">The offending literal's span in the scanned source.</param>
|
||||
/// <param name="Message">The operator-facing explanation.</param>
|
||||
public sealed record Finding(TextSpan Span, string Message);
|
||||
|
||||
/// <summary>The offending path literals of <paramref name="source"/>, in source order.</summary>
|
||||
/// <param name="source">The script source (any <c>{{equip}}</c> token still unresolved is fine).</param>
|
||||
/// <returns>The findings; empty when none.</returns>
|
||||
public static IReadOnlyList<Finding> Find(string? source)
|
||||
{
|
||||
if (string.IsNullOrEmpty(source) || !source.Contains(EquipToken, StringComparison.Ordinal))
|
||||
return [];
|
||||
var tree = CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(kind: SourceCodeKind.Script));
|
||||
var findings = new List<Finding>();
|
||||
foreach (var inv in tree.GetRoot().DescendantNodes().OfType<InvocationExpressionSyntax>())
|
||||
{
|
||||
if (inv.Expression is not MemberAccessExpressionSyntax
|
||||
{
|
||||
Expression: IdentifierNameSyntax { Identifier.ValueText: "ctx" },
|
||||
Name.Identifier.ValueText: "GetTag" or "SetVirtualTag",
|
||||
} member)
|
||||
continue;
|
||||
if (inv.ArgumentList.Arguments.Count == 0) continue;
|
||||
var path = inv.ArgumentList.Arguments[0].Expression;
|
||||
var form = OffendingForm(path);
|
||||
if (form is null) continue;
|
||||
findings.Add(new Finding(
|
||||
path.Span,
|
||||
$"The {EquipToken} token is inside a {form} string literal in ctx.{member.Name.Identifier.ValueText}(...), " +
|
||||
"where it is NOT substituted — the path would reach the runtime unresolved and read Bad. " +
|
||||
$"Use a regular \"…\" literal: ctx.{member.Name.Identifier.ValueText}(\"{EquipToken}/<RefName>\")."));
|
||||
}
|
||||
return findings;
|
||||
}
|
||||
|
||||
private static string? OffendingForm(ExpressionSyntax path)
|
||||
{
|
||||
switch (path)
|
||||
{
|
||||
case LiteralExpressionSyntax lit when lit.IsKind(SyntaxKind.StringLiteralExpression):
|
||||
{
|
||||
var text = lit.Token.Text;
|
||||
var form = text.StartsWith("@\"", StringComparison.Ordinal) ? "verbatim"
|
||||
: text.StartsWith("\"\"\"", StringComparison.Ordinal) ? "raw"
|
||||
: null;
|
||||
return form is not null && ((string?)lit.Token.Value ?? "").StartsWith(EquipToken, StringComparison.Ordinal)
|
||||
? form : null;
|
||||
}
|
||||
case InterpolatedStringExpressionSyntax interpolated
|
||||
when interpolated.ToString().Contains(EquipToken, StringComparison.Ordinal):
|
||||
return "interpolated";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,6 +112,13 @@ public sealed class RoslynDeployScriptChecker : IDeployScriptChecker
|
||||
var source = request.Source ?? string.Empty;
|
||||
try
|
||||
{
|
||||
// lmxopcua#1056: a {{equip}} token in a verbatim/raw/interpolated path literal is never substituted, so the
|
||||
// script would compile and then read a tag path that cannot resolve. The editor runs the same scan.
|
||||
var unsubstituted = EquipTokenLiteralScanner.Find(source);
|
||||
if (unsubstituted.Count > 0)
|
||||
return new CachedVerdict(
|
||||
["equip token not substituted — " + string.Join("; ", unsubstituted.Select(f => f.Message))],
|
||||
Cacheable: true);
|
||||
if (request.Kind == ScriptContextKind.AlarmPredicate)
|
||||
{
|
||||
var extraction = DependencyExtractor.Extract(source);
|
||||
|
||||
@@ -162,6 +162,10 @@ public sealed class ScriptAnalysisService
|
||||
// (3) DependencyExtractor — spans are offsets in the normalized user source; map directly.
|
||||
foreach (var r in DependencyExtractor.Extract(code).Rejections)
|
||||
markers.Add(ToUserMarker(code, r.Span, r.Message, "OTSCRIPT_DYNPATH"));
|
||||
// (3b) lmxopcua#1056: a {{equip}} token in a verbatim/raw/interpolated path literal is never substituted —
|
||||
// the deploy gate refuses it (RoslynDeployScriptChecker runs this same scan), so the editor does too.
|
||||
foreach (var f in EquipTokenLiteralScanner.Find(code))
|
||||
markers.Add(ToUserMarker(code, f.Span, f.Message, "OTSCRIPT_EQUIPLITERAL"));
|
||||
// (4) ctx.SetVirtualTag(...) is inert at runtime — a WARNING (severity 4), never an error: publish
|
||||
// accepts the script, so the editor must too; the marker just puts the hover's caveat in the Problems list.
|
||||
foreach (var inv in SetVirtualTagCalls(tree.GetRoot(ct), preambleLength))
|
||||
|
||||
Reference in New Issue
Block a user