Skip to content

Commit

Permalink
SAM debugconfig: fix env-vars.json (#1167)
Browse files Browse the repository at this point in the history
- 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`,
  • Loading branch information
justinmk3 authored Jun 25, 2020
1 parent eaa4a00 commit 18b0bd8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
25 changes: 17 additions & 8 deletions src/shared/sam/localLambdaRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,20 @@ export interface SAMTemplateEnvironmentVariables {
}
}

function getEnvironmentVariables(env?: { [k: string]: string | number | boolean }): SAMTemplateEnvironmentVariables {
return env ? { [TEMPLATE_RESOURCE_NAME]: env } : {}
function getEnvironmentVariables(
resourceName: string,
env?: { [k: string]: string | number | boolean }
): SAMTemplateEnvironmentVariables {
return env ? { [resourceName]: env } : {}
}

/**
* Decides the resource name for the generated template.yaml.
*/
function makeResourceName(config: SamLaunchRequestArgs): string {
return config.invokeTarget.target === 'code' ? 'awsToolkitSamLocalResource' : config.invokeTarget.logicalId
}

const TEMPLATE_RESOURCE_NAME = 'awsToolkitSamLocalResource'
const SAM_LOCAL_PORT_CHECK_RETRY_INTERVAL_MILLIS: number = 125
const SAM_LOCAL_PORT_CHECK_RETRY_TIMEOUT_MILLIS_DEFAULT: number = 30000
const MAX_DEBUGGER_RETRIES_DEFAULT: number = 30
Expand Down Expand Up @@ -81,6 +90,7 @@ export function getRelativeFunctionHandler(params: {
export async function makeInputTemplate(config: SamLaunchRequestArgs): Promise<string> {
let newTemplate: SamTemplateGenerator
let inputTemplatePath: string
const resourceName = makeResourceName(config)

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

newTemplate = new SamTemplateGenerator(template).withTemplateResources({
[config.invokeTarget.logicalId]: resourceWithOverriddenHandler,
[resourceName]: resourceWithOverriddenHandler,
})

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

const localInvokeArgs: SamCliLocalInvokeInvocationArguments = {
templateResourceName:
config.invokeTarget.target === 'code' ? TEMPLATE_RESOURCE_NAME : config.invokeTarget.logicalId,
templateResourceName: makeResourceName(config),
templatePath: config.templatePath,
eventPath: config.eventPayloadFile,
environmentVariablePath: config.envFile,
Expand Down Expand Up @@ -467,7 +476,7 @@ export async function makeConfig(config: SamLaunchRequestArgs): Promise<void> {
config.envFile = path.join(config.baseBuildDir!, 'env-vars.json')

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

// event.json
Expand Down
8 changes: 4 additions & 4 deletions src/test/shared/sam/debugger/samDebugConfigProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ describe('SamDebugConfigurationProvider', async () => {
assertEqualLaunchConfigs(actual, expected, appDir)
assertFileText(
expected.envFile,
'{"awsToolkitSamLocalResource":{"test-js-template-envvar-1":"test target=template envvar value 1","test-js-template-envvar-2":"test target=template envvar value 2"}}'
'{"SourceCodeTwoFoldersDeep":{"test-js-template-envvar-1":"test target=template envvar value 1","test-js-template-envvar-2":"test target=template envvar value 2"}}'
)
assertFileText(
expected.eventPayloadFile,
Expand Down Expand Up @@ -826,7 +826,7 @@ describe('SamDebugConfigurationProvider', async () => {
}

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

assertFileText(
Expand Down Expand Up @@ -1097,7 +1097,7 @@ Outputs:
}

assertEqualLaunchConfigs(actual, expected, appDir)
assertFileText(expected.envFile, '{"awsToolkitSamLocalResource":{}}')
assertFileText(expected.envFile, '{"HelloWorldFunction":{}}')
assertFileText(expected.eventPayloadFile, '{}')

assertFileText(
Expand Down Expand Up @@ -1250,7 +1250,7 @@ Outputs:
}

assertEqualLaunchConfigs(actual, expected, appDir)
assertFileText(expected.envFile, '{"awsToolkitSamLocalResource":{"var1":"2","var2":"1"}}')
assertFileText(expected.envFile, '{"myResource":{"var1":"2","var2":"1"}}')
assertFileText(expected.eventPayloadFile, '{}')

assertFileText(
Expand Down

0 comments on commit 18b0bd8

Please sign in to comment.