Skip to content

Commit 695ad0d

Browse files
authored
fix(sam): "Detect SAM CLI" uses cached sam location #3452
Problem: - Cached sam location is not evicted by the "AWS: Detect SAM CLI" command. - Toolkit logs the sam cli location but not its version, which makes troubleshooting less obvious. #3381 Solution: - Specify `forceSearch` when "AWS: Detect SAM CLI" calls `getOrDetectSamCli()`. - Log the sam version.
1 parent 1764cb2 commit 695ad0d

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

src/shared/sam/cli/samCliDetection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export async function detectSamCli(args: { passive: boolean; showMessage: boolea
3232
await config.delete('location')
3333
}
3434

35-
const sam = await config.getOrDetectSamCli()
35+
const sam = await config.getOrDetectSamCli(true)
3636
const notFound = sam.path === ''
3737

3838
// Update the user setting.

src/shared/sam/cli/samCliInfo.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55

66
import { getLogger, Logger } from '../../logger'
77
import { ChildProcess } from '../../utilities/childProcess'
8-
import { logAndThrowIfUnexpectedExitCode } from './samCliInvokerUtils'
98

10-
/**
11-
* Maps out the response text from the sam cli command `sam --info`
12-
*/
139
export interface SamCliInfoResponse {
1410
version: string
1511
}
@@ -18,13 +14,16 @@ export class SamCliInfoInvocation {
1814
public constructor(private readonly samPath: string) {}
1915

2016
public async execute(): Promise<SamCliInfoResponse> {
21-
const childProcessResult = await new ChildProcess(this.samPath, ['--info'], { logging: 'no' }).run()
17+
const r = await new ChildProcess(this.samPath, ['--info'], { logging: 'no' }).run()
2218

23-
logAndThrowIfUnexpectedExitCode(childProcessResult, 0)
24-
const response = this.convertOutput(childProcessResult.stdout)
19+
if (r.exitCode !== 0) {
20+
// getVersionValidatorResult() will return `SamCliVersionValidation.VersionNotParseable`.
21+
return { version: '' }
22+
}
23+
const response = this.convertOutput(r.stdout)
2524

2625
if (!response) {
27-
throw new Error('SAM CLI did not return expected data')
26+
return { version: '' }
2827
}
2928

3029
return response

src/shared/sam/cli/samCliLocator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { SystemUtilities } from '../../systemUtilities'
1313
import { PerfLog } from '../../logger/logger'
1414

1515
export interface SamCliLocationProvider {
16-
getLocation(): Promise<{ path: string; version: string } | undefined>
16+
getLocation(forceSearch?: boolean): Promise<{ path: string; version: string } | undefined>
1717
}
1818

1919
export class DefaultSamCliLocationProvider implements SamCliLocationProvider {
@@ -29,9 +29,9 @@ export class DefaultSamCliLocationProvider implements SamCliLocationProvider {
2929
* Gets the last-found `sam` location, or searches the system if a working
3030
* `sam` wasn't previously found and cached.
3131
*/
32-
public async getLocation() {
32+
public async getLocation(forceSearch?: boolean) {
3333
const perflog = new PerfLog('samCliLocator: getLocation')
34-
const cachedLoc = DefaultSamCliLocationProvider.cachedSamLocation
34+
const cachedLoc = forceSearch ? undefined : DefaultSamCliLocationProvider.cachedSamLocation
3535

3636
// Avoid searching the system for `sam` (especially slow on Windows).
3737
if (cachedLoc && (await DefaultSamCliLocationProvider.isValidSamLocation(cachedLoc.path))) {
@@ -107,7 +107,7 @@ abstract class BaseSamCliLocator {
107107
return { path: fullPath, version: validationResult.version }
108108
}
109109
this.logger.warn(
110-
`samCliLocator: found invalid SAM CLI: (${validationResult.validation}): ${fullPath}`
110+
`samCliLocator: found invalid SAM CLI (${validationResult.validation}): ${fullPath}`
111111
)
112112
} catch (e) {
113113
const err = e as Error

src/shared/sam/cli/samCliSettings.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,24 @@ export class SamCliSettings extends fromExtensionManifest('aws.samcli', descript
6868

6969
/**
7070
* Gets location of `sam` from:
71-
* 1. user config (if any; overrides auto-detected location)
72-
* 2. previous cached location (if valid)
73-
* 3. searching for `sam` on the system
71+
* 1. user config (if any)
72+
* 2. previous location cached in `getLocation` (if valid)
73+
* 3. system search (done by `getLocation`)
7474
*
7575
* @returns `autoDetected=true` if auto-detection was _attempted_.
7676
*/
77-
public async getOrDetectSamCli(): Promise<{ path: string | undefined; autoDetected: boolean }> {
77+
public async getOrDetectSamCli(
78+
forceSearch?: boolean
79+
): Promise<{ path: string | undefined; autoDetected: boolean }> {
7880
const fromConfig = this.get('location', '')
7981

8082
if (fromConfig) {
81-
SamCliSettings.logIfChanged(`SAM CLI location: ${fromConfig}`)
83+
SamCliSettings.logIfChanged(`SAM CLI location (from settings): ${fromConfig}`)
8284
return { path: fromConfig, autoDetected: false }
8385
}
8486

85-
const fromSearch = await this.locationProvider.getLocation()
86-
SamCliSettings.logIfChanged(`SAM CLI location: ${fromSearch?.path}`)
87+
const fromSearch = await this.locationProvider.getLocation(forceSearch)
88+
SamCliSettings.logIfChanged(`SAM CLI location (version: ${fromSearch?.version}): ${fromSearch?.path}`)
8789
return { path: fromSearch?.path, autoDetected: true }
8890
}
8991

0 commit comments

Comments
 (0)