From 3996e832ddfb31cd0226e161081187d18644ac63 Mon Sep 17 00:00:00 2001 From: Archimedes Trajano Date: Thu, 27 Nov 2025 20:05:46 -0500 Subject: [PATCH] Implement lookupHomeFolder for localHomeFolder case Add lookupHomeFolder function to retrieve home directory. https://github.com/devcontainers/spec/issues/335 --- src/spec-common/variableSubstitution.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/spec-common/variableSubstitution.ts b/src/spec-common/variableSubstitution.ts index d973f0cd6..272e5c4a0 100644 --- a/src/spec-common/variableSubstitution.ts +++ b/src/spec-common/variableSubstitution.ts @@ -97,6 +97,9 @@ function replaceWithContext(isWindows: boolean, context: SubstitutionContext, ma case 'localEnv': return lookupValue(isWindows, context.env, args, match, context.configFile); + case 'localHomeFolder': + return lookupHomeFolder(isWindows, context.env, args, match, context.configFile); + case 'localWorkspaceFolder': return context.localWorkspaceFolder !== undefined ? context.localWorkspaceFolder : match; @@ -134,6 +137,26 @@ function replaceDevContainerId(getDevContainerId: () => string | undefined, matc } } +function lookupHomeFolder(isWindows: boolean, envObj: NodeJS.ProcessEnv, args: string[], match: string, configFile: URI | undefined) { + if (args.length != 0) { + let envVariableName = "HOME"; + if (isWindows) { + envVariableName = "userprofile"; + } + const env = envObj[envVariableName]; + if (typeof env === 'string') { + return env; + } + + // For `env` we should do the same as a normal shell does - evaluates missing envs to an empty string #46436 + return ''; + } + throw new ContainerError({ + description: `'${match}'${configFile ? ` in ${path.posix.basename(configFile.path)}` : ''} localHomeFolder cannot have any arguments.` + }); +} + + function lookupValue(isWindows: boolean, envObj: NodeJS.ProcessEnv, args: string[], match: string, configFile: URI | undefined) { if (args.length > 0) { let envVariableName = args[0]; @@ -168,4 +191,4 @@ function devcontainerIdForLabels(idLabels: Record): string { .toString(32) .padStart(52, '0'); return uniqueId; -} \ No newline at end of file +}