From 18a38c799967879338dff0e16e585ca6db860d76 Mon Sep 17 00:00:00 2001 From: "Jonathan A. Sternberg" Date: Mon, 7 Jul 2025 13:43:24 -0500 Subject: [PATCH] debugger: update debugger components Debugger options have been updated to reflect the current version on master more closely. The names have been renamed to `Docker: Build` instead of `Dockerfile Debug`. The reason for this is to make the naming more consistent and make it so the bake target could be called `Docker: Bake`. Initial configurations and configuration snippets now more closely align with the default attributes most projects should expect. The vscode extension host debugging now uses `test/workspace` as the workspace folder and included a default dockerfile that can be used for testing debugging. Signed-off-by: Jonathan A. Sternberg --- .vscode/launch.json | 3 ++- package.json | 57 ++++++++++++++++++++++++++++++++------- src/dap/config.ts | 53 +++++++++++++++++++++++------------- test/workspace/Dockerfile | 15 +++++++++++ 4 files changed, 99 insertions(+), 29 deletions(-) create mode 100644 test/workspace/Dockerfile diff --git a/.vscode/launch.json b/.vscode/launch.json index 6c9e5d1..faf4d2f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,8 @@ "type": "extensionHost", "request": "launch", "args": [ - "--extensionDevelopmentPath=${workspaceFolder}" + "--extensionDevelopmentPath=${workspaceFolder}", + "${workspaceFolder}/test/workspace" ], "outFiles": [ "${workspaceFolder}/dist/**/*.js" diff --git a/package.json b/package.json index 9e0bf88..327971e 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "publisher": "docker", "categories": [ "Programming Languages", - "Linters" + "Linters", + "Debuggers" ], "repository": { "type": "git", @@ -34,6 +35,10 @@ "title": "Scan for CVEs with Docker Scout", "command": "docker.scout.imageScan", "enablement": "(view == dockerImages || view == vscode-containers.views.images) && viewItem == image" + }, + { + "title": "Build with Debugger", + "command": "docker.debug.editorContents" } ], "languages": [ @@ -67,21 +72,54 @@ "languages": [ "dockerfile" ], - "label": "Dockerfile Debug", + "label": "Docker: Build", "configurationAttributes": { "launch": { - "required": [ - "dockerfile" - ], + "required": [], "properties": { "dockerfile": { "type": "string", - "description": "Path to a Dockerfile", - "default": "${workspaceFolder}/Dockerfile" + "description": "Relative path from the context to the dockerfile.", + "default": "Dockerfile" + }, + "contextPath": { + "type": "string", + "description": "Path to the context.", + "default": "${workspaceFolder}" + }, + "target": { + "type": "string", + "description": "Target build stage to build." + }, + "args": { + "type": "array", + "description": "Arguments to pass to the build." } } } - } + }, + "initialConfigurations": [ + { + "type": "dockerfile", + "request": "launch", + "name": "Docker: Build", + "dockerfile": "Dockerfile", + "contextPath": "${workspaceFolder}" + } + ], + "configurationSnippets": [ + { + "label": "Docker: Build", + "description": "A new configuration for debugging a user selected Dockerfile.", + "body": { + "type": "dockerfile", + "request": "launch", + "name": "Docker: Build", + "dockerfile": "${2:Dockerfile}", + "contextPath": "^\"\\${workspaceFolder}\"" + } + } + ] } ], "configuration": { @@ -90,9 +128,8 @@ "docker.extension.enableBuildDebugging": { "type": "boolean", "description": "Enables build debugging. Note that changing this value requires a restart of Visual Studio Code to take effect.", - "markdownDescription": "Enable Compose editing features from the Docker DX extension. Note that changing this value requires a **restart** of Visual Studio Code to take effect.", + "markdownDescription": "Enable build debugging features from the Docker DX extension. Note that changing this value requires a **restart** of Visual Studio Code to take effect.", "default": false, - "scope": "application", "tags": [ "experimental" ] diff --git a/src/dap/config.ts b/src/dap/config.ts index c86e196..ebac9b9 100644 --- a/src/dap/config.ts +++ b/src/dap/config.ts @@ -1,29 +1,24 @@ -import * as path from 'path'; import * as vscode from 'vscode'; import { getExtensionSetting } from '../utils/settings'; +export const DebugEditorContentsCommandId = 'docker.debug.editorContents'; + class DebugAdapterExecutableFactory implements vscode.DebugAdapterDescriptorFactory { createDebugAdapterDescriptor( session: vscode.DebugSession, ): vscode.ProviderResult { - const parent = path.dirname(session.configuration.dockerfile); - return new vscode.DebugAdapterExecutable( - 'docker', - [ - 'buildx', - 'dap', - 'build', - '-f', - session.configuration.dockerfile, - parent, - ], - { - cwd: parent, - env: { BUILDX_EXPERIMENTAL: '1' }, - }, - ); + var args = ['buildx', 'dap', 'build']; + if (session.configuration?.args) { + args = args.concat(session.configuration?.args); + } + + const options = { + cwd: session.workspaceFolder?.uri.path, + }; + + return new vscode.DebugAdapterExecutable('docker', args, options); } } @@ -39,9 +34,10 @@ class DockerfileConfigurationProvider const editor = vscode.window.activeTextEditor; if (editor !== undefined && editor.document.languageId === 'dockerfile') { config.type = 'dockerfile'; - config.name = 'Debug Dockerfile'; + config.name = 'Docker: Build'; config.request = 'launch'; config.dockerfile = editor.document.uri.fsPath; + config.contextPath = '${workspaceFolder}'; } } return config; @@ -109,6 +105,27 @@ export function setupDebugging(ctx: vscode.ExtensionContext) { let channel = vscode.window.createOutputChannel('Dockerfile Debug', 'log'); + ctx.subscriptions.push( + vscode.commands.registerCommand( + DebugEditorContentsCommandId, + async (resource: vscode.Uri) => { + let targetResource = resource; + if (!targetResource && vscode.window.activeTextEditor) { + targetResource = vscode.window.activeTextEditor.document.uri; + } + if (targetResource) { + vscode.debug.startDebugging(undefined, { + type: 'dockerfile', + name: 'Docker: Build', + request: 'launch', + dockerfile: targetResource.fsPath, + contextPath: '${workspaceFolder}', + }); + } + }, + ), + ); + ctx.subscriptions.push( vscode.debug.registerDebugConfigurationProvider( 'dockerfile', diff --git a/test/workspace/Dockerfile b/test/workspace/Dockerfile new file mode 100644 index 0000000..282f933 --- /dev/null +++ b/test/workspace/Dockerfile @@ -0,0 +1,15 @@ +ARG FOO="init" +ARG AAA="init" + +FROM busybox AS build1 +RUN echo hello > /hello + +FROM busybox AS build2 +ARG FOO +ARG AAA +RUN echo hi > /hi && cat /fail + +FROM scratch +COPY --from=build1 /hello / +RUN cat fail > / +COPY --from=build2 /hi /