-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add DAP server #4298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rentziass
wants to merge
28
commits into
main
Choose a base branch
from
rentziass/debugger
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add DAP server #4298
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
8b1b23b
Get EnableDebugger from job context
rentziass cca15de
Add DAP protocol message types and service interfaces
rentziass 9737dfa
Add DAP TCP server with reconnection support
rentziass 17b05dd
Add minimal DAP debug session with next/continue support
rentziass 915e13c
Integrate DAP debugger into JobRunner and StepsRunner
rentziass 3d8c844
Add DapVariableProvider for scope inspection with centralized masking
rentziass 2c65db1
Wire DapVariableProvider into DapDebugSession for scope inspection
rentziass 0d33fd1
Add L0 tests for DAP scope inspection and secret masking
rentziass 1573e36
Add expression evaluation to DapVariableProvider
rentziass f31e1c7
Wire evaluate request into DapDebugSession
rentziass 2a98a8c
Add L0 tests for DAP expression evaluation
rentziass 852e872
Add DAP REPL command model and parser
rentziass 735dd69
Add DapReplExecutor for run command execution
rentziass 165fb90
Wire REPL routing into DapDebugSession
rentziass b76917a
Add L0 tests for REPL parser and session routing
rentziass 860a919
Fix expression expansion in REPL run command
rentziass 8d6b38a
Add completions support and friendly errors for unsupported commands
rentziass a8f3b91
Harden DAP server
rentziass e4406e0
Fix debug session race conditions and step-flow bugs
rentziass 75760d1
Centralize outbound DAP masking and harden secrets scope
rentziass 649dc74
More tests
rentziass 8d1e06f
Remove centralized masking
rentziass 5bad8cb
Mask step display names
rentziass 00bde90
remove waits
rentziass e11d6cf
lock state
rentziass 7d0f26a
encoding casting
rentziass 9d33c82
volatile state
rentziass 9cd74b0
ci
rentziass File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,377 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using GitHub.DistributedTask.Pipelines.ContextData; | ||
| using GitHub.Runner.Common; | ||
| using GitHub.Runner.Common.Util; | ||
| using GitHub.Runner.Sdk; | ||
| using GitHub.Runner.Worker.Handlers; | ||
|
|
||
| namespace GitHub.Runner.Worker.Dap | ||
| { | ||
| /// <summary> | ||
| /// Executes <see cref="RunCommand"/> objects in the job's runtime context. | ||
| /// | ||
| /// Mirrors the behavior of a normal workflow <c>run:</c> step as closely | ||
| /// as possible by reusing the runner's existing shell-resolution logic, | ||
| /// script fixup helpers, and process execution infrastructure. | ||
| /// | ||
| /// Output is streamed to the debugger via DAP <c>output</c> events with | ||
| /// secrets masked before emission. | ||
| /// </summary> | ||
| internal sealed class DapReplExecutor | ||
| { | ||
| private readonly IHostContext _hostContext; | ||
| private readonly IDapServer _server; | ||
| private readonly Tracing _trace; | ||
|
|
||
| public DapReplExecutor(IHostContext hostContext, IDapServer server) | ||
| { | ||
| _hostContext = hostContext ?? throw new ArgumentNullException(nameof(hostContext)); | ||
| _server = server; | ||
| _trace = hostContext.GetTrace(nameof(DapReplExecutor)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Executes a <see cref="RunCommand"/> and returns the exit code as a | ||
| /// formatted <see cref="EvaluateResponseBody"/>. | ||
| /// </summary> | ||
| public async Task<EvaluateResponseBody> ExecuteRunCommandAsync( | ||
| RunCommand command, | ||
| IExecutionContext context, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| if (context == null) | ||
| { | ||
| return ErrorResult("No execution context available. The debugger must be paused at a step to run commands."); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| return await ExecuteScriptAsync(command, context, cancellationToken); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _trace.Error($"REPL run command failed ({ex.GetType().Name})"); | ||
| var maskedError = _hostContext.SecretMasker.MaskSecrets(ex.Message); | ||
| return ErrorResult($"Command failed: {maskedError}"); | ||
| } | ||
| } | ||
|
|
||
| private async Task<EvaluateResponseBody> ExecuteScriptAsync( | ||
| RunCommand command, | ||
| IExecutionContext context, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| // 1. Resolve shell — same logic as ScriptHandler | ||
| string shellCommand; | ||
| string argFormat; | ||
|
|
||
| if (!string.IsNullOrEmpty(command.Shell)) | ||
| { | ||
| // Explicit shell from the DSL | ||
| var parsed = ScriptHandlerHelpers.ParseShellOptionString(command.Shell); | ||
| shellCommand = parsed.shellCommand; | ||
| argFormat = string.IsNullOrEmpty(parsed.shellArgs) | ||
| ? ScriptHandlerHelpers.GetScriptArgumentsFormat(shellCommand) | ||
| : parsed.shellArgs; | ||
| } | ||
| else | ||
| { | ||
| // Default shell — mirrors ScriptHandler platform defaults | ||
| shellCommand = ResolveDefaultShell(context); | ||
| argFormat = ScriptHandlerHelpers.GetScriptArgumentsFormat(shellCommand); | ||
| } | ||
|
|
||
| _trace.Info("Resolved REPL shell"); | ||
|
|
||
| // 2. Expand ${{ }} expressions in the script body, just like | ||
| // ActionRunner evaluates step inputs before ScriptHandler sees them | ||
| var contents = ExpandExpressions(command.Script, context); | ||
| contents = ScriptHandlerHelpers.FixUpScriptContents(shellCommand, contents); | ||
|
|
||
| // Write to a temp file (same pattern as ScriptHandler) | ||
| var extension = ScriptHandlerHelpers.GetScriptFileExtension(shellCommand); | ||
| var scriptFilePath = Path.Combine( | ||
| _hostContext.GetDirectory(WellKnownDirectory.Temp), | ||
| $"dap_repl_{Guid.NewGuid()}{extension}"); | ||
|
|
||
| Encoding encoding = new UTF8Encoding(false); | ||
| #if OS_WINDOWS | ||
| contents = contents.Replace("\r\n", "\n").Replace("\n", "\r\n"); | ||
| encoding = Console.InputEncoding.CodePage != 65001 | ||
| ? Console.InputEncoding | ||
| : encoding; | ||
| #endif | ||
| File.WriteAllText(scriptFilePath, contents, encoding); | ||
|
|
||
| try | ||
| { | ||
| // 3. Format arguments with script path | ||
| var resolvedPath = scriptFilePath.Replace("\"", "\\\""); | ||
| if (string.IsNullOrEmpty(argFormat) || !argFormat.Contains("{0}")) | ||
| { | ||
| return ErrorResult($"Invalid shell option '{shellCommand}'. Shell must be a valid built-in (bash, sh, cmd, powershell, pwsh) or a format string containing '{{0}}'"); | ||
| } | ||
| var arguments = string.Format(argFormat, resolvedPath); | ||
|
|
||
| // 4. Resolve shell command path | ||
| string prependPath = string.Join( | ||
| Path.PathSeparator.ToString(), | ||
| Enumerable.Reverse(context.Global.PrependPath)); | ||
| var commandPath = WhichUtil.Which(shellCommand, false, _trace, prependPath) | ||
| ?? shellCommand; | ||
|
|
||
| // 5. Build environment — merge from execution context like a real step | ||
| var environment = BuildEnvironment(context, command.Env); | ||
|
|
||
| // 6. Resolve working directory | ||
| var workingDirectory = command.WorkingDirectory; | ||
| if (string.IsNullOrEmpty(workingDirectory)) | ||
| { | ||
| var githubContext = context.ExpressionValues.TryGetValue("github", out var gh) | ||
| ? gh as DictionaryContextData | ||
| : null; | ||
| var workspace = githubContext?.TryGetValue("workspace", out var ws) == true | ||
| ? (ws as StringContextData)?.Value | ||
| : null; | ||
| workingDirectory = workspace ?? _hostContext.GetDirectory(WellKnownDirectory.Work); | ||
| } | ||
|
|
||
| _trace.Info("Executing REPL command"); | ||
|
|
||
| // Stream execution info to debugger | ||
| SendOutput("console", $"$ {shellCommand} {command.Script.Substring(0, Math.Min(command.Script.Length, 80))}{(command.Script.Length > 80 ? "..." : "")}\n"); | ||
|
|
||
| // 7. Execute via IProcessInvoker (same as DefaultStepHost) | ||
| int exitCode; | ||
| using (var processInvoker = _hostContext.CreateService<IProcessInvoker>()) | ||
| { | ||
| processInvoker.OutputDataReceived += (sender, args) => | ||
| { | ||
| if (!string.IsNullOrEmpty(args.Data)) | ||
| { | ||
| var masked = _hostContext.SecretMasker.MaskSecrets(args.Data); | ||
| SendOutput("stdout", masked + "\n"); | ||
| } | ||
| }; | ||
|
|
||
| processInvoker.ErrorDataReceived += (sender, args) => | ||
| { | ||
| if (!string.IsNullOrEmpty(args.Data)) | ||
| { | ||
| var masked = _hostContext.SecretMasker.MaskSecrets(args.Data); | ||
| SendOutput("stderr", masked + "\n"); | ||
| } | ||
| }; | ||
|
|
||
| exitCode = await processInvoker.ExecuteAsync( | ||
| workingDirectory: workingDirectory, | ||
| fileName: commandPath, | ||
| arguments: arguments, | ||
| environment: environment, | ||
| requireExitCodeZero: false, | ||
| outputEncoding: null, | ||
| killProcessOnCancel: true, | ||
| cancellationToken: cancellationToken); | ||
| } | ||
|
|
||
| _trace.Info($"REPL command exited with code {exitCode}"); | ||
|
|
||
| // 8. Return only the exit code summary (output was already streamed) | ||
| return new EvaluateResponseBody | ||
| { | ||
| Result = exitCode == 0 ? $"(exit code: {exitCode})" : $"Process completed with exit code {exitCode}.", | ||
| Type = exitCode == 0 ? "string" : "error", | ||
| VariablesReference = 0 | ||
| }; | ||
| } | ||
| finally | ||
| { | ||
| // Clean up temp script file | ||
| try { File.Delete(scriptFilePath); } | ||
| catch { /* best effort */ } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Expands <c>${{ }}</c> expressions in the input string using the | ||
| /// runner's template evaluator — the same evaluation path that processes | ||
| /// step inputs before <see cref="ScriptHandler"/> runs them. | ||
| /// | ||
| /// Each <c>${{ expr }}</c> occurrence is individually evaluated and | ||
| /// replaced with its masked string result, mirroring the semantics of | ||
| /// expression interpolation in a workflow <c>run:</c> step body. | ||
| /// </summary> | ||
| internal string ExpandExpressions(string input, IExecutionContext context) | ||
| { | ||
| if (string.IsNullOrEmpty(input) || !input.Contains("${{")) | ||
| { | ||
| return input ?? string.Empty; | ||
| } | ||
|
|
||
| var result = new StringBuilder(); | ||
| int pos = 0; | ||
|
|
||
| while (pos < input.Length) | ||
| { | ||
| var start = input.IndexOf("${{", pos, StringComparison.Ordinal); | ||
| if (start < 0) | ||
| { | ||
| result.Append(input, pos, input.Length - pos); | ||
| break; | ||
| } | ||
|
|
||
| // Append the literal text before the expression | ||
| result.Append(input, pos, start - pos); | ||
|
|
||
| var end = input.IndexOf("}}", start + 3, StringComparison.Ordinal); | ||
| if (end < 0) | ||
| { | ||
| // Unterminated expression — keep literal | ||
| result.Append(input, start, input.Length - start); | ||
| break; | ||
| } | ||
|
|
||
| var expr = input.Substring(start + 3, end - start - 3).Trim(); | ||
| end += 2; // skip past "}}" | ||
|
|
||
| // Evaluate the expression | ||
| try | ||
| { | ||
| var templateEvaluator = context.ToPipelineTemplateEvaluator(); | ||
| var token = new GitHub.DistributedTask.ObjectTemplating.Tokens.BasicExpressionToken( | ||
| null, null, null, expr); | ||
| var evaluated = templateEvaluator.EvaluateStepDisplayName( | ||
| token, | ||
| context.ExpressionValues, | ||
| context.ExpressionFunctions); | ||
| result.Append(_hostContext.SecretMasker.MaskSecrets(evaluated ?? string.Empty)); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _trace.Warning($"Expression expansion failed ({ex.GetType().Name})"); | ||
| // Keep the original expression literal on failure | ||
| result.Append(input, start, end - start); | ||
| } | ||
|
|
||
| pos = end; | ||
| } | ||
|
|
||
| return result.ToString(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Resolves the default shell the same way <see cref="ScriptHandler"/> | ||
| /// does: check job defaults, then fall back to platform default. | ||
| /// </summary> | ||
| internal string ResolveDefaultShell(IExecutionContext context) | ||
| { | ||
| // Check job defaults | ||
| if (context.Global?.JobDefaults != null && | ||
| context.Global.JobDefaults.TryGetValue("run", out var runDefaults) && | ||
| runDefaults.TryGetValue("shell", out var defaultShell) && | ||
| !string.IsNullOrEmpty(defaultShell)) | ||
| { | ||
| _trace.Info("Using job default shell"); | ||
| return defaultShell; | ||
| } | ||
|
|
||
| #if OS_WINDOWS | ||
| string prependPath = string.Join( | ||
| Path.PathSeparator.ToString(), | ||
| context.Global?.PrependPath != null ? Enumerable.Reverse(context.Global.PrependPath) : Array.Empty<string>()); | ||
| var pwshPath = WhichUtil.Which("pwsh", false, _trace, prependPath); | ||
| return !string.IsNullOrEmpty(pwshPath) ? "pwsh" : "powershell"; | ||
| #else | ||
| return "sh"; | ||
| #endif | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Merges the job context environment with any REPL-specific overrides. | ||
| /// </summary> | ||
| internal Dictionary<string, string> BuildEnvironment( | ||
| IExecutionContext context, | ||
| Dictionary<string, string> replEnv) | ||
| { | ||
| var env = new Dictionary<string, string>(VarUtil.EnvironmentVariableKeyComparer); | ||
|
|
||
| // Pull environment from the execution context (same as ActionRunner) | ||
| if (context.ExpressionValues.TryGetValue("env", out var envData)) | ||
| { | ||
| if (envData is DictionaryContextData dictEnv) | ||
| { | ||
| foreach (var pair in dictEnv) | ||
| { | ||
| if (pair.Value is StringContextData str) | ||
| { | ||
| env[pair.Key] = str.Value; | ||
| } | ||
| } | ||
| } | ||
| else if (envData is CaseSensitiveDictionaryContextData csEnv) | ||
| { | ||
| foreach (var pair in csEnv) | ||
| { | ||
| if (pair.Value is StringContextData str) | ||
| { | ||
| env[pair.Key] = str.Value; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Expose runtime context variables to the environment (GITHUB_*, RUNNER_*, etc.) | ||
| foreach (var ctxPair in context.ExpressionValues) | ||
| { | ||
| if (ctxPair.Value is IEnvironmentContextData runtimeContext && runtimeContext != null) | ||
| { | ||
| foreach (var rtEnv in runtimeContext.GetRuntimeEnvironmentVariables()) | ||
| { | ||
| env[rtEnv.Key] = rtEnv.Value; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Apply REPL-specific overrides last (so they win), | ||
| // expanding any ${{ }} expressions in the values | ||
| if (replEnv != null) | ||
| { | ||
| foreach (var pair in replEnv) | ||
| { | ||
| env[pair.Key] = ExpandExpressions(pair.Value, context); | ||
| } | ||
| } | ||
|
|
||
| return env; | ||
| } | ||
|
|
||
| private void SendOutput(string category, string text) | ||
| { | ||
| _server?.SendEvent(new Event | ||
| { | ||
| EventType = "output", | ||
| Body = new OutputEventBody | ||
| { | ||
| Category = category, | ||
| Output = text | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private static EvaluateResponseBody ErrorResult(string message) | ||
| { | ||
| return new EvaluateResponseBody | ||
| { | ||
| Result = message, | ||
| Type = "error", | ||
| VariablesReference = 0 | ||
| }; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enables
runlike commands through REPL: