-
Notifications
You must be signed in to change notification settings - Fork 51
fix(deploy): auto-populate default target on non-interactive deploy #1478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jariy17
merged 3 commits into
aws:main
from
jariy17:fix/non-interactive-deploy-autopopulate-target
Jun 8, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { type ConfigIO, ConfigNotFoundError } from '../../../../lib'; | ||
| import { ensureDefaultDeploymentTarget } from '../ensure-target.js'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| const { mockDetectAwsContext } = vi.hoisted(() => ({ | ||
| mockDetectAwsContext: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('../../../aws', () => ({ | ||
| detectAwsContext: mockDetectAwsContext, | ||
| })); | ||
|
|
||
| /** Build a fake ConfigIO with stubbed read/write target methods. */ | ||
| function makeConfigIO(opts: { read?: () => Promise<unknown> }): { configIO: ConfigIO; writes: unknown[] } { | ||
| const writes: unknown[] = []; | ||
| const configIO = { | ||
| readAWSDeploymentTargets: opts.read ?? (() => Promise.resolve([])), | ||
| writeAWSDeploymentTargets: (data: unknown) => { | ||
| writes.push(data); | ||
| return Promise.resolve(); | ||
| }, | ||
| } as unknown as ConfigIO; | ||
| return { configIO, writes }; | ||
| } | ||
|
|
||
| describe('ensureDefaultDeploymentTarget', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mockDetectAwsContext.mockResolvedValue({ accountId: '123456789012', region: 'us-east-1', regionSource: 'env' }); | ||
| }); | ||
|
|
||
| it('writes a default target when aws-targets.json is empty', async () => { | ||
| const { configIO, writes } = makeConfigIO({ read: () => Promise.resolve([]) }); | ||
|
|
||
| const wrote = await ensureDefaultDeploymentTarget(configIO); | ||
|
|
||
| expect(wrote).toBe(true); | ||
| expect(writes).toHaveLength(1); | ||
| expect(writes[0]).toEqual([{ name: 'default', account: '123456789012', region: 'us-east-1' }]); | ||
| }); | ||
|
|
||
| it('does not overwrite when a target already exists', async () => { | ||
| const existing = [{ name: 'prod', account: '999888777666', region: 'us-west-2' }]; | ||
| const { configIO, writes } = makeConfigIO({ read: () => Promise.resolve(existing) }); | ||
|
|
||
| const wrote = await ensureDefaultDeploymentTarget(configIO); | ||
|
|
||
| expect(wrote).toBe(false); | ||
| expect(writes).toHaveLength(0); | ||
| expect(mockDetectAwsContext).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('treats a missing targets file (ConfigNotFoundError) as empty and populates it', async () => { | ||
| const { configIO, writes } = makeConfigIO({ | ||
| read: () => Promise.reject(new ConfigNotFoundError('aws-targets.json', 'AWS Targets')), | ||
| }); | ||
|
|
||
| const wrote = await ensureDefaultDeploymentTarget(configIO); | ||
|
|
||
| expect(wrote).toBe(true); | ||
| expect(writes[0]).toEqual([{ name: 'default', account: '123456789012', region: 'us-east-1' }]); | ||
| }); | ||
|
|
||
| it('surfaces a non-not-found read error instead of overwriting the file', async () => { | ||
| const { configIO, writes } = makeConfigIO({ | ||
| read: () => Promise.reject(new Error('Unexpected end of JSON input')), | ||
| }); | ||
|
|
||
| await expect(ensureDefaultDeploymentTarget(configIO)).rejects.toThrow('Unexpected end of JSON input'); | ||
| expect(writes).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('does not write when the AWS account cannot be detected', async () => { | ||
| mockDetectAwsContext.mockResolvedValue({ accountId: null, region: 'us-east-1', regionSource: 'default' }); | ||
| const { configIO, writes } = makeConfigIO({ read: () => Promise.resolve([]) }); | ||
|
|
||
| const wrote = await ensureDefaultDeploymentTarget(configIO); | ||
|
|
||
| expect(wrote).toBe(false); | ||
| expect(writes).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('uses the detected region for the default target', async () => { | ||
| mockDetectAwsContext.mockResolvedValue({ accountId: '123456789012', region: 'eu-west-1', regionSource: 'config' }); | ||
| const { configIO, writes } = makeConfigIO({ read: () => Promise.resolve([]) }); | ||
|
|
||
| await ensureDefaultDeploymentTarget(configIO); | ||
|
|
||
| expect(writes[0]).toEqual([{ name: 'default', account: '123456789012', region: 'eu-west-1' }]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { type ConfigIO, ConfigNotFoundError } from '../../../lib'; | ||
| import { detectAwsContext } from '../../aws'; | ||
|
|
||
| /** | ||
| * Ensure `aws-targets.json` has at least one deployment target. | ||
| * | ||
| * Freshly-created projects (via `agentcore create`, interactive or not) write an | ||
| * empty `aws-targets.json` by design — the target is expected to be populated at | ||
| * deploy time. The interactive deploy flow prompts the user for it, but the | ||
| * non-interactive deploy path (`deploy --yes` / `--json` / `--target`) has no | ||
| * prompt, so it would otherwise fail with `Target "default" not found`. | ||
| * | ||
| * This mirrors the auto-populate behavior already used by `agentcore dev`: if no | ||
| * targets exist, detect the account/region from the environment and write a | ||
| * single `default` target. Best-effort — if the account can't be detected, the | ||
| * file is left as-is and the caller surfaces a clear "target not found" error. | ||
| * | ||
| * A missing `aws-targets.json` is treated as empty. Any other read failure | ||
| * (corrupt JSON, validation error, permissions) is surfaced rather than silently | ||
| * overwriting a file that exists but couldn't be parsed. | ||
| * | ||
| * @returns true if a default target was written, false otherwise. | ||
| */ | ||
| export async function ensureDefaultDeploymentTarget(configIO: ConfigIO): Promise<boolean> { | ||
| let targets; | ||
| try { | ||
| targets = await configIO.readAWSDeploymentTargets(); | ||
| } catch (err) { | ||
| // Only treat a genuinely-missing file as empty; surface real read errors. | ||
| if (!(err instanceof ConfigNotFoundError)) { | ||
| throw err; | ||
| } | ||
| targets = []; | ||
| } | ||
|
|
||
| if (targets.length > 0) { | ||
| return false; | ||
| } | ||
|
|
||
| const ctx = await detectAwsContext(); | ||
| if (!ctx.accountId) { | ||
| return false; | ||
| } | ||
|
|
||
| await configIO.writeAWSDeploymentTargets([{ name: 'default', account: ctx.accountId, region: ctx.region }]); | ||
| return true; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we just return false here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 0a64ed8 — the catch now rethrows non-
ConfigNotFoundErrorerrors and only falls through totargets = []for the missing-file case, so the structure is cleaner.