Skip to content

Commit 183e0e4

Browse files
committed
fix(sam): "failed to process" template URI containing ${workspaceFolder}
Problem: Logs show "failed to process" when initializing the "template registry": CloudFormationTemplateRegistry: failed to process: file:///Volumes/workplace/sam-samples/%24%7BworkspaceFolder%7D/lambda-python3.11/template.yaml Solution: Replace the special vscode `${workspaceFolder}` variable just-in-time. P.S.: This is a hack, it should be done earlier in the codepath. Related comment: https://github.com/aws/aws-toolkit-vscode/blob/a4a184c7031341dae04b2d10d645f464080dbebf/src/lambda/vue/configEditor/samInvokeBackend.ts#L331-L335 Followup to: 616835a
1 parent 25cd4d0 commit 183e0e4

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/lambda/commands/createNewSamApp.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { addFolderToWorkspace, tryGetAbsolutePath } from '../../shared/utilities
3232
import { goRuntimes } from '../models/samLambdaRuntime'
3333
import { eventBridgeStarterAppTemplate } from '../models/samTemplates'
3434
import { CreateNewSamAppWizard, CreateNewSamAppWizardForm } from '../wizards/samInitWizard'
35-
import { LaunchConfiguration } from '../../shared/debug/launchConfiguration'
35+
import { LaunchConfiguration, replaceVscodeVars } from '../../shared/debug/launchConfiguration'
3636
import { SamDebugConfigProvider } from '../../shared/sam/debugger/awsSamDebugger'
3737
import { ExtContext } from '../../shared/extensions'
3838
import { isTemplateTargetProperties } from '../../shared/sam/debugger/awsSamDebugConfiguration'
@@ -395,8 +395,7 @@ export async function addInitialLaunchConfiguration(
395395
const targetDir: string = path.dirname(targetUri.fsPath)
396396
const filtered = configurations.filter(config => {
397397
let templatePath: string = (config.invokeTarget as TemplateTargetProperties).templatePath
398-
// TODO: write utility function that does this for other variables too
399-
templatePath = templatePath.replace('${workspaceFolder}', folder.uri.fsPath)
398+
templatePath = replaceVscodeVars(templatePath, folder.uri.fsPath)
400399

401400
return (
402401
isTemplateTargetProperties(config.invokeTarget) &&

src/shared/debug/launchConfiguration.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,13 @@ export async function getReferencedHandlerPaths(launchConfig: LaunchConfiguratio
204204
.thru(array => new Set(array))
205205
.value()
206206
}
207+
208+
/**
209+
* Replaces magic vscode variables in a launch config value.
210+
*/
211+
export function replaceVscodeVars(val: string, workspaceFolder?: string): string {
212+
if (!workspaceFolder) {
213+
return val
214+
}
215+
return val.replace('${workspaceFolder}', workspaceFolder)
216+
}

src/shared/sam/debugger/awsSamDebugConfigurationValidator.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from './awsSamDebugConfiguration'
2121
import { tryGetAbsolutePath } from '../../utilities/workspaceUtils'
2222
import { CloudFormationTemplateRegistry } from '../../fs/templateRegistry'
23+
import { replaceVscodeVars } from '../../debug/launchConfiguration'
2324

2425
export interface ValidationResult {
2526
isValid: boolean
@@ -69,8 +70,13 @@ export class DefaultAwsSamDebugConfigurationValidator implements AwsSamDebugConf
6970
) {
7071
let cfnTemplate: CloudFormation.Template | undefined
7172
if (config.invokeTarget.templatePath) {
72-
const fullpath = tryGetAbsolutePath(this.workspaceFolder, config.invokeTarget.templatePath)
73+
// TODO: why wasn't ${workspaceFolder} resolved before now?
74+
const resolvedPath = replaceVscodeVars(
75+
config.invokeTarget.templatePath,
76+
this.workspaceFolder?.uri.fsPath
77+
)
7378
// Normalize to absolute path for use in the runner.
79+
const fullpath = tryGetAbsolutePath(this.workspaceFolder, resolvedPath)
7480
config.invokeTarget.templatePath = fullpath
7581
// Forcefully add to the registry in case the registry scan somehow missed the file. #2614
7682
// If the user (launch config) gave an explicit path we should always "find" it.

0 commit comments

Comments
 (0)