Skip to content

Commit 18b0bd8

Browse files
authored
SAM debugconfig: fix env-vars.json (#1167)
- Problem: Since 7b5aae7 , `target=template` uses the original resource name instead of the dummy name `awsToolkitSamLocalResource`. But we forgot to update the name in `env-vars.json`, so envvars specified in `launch.json` were not applied. - Solution: use the correct name in `env-vars.json`,
1 parent eaa4a00 commit 18b0bd8

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/shared/sam/localLambdaRunner.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,20 @@ export interface SAMTemplateEnvironmentVariables {
4545
}
4646
}
4747

48-
function getEnvironmentVariables(env?: { [k: string]: string | number | boolean }): SAMTemplateEnvironmentVariables {
49-
return env ? { [TEMPLATE_RESOURCE_NAME]: env } : {}
48+
function getEnvironmentVariables(
49+
resourceName: string,
50+
env?: { [k: string]: string | number | boolean }
51+
): SAMTemplateEnvironmentVariables {
52+
return env ? { [resourceName]: env } : {}
53+
}
54+
55+
/**
56+
* Decides the resource name for the generated template.yaml.
57+
*/
58+
function makeResourceName(config: SamLaunchRequestArgs): string {
59+
return config.invokeTarget.target === 'code' ? 'awsToolkitSamLocalResource' : config.invokeTarget.logicalId
5060
}
5161

52-
const TEMPLATE_RESOURCE_NAME = 'awsToolkitSamLocalResource'
5362
const SAM_LOCAL_PORT_CHECK_RETRY_INTERVAL_MILLIS: number = 125
5463
const SAM_LOCAL_PORT_CHECK_RETRY_TIMEOUT_MILLIS_DEFAULT: number = 30000
5564
const MAX_DEBUGGER_RETRIES_DEFAULT: number = 30
@@ -81,6 +90,7 @@ export function getRelativeFunctionHandler(params: {
8190
export async function makeInputTemplate(config: SamLaunchRequestArgs): Promise<string> {
8291
let newTemplate: SamTemplateGenerator
8392
let inputTemplatePath: string
93+
const resourceName = makeResourceName(config)
8494

8595
// use existing template to create a temporary template with a resource that has an overridden handler name.
8696
// this is necessary for Python, which overrides the handler name due to the debug file.
@@ -101,7 +111,7 @@ export async function makeInputTemplate(config: SamLaunchRequestArgs): Promise<s
101111
}
102112

103113
newTemplate = new SamTemplateGenerator(template).withTemplateResources({
104-
[config.invokeTarget.logicalId]: resourceWithOverriddenHandler,
114+
[resourceName]: resourceWithOverriddenHandler,
105115
})
106116

107117
// template type uses the template dir and a throwaway template name so we can use existing relative paths
@@ -111,7 +121,7 @@ export async function makeInputTemplate(config: SamLaunchRequestArgs): Promise<s
111121
} else {
112122
newTemplate = new SamTemplateGenerator()
113123
.withFunctionHandler(config.handlerName)
114-
.withResourceName(TEMPLATE_RESOURCE_NAME)
124+
.withResourceName(resourceName)
115125
.withRuntime(config.runtime)
116126
.withCodeUri(config.codeRoot)
117127
if (config.lambda?.environmentVariables) {
@@ -212,8 +222,7 @@ export async function invokeLambdaFunction(
212222
const maxRetries: number = getAttachDebuggerMaxRetryLimit(ctx.settings, MAX_DEBUGGER_RETRIES_DEFAULT)
213223

214224
const localInvokeArgs: SamCliLocalInvokeInvocationArguments = {
215-
templateResourceName:
216-
config.invokeTarget.target === 'code' ? TEMPLATE_RESOURCE_NAME : config.invokeTarget.logicalId,
225+
templateResourceName: makeResourceName(config),
217226
templatePath: config.templatePath,
218227
eventPath: config.eventPayloadFile,
219228
environmentVariablePath: config.envFile,
@@ -467,7 +476,7 @@ export async function makeConfig(config: SamLaunchRequestArgs): Promise<void> {
467476
config.envFile = path.join(config.baseBuildDir!, 'env-vars.json')
468477

469478
// env-vars.json (NB: effectively ignored for the `target=code` case).
470-
const env = JSON.stringify(getEnvironmentVariables(config.lambda?.environmentVariables))
479+
const env = JSON.stringify(getEnvironmentVariables(makeResourceName(config), config.lambda?.environmentVariables))
471480
await writeFile(config.envFile, env)
472481

473482
// event.json

src/test/shared/sam/debugger/samDebugConfigProvider.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ describe('SamDebugConfigurationProvider', async () => {
557557
assertEqualLaunchConfigs(actual, expected, appDir)
558558
assertFileText(
559559
expected.envFile,
560-
'{"awsToolkitSamLocalResource":{"test-js-template-envvar-1":"test target=template envvar value 1","test-js-template-envvar-2":"test target=template envvar value 2"}}'
560+
'{"SourceCodeTwoFoldersDeep":{"test-js-template-envvar-1":"test target=template envvar value 1","test-js-template-envvar-2":"test target=template envvar value 2"}}'
561561
)
562562
assertFileText(
563563
expected.eventPayloadFile,
@@ -826,7 +826,7 @@ describe('SamDebugConfigurationProvider', async () => {
826826
}
827827

828828
assertEqualLaunchConfigs(actual, expected, appDir)
829-
assertFileText(expected.envFile, '{"awsToolkitSamLocalResource":{"test-envvar-1":"test value 1"}}')
829+
assertFileText(expected.envFile, '{"HelloWorldFunction":{"test-envvar-1":"test value 1"}}')
830830
assertFileText(expected.eventPayloadFile, '{"test-payload-key-1":"test payload value 1"}')
831831

832832
assertFileText(
@@ -1097,7 +1097,7 @@ Outputs:
10971097
}
10981098

10991099
assertEqualLaunchConfigs(actual, expected, appDir)
1100-
assertFileText(expected.envFile, '{"awsToolkitSamLocalResource":{}}')
1100+
assertFileText(expected.envFile, '{"HelloWorldFunction":{}}')
11011101
assertFileText(expected.eventPayloadFile, '{}')
11021102

11031103
assertFileText(
@@ -1250,7 +1250,7 @@ Outputs:
12501250
}
12511251

12521252
assertEqualLaunchConfigs(actual, expected, appDir)
1253-
assertFileText(expected.envFile, '{"awsToolkitSamLocalResource":{"var1":"2","var2":"1"}}')
1253+
assertFileText(expected.envFile, '{"myResource":{"var1":"2","var2":"1"}}')
12541254
assertFileText(expected.eventPayloadFile, '{}')
12551255

12561256
assertFileText(

0 commit comments

Comments
 (0)