Skip to content

Commit 3b45960

Browse files
authored
fix(lambda): save params before running sam command when --watch is selected (#6089)
## Problem When --watch flag is used, the sync process remains alive. The region and stack_name info get written to config file only after the process finishes. This means customer would not be able to see (or refresh) the latest deployed resoures during the sync process. ## Solution Write to samconfig.toml before running sam sync --- <!--- REMINDER: Ensure that your PR meets the guidelines in CONTRIBUTING.md --> License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 61d67f8 commit 3b45960

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

packages/core/src/shared/sam/sync.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,15 @@ export async function runSamSync(args: SyncParams) {
354354
}),
355355
})
356356

357-
await runInTerminal(sam, 'sync')
357+
// with '--watch' selected, the sync process will run in the background until the user manually kills it
358+
// we need to save the stack and region to the samconfig file now, otherwise the user would not see latest deployed resoure during this sync process
358359
const { paramsSource, stackName, region, projectRoot } = args
359360
const shouldWriteSyncSamconfigGlobal = paramsSource !== ParamsSource.SamConfig && !!stackName && !!region
361+
if (boundArgs.includes('--watch')) {
362+
shouldWriteSyncSamconfigGlobal && (await writeSamconfigGlobal(projectRoot, stackName, region))
363+
}
364+
365+
await runInTerminal(sam, 'sync')
360366
shouldWriteSyncSamconfigGlobal && (await writeSamconfigGlobal(projectRoot, stackName, region))
361367
}
362368

packages/core/src/test/shared/sam/sync.test.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,91 @@ describe('SAM runSync', () => {
10301030
prompterTester.assertCallAll()
10311031
})
10321032

1033+
it('[entry: command palette] specify and save flag (with --watch) should save params before starting SAM process', async () => {
1034+
const prompterTester = PrompterTester.init()
1035+
.handleQuickPick('Select a SAM/CloudFormation Template', async (quickPick) => {
1036+
// Need sometime to wait for the template to search for template file
1037+
await quickPick.untilReady()
1038+
assert.strictEqual(quickPick.items[0].label, templateFile.fsPath)
1039+
quickPick.acceptItem(quickPick.items[0])
1040+
})
1041+
.handleQuickPick('Specify parameter source for sync', async (picker) => {
1042+
// Need time to check samconfig.toml file and generate options
1043+
await picker.untilReady()
1044+
assert.strictEqual(picker.items[0].label, 'Specify required parameters and save as defaults')
1045+
picker.acceptItem(picker.items[0])
1046+
})
1047+
.handleQuickPick('Select a region', (quickPick) => {
1048+
const select = quickPick.items.filter((i) => i.detail === 'us-west-2')[0]
1049+
quickPick.acceptItem(select || quickPick.items[0])
1050+
})
1051+
.handleQuickPick('Select a CloudFormation Stack', async (picker) => {
1052+
await picker.untilReady()
1053+
assert.strictEqual(picker.items[2].label, 'stack3')
1054+
picker.acceptItem(picker.items[2])
1055+
})
1056+
.handleQuickPick('Specify S3 bucket for deployment artifacts', async (picker) => {
1057+
await picker.untilReady()
1058+
assert.strictEqual(picker.items.length, 2)
1059+
assert.strictEqual(picker.items[0].label, 'Create a SAM CLI managed S3 bucket')
1060+
picker.acceptItem(picker.items[0])
1061+
})
1062+
.handleQuickPick('Specify parameters for sync', async (picker) => {
1063+
await picker.untilReady()
1064+
assert.strictEqual(picker.items.length, 9)
1065+
const dependencyLayer = picker.items.filter((item) => item.label === 'Dependency layer')[0]
1066+
const useContainer = picker.items.filter((item) => item.label === 'Use container')[0]
1067+
const watch = picker.items.filter((item) => item.label === 'Watch')[0]
1068+
picker.acceptItems(dependencyLayer, useContainer, watch)
1069+
})
1070+
.build()
1071+
1072+
// Invoke sync command from command palette
1073+
await runSync('code', undefined)
1074+
1075+
assert(mockGetSamCliPath.calledOnce)
1076+
assert(mockChildProcessClass.calledOnce)
1077+
assert.deepEqual(mockChildProcessClass.getCall(0).args, [
1078+
'sam-cli-path',
1079+
[
1080+
'sync',
1081+
'--code',
1082+
'--template',
1083+
`${templateFile.fsPath}`,
1084+
'--stack-name',
1085+
'stack3',
1086+
'--region',
1087+
'us-west-2',
1088+
'--no-dependency-layer',
1089+
'--save-params',
1090+
'--dependency-layer',
1091+
'--use-container',
1092+
'--watch',
1093+
],
1094+
{
1095+
spawnOptions: {
1096+
cwd: projectRoot?.fsPath,
1097+
env: {
1098+
AWS_TOOLING_USER_AGENT: 'AWS-Toolkit-For-VSCode/testPluginVersion',
1099+
SAM_CLI_TELEMETRY: '0',
1100+
},
1101+
},
1102+
},
1103+
])
1104+
assert(mockGetSpawnEnv.calledOnce)
1105+
assert(spyRunInterminal.calledOnce)
1106+
assert.deepEqual(spyRunInterminal.getCall(0).args, [mockSamSyncChildProcess, 'sync'])
1107+
assert.strictEqual(spyWriteSamconfigGlobal.callCount, 2)
1108+
assert(spyWriteSamconfigGlobal.calledBefore(spyRunInterminal))
1109+
// Check telementry
1110+
assertTelemetry('sam_sync', { result: 'Succeeded', source: undefined })
1111+
assertTelemetryCurried('sam_sync')({
1112+
syncedResources: 'CodeOnly',
1113+
source: undefined,
1114+
})
1115+
prompterTester.assertCallAll()
1116+
})
1117+
10331118
it('[entry: template file] specify flag should instantiate correct process in terminal', async () => {
10341119
const prompterTester = PrompterTester.init()
10351120
.handleQuickPick('Specify parameter source for sync', async (picker) => {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "appBuilder refresh feature doesnt work during sync --watch"
4+
}

0 commit comments

Comments
 (0)