Skip to content

Commit 9466f98

Browse files
authored
fix(lambda): redirect view logs action to terminal (#6103)
## Problem When a build error occurs, the error details are correctly shown under the "Terminal" tab. However, the toast notification says "build failed" with a button to "See logs." Clicking this button incorrectly redirects the user to the "Output" tab, which only shows a generic message (Error: sam build exited with a non-zero exit code: 1 [NonZeroExitCode]), instead of the detailed logs from the "Terminal" tab. This creates confusion and makes troubleshooting more difficult. ## Solution Redirect view logs action to open the terminal instead of output channel --- <!--- 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 3b45960 commit 9466f98

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ import globals from '../extensionGlobals'
1919
import { TreeNode } from '../treeview/resourceTreeDataProvider'
2020
import { telemetry } from '../telemetry/telemetry'
2121
import { getSpawnEnv } from '../env/resolveEnv'
22-
import { getErrorCode, getProjectRoot, getSamCliPathAndVersion, isDotnetRuntime, updateRecentResponse } from './utils'
22+
import {
23+
getErrorCode,
24+
getProjectRoot,
25+
getSamCliPathAndVersion,
26+
getTerminalFromError,
27+
isDotnetRuntime,
28+
updateRecentResponse,
29+
} from './utils'
2330
import { getConfigFileUri, validateSamBuildConfig } from './config'
2431
import { runInTerminal } from './processTerminal'
2532

@@ -240,7 +247,7 @@ export async function runBuild(arg?: TreeNode): Promise<SamBuildResult> {
240247
}
241248
} catch (error) {
242249
throw ToolkitError.chain(error, 'Failed to build SAM template', {
243-
details: { ...resolveBuildArgConflict(buildFlags) },
250+
details: { terminal: getTerminalFromError(error), ...resolveBuildArgConflict(buildFlags) },
244251
code: getErrorCode(error),
245252
})
246253
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ import { throwIfErrorMatches } from './utils'
1616

1717
let oldTerminal: ProcessTerminal | undefined
1818
export async function runInTerminal(proc: ChildProcess, cmd: string) {
19-
const handleResult = (result?: ChildProcessResult) => {
19+
const handleResult = (result?: ChildProcessResult, terminal?: vscode.Terminal) => {
2020
if (result && result.exitCode !== 0) {
21-
throwIfErrorMatches(result)
21+
throwIfErrorMatches(result, terminal)
2222

2323
// If no specific error matched, throw the default non-zero exit code error.
2424
const defaultMessage = `sam ${cmd} exited with a non-zero exit code: ${result.exitCode}`
2525
throw ToolkitError.chain(result.error, defaultMessage, {
2626
code: 'NonZeroExitCode',
27+
details: { terminal: terminal },
2728
})
2829
}
2930
}
@@ -56,7 +57,7 @@ export async function runInTerminal(proc: ChildProcess, cmd: string) {
5657
? ToolkitError.chain(result.error, 'SAM CLI was cancelled before exiting', { cancelled: true })
5758
: new CancellationError('user')
5859
} else {
59-
return handleResult(result)
60+
return handleResult(result, terminal)
6061
}
6162
}
6263

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,23 @@ export function getErrorCode(error: unknown): string | undefined {
121121
return error instanceof ToolkitError ? error.code : undefined
122122
}
123123

124-
export function throwIfErrorMatches(result: ChildProcessResult) {
124+
export function throwIfErrorMatches(result: ChildProcessResult, terminal?: vscode.Terminal) {
125125
const errorMessage = getSamCliErrorMessage(result.stderr)
126126
for (const errorType in SamCliErrorTypes) {
127127
if (errorMessage.includes(SamCliErrorTypes[errorType as keyof typeof SamCliErrorTypes])) {
128128
throw ToolkitError.chain(result.error, errorMessage, {
129129
code: errorType,
130+
details: { terminal: terminal },
130131
})
131132
}
132133
}
133134
}
134135

136+
export function getTerminalFromError(error: any): vscode.Terminal {
137+
return error.details?.['terminal'] as unknown as vscode.Terminal
138+
//return vscode.window.activeTerminal as vscode.Terminal
139+
}
140+
135141
export enum SamCliErrorTypes {
136142
DockerUnreachable = 'Docker is unreachable.',
137143
ResolveS3AndS3Set = 'Cannot use both --resolve-s3 and --s3-bucket parameters in non-guided deployments.',

packages/core/src/shared/utilities/logAndShowUtils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,18 @@ export async function logAndShowError(
3232
return
3333
}
3434
const logsItem = localize('AWS.generic.message.viewLogs', 'View Logs...')
35+
const viewInTerminalItem = localize('AWS.generic.message.viewInTerminal', 'View Logs In Terminal')
3536
const logId = getLogger().error(`${topic}: %s`, error)
3637
const message = resolveErrorMessageToDisplay(error, defaultMessage)
3738

3839
if (error instanceof ToolkitError && error.documentationUri) {
3940
await showMessageWithUrl(message, error.documentationUri, 'View Documentation', 'error')
41+
} else if (error instanceof ToolkitError && (error.details?.['terminal'] as unknown as vscode.Terminal)) {
42+
await vscode.window.showErrorMessage(message, viewInTerminalItem).then(async (resp) => {
43+
if (resp === viewInTerminalItem) {
44+
;(error.details?.['terminal'] as unknown as vscode.Terminal).show()
45+
}
46+
})
4047
} else {
4148
await vscode.window.showErrorMessage(message, logsItem).then(async (resp) => {
4249
if (resp === logsItem) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ describe('SAM runBuild', () => {
515515
}
516516
})
517517

518-
it('should throw ToolkitError when sync command fail', async () => {
518+
it('should throw ToolkitError when build command fail', async () => {
519519
const prompterTester = PrompterTester.init()
520520
.handleQuickPick('Specify parameter source for build', async (quickPick) => {
521521
await quickPick.untilReady()
@@ -532,7 +532,7 @@ describe('SAM runBuild', () => {
532532
value: sandbox.stub().resolves({
533533
exitCode: -1,
534534
stdout: 'Mock build command execution failure',
535-
stderr: '',
535+
stderr: 'Docker is unreachable.',
536536
}),
537537
},
538538
})
@@ -544,6 +544,8 @@ describe('SAM runBuild', () => {
544544
} catch (error: any) {
545545
assert(error instanceof ToolkitError)
546546
assert.strictEqual(error.message, 'Failed to build SAM template')
547+
assert(error.details?.['terminal'] as unknown as vscode.Terminal)
548+
assert.strictEqual((error.details?.['terminal'] as unknown as vscode.Terminal).name, 'SAM build')
547549
}
548550
prompterTester.assertCallAll()
549551
})
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": "'View Logs...' action redirects to output channel instead of Terminal"
4+
}

0 commit comments

Comments
 (0)