Skip to content

Commit

Permalink
preserve workflow file/line/column for better error messages (actions…
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsciple authored Mar 4, 2020
1 parent 94e7560 commit 0cba425
Show file tree
Hide file tree
Showing 18 changed files with 721 additions and 613 deletions.
574 changes: 13 additions & 561 deletions src/Runner.Worker/ActionManifestManager.cs

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions src/Runner.Worker/ActionRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ public async Task RunAsync()

// Load the inputs.
ExecutionContext.Debug("Loading inputs");
var templateTrace = ExecutionContext.ToTemplateTraceWriter();
var schema = new PipelineTemplateSchemaFactory().CreateSchema();
var templateEvaluator = new PipelineTemplateEvaluator(templateTrace, schema);
var templateEvaluator = ExecutionContext.ToPipelineTemplateEvaluator();
var inputs = templateEvaluator.EvaluateStepInputs(Action.Inputs, ExecutionContext.ExpressionValues);

foreach (KeyValuePair<string, string> input in inputs)
Expand Down Expand Up @@ -295,8 +293,7 @@ private string GenerateDisplayName(ActionStep action, DictionaryContextData cont
return displayName;
}
// Try evaluating fully
var schema = new PipelineTemplateSchemaFactory().CreateSchema();
var templateEvaluator = new PipelineTemplateEvaluator(context.ToTemplateTraceWriter(), schema);
var templateEvaluator = context.ToPipelineTemplateEvaluator();
try
{
didFullyEvaluate = templateEvaluator.TryEvaluateStepDisplayName(tokenToParse, contextData, out displayName);
Expand Down
14 changes: 14 additions & 0 deletions src/Runner.Worker/ExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using GitHub.Services.WebApi;
using GitHub.DistributedTask.Pipelines;
using GitHub.DistributedTask.Pipelines.ContextData;
using GitHub.DistributedTask.Pipelines.ObjectTemplating;
using GitHub.DistributedTask.WebApi;
using GitHub.Runner.Common.Util;
using GitHub.Runner.Common;
Expand Down Expand Up @@ -49,6 +50,7 @@ public interface IExecutionContext : IRunnerService
HashSet<string> OutputVariables { get; }
IDictionary<String, String> EnvironmentVariables { get; }
IDictionary<String, ContextScope> Scopes { get; }
IList<String> FileTable { get; }
StepsContext StepsContext { get; }
DictionaryContextData ExpressionValues { get; }
List<string> PrependPath { get; }
Expand Down Expand Up @@ -141,6 +143,7 @@ public sealed class ExecutionContext : RunnerService, IExecutionContext
public HashSet<string> OutputVariables => _outputvariables;
public IDictionary<String, String> EnvironmentVariables { get; private set; }
public IDictionary<String, ContextScope> Scopes { get; private set; }
public IList<String> FileTable { get; private set; }
public StepsContext StepsContext { get; private set; }
public DictionaryContextData ExpressionValues { get; } = new DictionaryContextData();
public bool WriteDebug { get; private set; }
Expand Down Expand Up @@ -266,6 +269,7 @@ public IExecutionContext CreateChild(Guid recordId, string displayName, string r
}
child.EnvironmentVariables = EnvironmentVariables;
child.Scopes = Scopes;
child.FileTable = FileTable;
child.StepsContext = StepsContext;
foreach (var pair in ExpressionValues)
{
Expand Down Expand Up @@ -569,6 +573,9 @@ public void InitializeJob(Pipelines.AgentJobRequestMessage message, Cancellation
}
}

// File table
FileTable = new List<String>(message.FileTable ?? new string[0]);

// Expression functions
if (Variables.GetBoolean("System.HashFilesV2") == true)
{
Expand Down Expand Up @@ -886,6 +893,13 @@ public static void Debug(this IExecutionContext context, string message)
}
}

public static PipelineTemplateEvaluator ToPipelineTemplateEvaluator(this IExecutionContext context)
{
var templateTrace = context.ToTemplateTraceWriter();
var schema = new PipelineTemplateSchemaFactory().CreateSchema();
return new PipelineTemplateEvaluator(templateTrace, schema, context.FileTable);
}

public static ObjectTemplating.ITraceWriter ToTemplateTraceWriter(this IExecutionContext context)
{
return new TemplateTraceWriter(context);
Expand Down
4 changes: 1 addition & 3 deletions src/Runner.Worker/JobExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ public async Task<List<IStep>> InitializeJob(IExecutionContext jobContext, Pipel

// Evaluate the job-level environment variables
context.Debug("Evaluating job-level environment variables");
var templateTrace = context.ToTemplateTraceWriter();
var schema = new PipelineTemplateSchemaFactory().CreateSchema();
var templateEvaluator = new PipelineTemplateEvaluator(templateTrace, schema);
var templateEvaluator = context.ToPipelineTemplateEvaluator();
foreach (var token in message.EnvironmentVariables)
{
var environmentVariables = templateEvaluator.EvaluateStepEnvironment(token, jobContext.ExpressionValues, VarUtil.EnvironmentVariableKeyComparer);
Expand Down
17 changes: 4 additions & 13 deletions src/Runner.Worker/StepsRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ public async Task RunAsync(IExecutionContext jobContext)
step.ExecutionContext.SetGitHubContext("action", actionStep.Action.Name);

// Evaluate and merge action's env block to env context
var templateTrace = step.ExecutionContext.ToTemplateTraceWriter();
var schema = new PipelineTemplateSchemaFactory().CreateSchema();
var templateEvaluator = new PipelineTemplateEvaluator(templateTrace, schema);
var templateEvaluator = step.ExecutionContext.ToPipelineTemplateEvaluator();
var actionEnvironment = templateEvaluator.EvaluateStepEnvironment(actionStep.Action.Environment, step.ExecutionContext.ExpressionValues, VarUtil.EnvironmentVariableKeyComparer);
foreach (var env in actionEnvironment)
{
Expand Down Expand Up @@ -247,7 +245,7 @@ private async Task RunStepAsync(IStep step, CancellationToken jobCancellationTok

// Set the timeout
var timeoutMinutes = 0;
var templateEvaluator = CreateTemplateEvaluator(step.ExecutionContext);
var templateEvaluator = step.ExecutionContext.ToPipelineTemplateEvaluator();
try
{
timeoutMinutes = templateEvaluator.EvaluateStepTimeout(step.Timeout, step.ExecutionContext.ExpressionValues);
Expand Down Expand Up @@ -389,7 +387,7 @@ private bool InitializeScope(IStep step, Dictionary<string, PipelineContextData>
executionContext.Debug($"Initializing scope '{scope.Name}'");
executionContext.ExpressionValues["steps"] = stepsContext.GetScope(scope.ParentName);
executionContext.ExpressionValues["inputs"] = !String.IsNullOrEmpty(scope.ParentName) ? scopeInputs[scope.ParentName] : null;
var templateEvaluator = CreateTemplateEvaluator(executionContext);
var templateEvaluator = executionContext.ToPipelineTemplateEvaluator();
var inputs = default(DictionaryContextData);
try
{
Expand Down Expand Up @@ -445,7 +443,7 @@ private void CompleteStep(IStep step, IStep nextStep, TaskResult? result = null,
executionContext.Debug($"Finalizing scope '{scope.Name}'");
executionContext.ExpressionValues["steps"] = stepsContext.GetScope(scope.Name);
executionContext.ExpressionValues["inputs"] = null;
var templateEvaluator = CreateTemplateEvaluator(executionContext);
var templateEvaluator = executionContext.ToPipelineTemplateEvaluator();
var outputs = default(DictionaryContextData);
try
{
Expand Down Expand Up @@ -477,12 +475,5 @@ private void CompleteStep(IStep step, IStep nextStep, TaskResult? result = null,

executionContext.Complete(result, resultCode: resultCode);
}

private PipelineTemplateEvaluator CreateTemplateEvaluator(IExecutionContext executionContext)
{
var templateTrace = executionContext.ToTemplateTraceWriter();
var schema = new PipelineTemplateSchemaFactory().CreateSchema();
return new PipelineTemplateEvaluator(templateTrace, schema);
}
}
}
12 changes: 9 additions & 3 deletions src/Sdk/DTObjectTemplating/ObjectTemplating/TemplateContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,30 @@ internal Int32 GetFileId(String file)
id = FileIds.Count + 1;
FileIds.Add(file, id);
FileNames.Add(file);
Memory.AddBytes(file);
}

return id;
}

internal String GetFileName(Int32 fileId)
{
return FileNames[fileId - 1];
return FileNames.Count >= fileId ? FileNames[fileId - 1] : null;
}

internal IReadOnlyList<String> GetFileTable()
{
return FileNames.AsReadOnly();
}

private String GetErrorPrefix(
Int32? fileId,
Int32? line,
Int32? column)
{
if (fileId != null)
var fileName = fileId.HasValue ? GetFileName(fileId.Value) : null;
if (!String.IsNullOrEmpty(fileName))
{
var fileName = GetFileName(fileId.Value);
if (line != null && column != null)
{
return $"{fileName} {TemplateStrings.LineColumn(line, column)}:";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ protected TemplateToken(
Column = column;
}

[IgnoreDataMember]
internal Int32? FileId { get; set; }
[DataMember(Name = "file", EmitDefaultValue = false)]
internal Int32? FileId { get; private set; }

[DataMember(Name = "line", EmitDefaultValue = false)]
internal Int32? Line { get; }
internal Int32? Line { get; private set; }

[DataMember(Name = "col", EmitDefaultValue = false)]
internal Int32? Column { get; }
internal Int32? Column { get; private set; }

[DataMember(Name = "type", EmitDefaultValue = false)]
internal Int32 Type { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,12 @@ public override void WriteJson(
Object value,
JsonSerializer serializer)
{
base.WriteJson(writer, value, serializer);
if (value is TemplateToken token)
{
switch (token.Type)
{
case TokenType.Null:
if (token.Line == null && token.Column == null)
if (token.FileId == null && token.Line == null && token.Column == null)
{
writer.WriteNull();
}
Expand All @@ -130,12 +129,17 @@ public override void WriteJson(
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue(token.Type);
if (token.FileId != null)
{
writer.WritePropertyName("file");
writer.WriteValue(token.FileId);
}
if (token.Line != null)
{
writer.WritePropertyName("line");
writer.WriteValue(token.Line);
}
if (token.Line != null)
if (token.Column != null)
{
writer.WritePropertyName("col");
writer.WriteValue(token.Column);
Expand All @@ -146,7 +150,7 @@ public override void WriteJson(

case TokenType.Boolean:
var booleanToken = token as BooleanToken;
if (token.Line == null && token.Column == null)
if (token.FileId == null && token.Line == null && token.Column == null)
{
writer.WriteValue(booleanToken.Value);
}
Expand All @@ -155,12 +159,17 @@ public override void WriteJson(
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue(token.Type);
if (token.FileId != null)
{
writer.WritePropertyName("file");
writer.WriteValue(token.FileId);
}
if (token.Line != null)
{
writer.WritePropertyName("line");
writer.WriteValue(token.Line);
}
if (token.Line != null)
if (token.Column != null)
{
writer.WritePropertyName("col");
writer.WriteValue(token.Column);
Expand All @@ -173,7 +182,7 @@ public override void WriteJson(

case TokenType.Number:
var numberToken = token as NumberToken;
if (token.Line == null && token.Column == null)
if (token.FileId == null && token.Line == null && token.Column == null)
{
writer.WriteValue(numberToken.Value);
}
Expand All @@ -182,12 +191,17 @@ public override void WriteJson(
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue(token.Type);
if (token.FileId != null)
{
writer.WritePropertyName("file");
writer.WriteValue(token.FileId);
}
if (token.Line != null)
{
writer.WritePropertyName("line");
writer.WriteValue(token.Line);
}
if (token.Line != null)
if (token.Column != null)
{
writer.WritePropertyName("col");
writer.WriteValue(token.Column);
Expand All @@ -200,7 +214,7 @@ public override void WriteJson(

case TokenType.String:
var stringToken = token as StringToken;
if (token.Line == null && token.Column == null)
if (token.FileId == null && token.Line == null && token.Column == null)
{
writer.WriteValue(stringToken.Value);
}
Expand All @@ -209,12 +223,17 @@ public override void WriteJson(
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue(token.Type);
if (token.FileId != null)
{
writer.WritePropertyName("file");
writer.WriteValue(token.FileId);
}
if (token.Line != null)
{
writer.WritePropertyName("line");
writer.WriteValue(token.Line);
}
if (token.Line != null)
if (token.Column != null)
{
writer.WritePropertyName("col");
writer.WriteValue(token.Column);
Expand All @@ -230,12 +249,17 @@ public override void WriteJson(
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue(token.Type);
if (token.FileId != null)
{
writer.WritePropertyName("file");
writer.WriteValue(token.FileId);
}
if (token.Line != null)
{
writer.WritePropertyName("line");
writer.WriteValue(token.Line);
}
if (token.Line != null)
if (token.Column != null)
{
writer.WritePropertyName("col");
writer.WriteValue(token.Column);
Expand All @@ -253,12 +277,17 @@ public override void WriteJson(
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue(token.Type);
if (token.FileId != null)
{
writer.WritePropertyName("file");
writer.WriteValue(token.FileId);
}
if (token.Line != null)
{
writer.WritePropertyName("line");
writer.WriteValue(token.Line);
}
if (token.Line != null)
if (token.Column != null)
{
writer.WritePropertyName("col");
writer.WriteValue(token.Column);
Expand All @@ -273,12 +302,17 @@ public override void WriteJson(
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue(token.Type);
if (token.FileId != null)
{
writer.WritePropertyName("file");
writer.WriteValue(token.FileId);
}
if (token.Line != null)
{
writer.WritePropertyName("line");
writer.WriteValue(token.Line);
}
if (token.Line != null)
if (token.Column != null)
{
writer.WritePropertyName("col");
writer.WriteValue(token.Column);
Expand All @@ -301,12 +335,17 @@ public override void WriteJson(
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue(token.Type);
if (token.FileId != null)
{
writer.WritePropertyName("file");
writer.WriteValue(token.FileId);
}
if (token.Line != null)
{
writer.WritePropertyName("line");
writer.WriteValue(token.Line);
}
if (token.Line != null)
if (token.Column != null)
{
writer.WritePropertyName("col");
writer.WriteValue(token.Column);
Expand Down
Loading

0 comments on commit 0cba425

Please sign in to comment.