Skip to content
2 changes: 1 addition & 1 deletion packages/core/src/shared/env/resolveEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export async function getSpawnEnv(
export async function mergeResolvedShellPath(env: IProcessEnvironment): Promise<typeof process.env> {
const shellEnv = await getResolvedShellEnv(env)
// resolve failed or doesn't need to resolve
if (!shellEnv) {
if (!shellEnv || Object.keys(shellEnv).length === 0) {
return env
}
try {
Expand Down
29 changes: 14 additions & 15 deletions packages/core/src/shared/sam/localLambdaRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ async function invokeLambdaHandler(
return config
.samLocalInvokeCommand!.invoke({
options: {
env: await getSpawnEnv({
...process.env,
...env,
}),
env: env,
},
command: samCommand,
args: samArgs,
Expand All @@ -236,7 +233,7 @@ async function invokeLambdaHandler(
templatePath: config.templatePath,
eventPath: config.eventPayloadFile,
environmentVariablePath: config.envFile,
environmentVariables: await getSpawnEnv(env),
environmentVariables: env,
invoker: config.samLocalInvokeCommand!,
dockerNetwork: config.sam?.dockerNetwork,
debugPort: debugPort,
Expand Down Expand Up @@ -290,15 +287,16 @@ export async function runLambdaFunction(
getLogger().info(localize('AWS.output.sam.local.startRun', 'Preparing to run locally: {0}', config.handlerName))
}

const envVars = {
const envVars = await getSpawnEnv({
...process.env,
...(config.aws?.region ? { AWS_DEFAULT_REGION: config.aws.region } : {}),
}
})

const settings = SamCliSettings.instance
const timer = new Timeout(settings.getLocalInvokeTimeout())

// TODO: refactor things to not mutate the config
config.templatePath = await buildLambdaHandler(timer, await getSpawnEnv(envVars), config, settings)
config.templatePath = await buildLambdaHandler(timer, envVars, config, settings)

await onAfterBuild()
timer.refresh()
Expand All @@ -315,12 +313,13 @@ export async function runLambdaFunction(

// SAM CLI and any API requests are executed in parallel
// A failure from either is a failure for the whole invocation
const [process] = await Promise.all([invokeLambdaHandler(timer, envVars, config, settings), apiRequest]).catch(
(err) => {
timer.cancel()
throw err
}
)
const [processInvoker] = await Promise.all([
invokeLambdaHandler(timer, envVars, config, settings),
apiRequest,
]).catch((err) => {
timer.cancel()
throw err
})

if (config.noDebug) {
return config
Expand All @@ -329,7 +328,7 @@ export async function runLambdaFunction(
const terminationListener = vscode.debug.onDidTerminateDebugSession((session) => {
const config = session.configuration as SamLaunchRequestArgs
if (config.invokeTarget?.target === 'api') {
stopApi(process, config)
stopApi(processInvoker, config)
}
})

Expand Down
45 changes: 45 additions & 0 deletions packages/core/src/test/shared/env/resolveEnv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

import assert from 'assert'
import { mergeResolvedShellPath } from '../../../shared/env/resolveEnv'
import sinon from 'sinon'

describe('resolveEnv', async function () {
let sandbox: sinon.SinonSandbox
beforeEach(function () {
sandbox = sinon.createSandbox()
sandbox.stub(process, 'platform').value('win32')
})

afterEach(function () {
sandbox.restore()
})

describe('resolveWindows', async function () {
beforeEach(function () {
sandbox.stub(process, 'platform').value('win32')
})

it('mergeResolvedShellPath should not change path on windows', async function () {
const env = await mergeResolvedShellPath(process.env)
assert(env.PATH)
assert.strictEqual(env, process.env)
})
})

describe('resolveMac', async function () {
beforeEach(function () {
sandbox.stub(process, 'platform').value('darwin')
})

it('mergeResolvedShellPath should get path on mac', async function () {
sandbox.stub(process.env, 'PATH').value('')
const env = await mergeResolvedShellPath(process.env)
assert(env.PATH)
assert.notEqual(env, process.env)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "Bug Fix",
"description": "getSpawnEnv always return correct env on windows"
}
Loading