File size: 10,480 Bytes
6582e08 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | /*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import * as os from 'os'
import * as path from 'path'
import {
DotNetDebugConfiguration,
dotnetDebuggerPath,
getCodeRoot,
isImageLambdaConfig,
} from '../../../lambda/local/debugConfiguration'
import { RuntimeFamily } from '../../../lambda/models/samLambdaRuntime'
import * as pathutil from '../../../shared/utilities/pathUtils'
import { ExtContext } from '../../extensions'
import { DefaultSamLocalInvokeCommand, waitForDebuggerMessages } from '../cli/samCliLocalInvoke'
import { runLambdaFunction, waitForPort } from '../localLambdaRunner'
import { SamLaunchRequestArgs } from './awsSamDebugger'
import { ChildProcess } from '../../utilities/processUtils'
import { HttpResourceFetcher } from '../../resourcefetcher/httpResourceFetcher'
import { getLogger } from '../../logger/logger'
import * as vscode from 'vscode'
import * as nls from 'vscode-nls'
import globals from '../../extensionGlobals'
import fs from '../../fs/fs'
const localize = nls.loadMessageBundle()
/**
* Gathers and sets launch-config info by inspecting the workspace and creating
* temp files/directories as needed.
*
* Does NOT execute/invoke SAM, docker, etc.
*/
export async function makeCsharpConfig(config: SamLaunchRequestArgs): Promise<SamLaunchRequestArgs> {
if (!config.baseBuildDir) {
throw Error('invalid state: config.baseBuildDir was not set')
}
config.codeRoot = (await getCodeRoot(config.workspaceFolder, config))!
// TODO: avoid the reassignment
// TODO: walk the tree to find .sln, .csproj ?
const originalCodeRoot = config.codeRoot
config.codeRoot = getSamProjectDirPathForFile(config.templatePath)
config = {
...config,
// https://github.com/dotnet/vscode-csharp/blob/a270be4f2f1236d9bc1b31b6214f1672096788b7/src/lsptoolshost/debugger.ts#L45C1-L54
// The "ms-dotnettools.csharp" extension declares:
// - "coreclr" as the debugger type for ".NET 5+ and .NET Core"
// - "dotnet" as the debugger type for "C#"
// but haven't found the actual circumstances when "dotnet" works while "coreclr" doesn't.
// type: config.runtime == 'dotnet5.0' ? 'coreclr' : 'dotnet',
type: 'coreclr',
request: config.noDebug ? 'launch' : 'attach',
runtimeFamily: RuntimeFamily.DotNet,
}
if (config.sam?.containerBuild) {
config.mountWith = 'write'
}
if (!config.noDebug) {
config = await makeDotnetDebugConfiguration(config, originalCodeRoot)
}
return config
}
/**
* Launches and attaches debugger to a SAM dotnet (csharp) project.
*
* We spin up a C# Lambda Docker container, download and build the debugger for
* Linux, then mount it with the SAM app on run. User's C# workspace dir will
* have a `.vsdbg` dir after the first run.
*/
export async function invokeCsharpLambda(ctx: ExtContext, config: SamLaunchRequestArgs): Promise<SamLaunchRequestArgs> {
config.samLocalInvokeCommand = new DefaultSamLocalInvokeCommand([waitForDebuggerMessages.DOTNET])
// eslint-disable-next-line @typescript-eslint/unbound-method
config.onWillAttachDebugger = waitForPort
const platformArchitecture = os.arch()
if (!config.noDebug) {
if ([config.architecture, platformArchitecture].includes('arm64')) {
void vscode.window.showWarningMessage(
localize(
'AWS.sam.noArm.dotnet.debug',
'The vsdbg debugger does not currently support the arm64 architecture. Function will run locally without debug.'
)
)
getLogger().warn('SAM Invoke: Attempting to debug dotnet on ARM - removing debug flag.')
config.noDebug = true
}
}
return await runLambdaFunction(ctx, config, async () => {
if (!config.noDebug) {
await _installDebugger({
debuggerPath: config.debuggerPath!,
})
}
})
}
interface InstallDebuggerArgs {
debuggerPath: string
}
function getDebuggerPath(parentFolder: string): string {
return path.resolve(parentFolder, '.vsdbg')
}
async function _installDebugger({ debuggerPath }: InstallDebuggerArgs): Promise<void> {
await fs.mkdir(debuggerPath)
try {
getLogger().info(
localize(
'AWS.samcli.local.invoke.debugger.install',
'Installing .NET Core Debugger to {0}...',
debuggerPath
)
)
const vsDbgVersion = 'latest'
// TODO: If vsdbg works with qemu, have this detect Architecture and swap to `linux-arm64` if ARM.
// See https://github.com/OmniSharp/omnisharp-vscode/issues/3277 ;
// qemu appears to set PrivateTmp=true : https://github.com/qemu/qemu/blob/326ff8dd09556fc2e257196c49f35009700794ac/contrib/systemd/qemu-pr-helper.service#L8 ?
const vsDbgRuntime = 'linux-x64'
const installScriptPath = await downloadInstallScript(debuggerPath)
let installCommand: string
let installArgs: string[]
if (os.platform() === 'win32') {
const windir = process.env['WINDIR']
if (!windir) {
throw new Error('Environment variable `WINDIR` not defined')
}
installCommand = `${windir}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`
installArgs = [
'-NonInteractive',
'-NoProfile',
'-WindowStyle',
'Hidden',
'-ExecutionPolicy',
'RemoteSigned',
'-File',
installScriptPath,
'-Version',
vsDbgVersion,
'-RuntimeID',
vsDbgRuntime,
'-InstallPath',
debuggerPath,
]
} else {
installCommand = installScriptPath
installArgs = ['-v', vsDbgVersion, '-r', vsDbgRuntime, '-l', debuggerPath]
}
const childProcess = new ChildProcess(installCommand, installArgs)
const install = await childProcess.run({
onStdout: (text: string) => {
globals.outputChannel.append(text)
},
onStderr: (text: string) => {
globals.outputChannel.append(text)
},
})
if (install.exitCode) {
throw new Error(`command failed (exit code: ${install.exitCode}): ${installCommand}`)
}
} catch (err) {
getLogger().info(
localize(
'AWS.samcli.local.invoke.debugger.install.failed',
'Error installing .NET Core Debugger: {0}',
err instanceof Error ? (err as Error).message : String(err)
)
)
throw err
}
}
async function downloadInstallScript(debuggerPath: string): Promise<string> {
let installScriptUrl: string
let installScriptPath: string
if (os.platform() === 'win32') {
installScriptUrl = 'https://aka.ms/getvsdbgps1'
installScriptPath = path.join(debuggerPath, 'installVsdbgScript.ps1')
} else {
installScriptUrl = 'https://aka.ms/getvsdbgsh'
installScriptPath = path.join(debuggerPath, 'installVsdbgScript.sh')
}
const installScriptFetcher = await new HttpResourceFetcher(installScriptUrl, { showUrl: true }).get()
const installScript = await installScriptFetcher?.text()
if (!installScript) {
throw Error(`Failed to download ${installScriptUrl}`)
}
await fs.writeFile(installScriptPath, installScript, 'utf8')
await fs.chmod(installScriptPath, 0o700)
return installScriptPath
}
function getSamProjectDirPathForFile(filepath: string): string {
return pathutil.normalize(path.dirname(filepath))
}
/**
* Creates a .NET launch-config composed with the given `config`.
*/
export async function makeDotnetDebugConfiguration(
config: SamLaunchRequestArgs,
codeUri: string
): Promise<DotNetDebugConfiguration> {
if (config.noDebug) {
throw Error(`SAM debug: invalid config: ${config.name}`)
}
const pipeArgs = ['-c', `docker exec -i $(docker ps -q -f publish=${config.debugPort}) \${debuggerCommand}`]
config.debuggerPath = pathutil.normalize(getDebuggerPath(codeUri))
await fs.mkdir(config.debuggerPath)
const isImageLambda = await isImageLambdaConfig(config)
if (isImageLambda && !config.noDebug) {
config.containerEnvVars = {
_AWS_LAMBDA_DOTNET_DEBUGGING: '1',
}
}
if (os.platform() === 'win32') {
// Coerce drive letter to uppercase. While Windows is case-insensitive, sourceFileMap is case-sensitive.
codeUri = codeUri.replace(pathutil.driveLetterRegex, (match) => match.toUpperCase())
}
if (isImageLambda) {
// default build path, in dotnet image-based templates
// Not needed for ZIP lambdas, because SAM prevents dotnet from being
// built in-container thus PDBs already point to the user workspace.
if (!config.sourceFileMap) {
config.sourceFileMap = {}
}
config.sourceFileMap['/build'] = codeUri
}
if (config.lambda?.pathMappings !== undefined) {
if (!config.sourceFileMap) {
config.sourceFileMap = {}
}
// we could safely leave this entry in, but might as well give the user full control if they're specifying mappings
delete config.sourceFileMap['/build']
for (const mapping of config.lambda.pathMappings) {
// this looks weird because we're mapping the PDB path to the local workspace
config.sourceFileMap[mapping.remoteRoot] = mapping.localRoot
}
}
return {
...config,
runtimeFamily: RuntimeFamily.DotNet,
request: 'attach',
// Since SAM CLI 1.0 we cannot assume PID=1. So use processName=dotnet
// instead of processId=1.
processName: 'dotnet',
pipeTransport: {
pipeProgram: 'sh',
pipeArgs,
debuggerPath: dotnetDebuggerPath,
pipeCwd: codeUri,
},
windows: {
pipeTransport: {
pipeProgram: 'powershell',
pipeArgs,
debuggerPath: dotnetDebuggerPath,
pipeCwd: codeUri,
},
},
}
}
|