Skip to content

[azure.ai.agents] Fix init from manifest in CWD: allow copy when target is subpath#8522

Merged
trangevi merged 2 commits into
mainfrom
fix/init-manifest-subpath-copy
Jun 2, 2026
Merged

[azure.ai.agents] Fix init from manifest in CWD: allow copy when target is subpath#8522
trangevi merged 2 commits into
mainfrom
fix/init-manifest-subpath-copy

Conversation

@v1212
Copy link
Copy Markdown
Collaborator

@v1212 v1212 commented Jun 2, 2026

Fixes #8523

Problem

When running azd ai agent init from a directory that contains an agent.manifest.yaml but no azure.yaml in the directory tree (e.g. inside a sample repo like foundry-samples/.../01-basic/), the from-manifest flow fails with:

cannot copy agent files: target is inside the manifest directory

The auto-detect flow finds the manifest, guides the user through setup, then ensureProject creates a project subdirectory under CWD. The subsequent copy step detects that the target is inside the manifest directory and rejects.

azd ai agent init - All Cases

# CWD State Manifest azure.yaml Path Taken ensureProject Behavior Result
1 Empty dir None None Template (user picks) azd init -t template creates subdirectory OK
2 Has code None None From-code scaffoldTemplate downloads azure.yaml + infra into CWD (no subdir) OK
3 Has code Yes Yes (parent) From-manifest Finds existing project, no subdir, no copy needed OK
4 Has code Yes None From-manifest azd init -t starter creates subdirectory then copy BUG
5 Has code None Yes (parent) From-code Finds existing project, generates agent.yaml in-place OK
6 Empty dir None Yes Template adds to existing project N/A OK
7 Has code Yes (via -m url) None From-manifest (remote) Downloads to temp, no subpath issue OK
8 Has code Yes (via -m ../path) None From-manifest (external) manifestDir separate from project dir OK

Only Case 4 triggers the bug - manifest auto-detected in CWD + no azure.yaml = ensureProject creates subdirectory under manifestDir.

Root Cause

The from-manifest ensureProject uses azd init -t template subdir which creates a new directory. The from-code ensureProject uses scaffoldTemplate which downloads only infra/ + azure.yaml into CWD without a subdirectory. When the manifest is auto-detected in CWD, the from-manifest path creates the project as a child of the manifest directory, making copy impossible without recursion.

Fix

  1. validateLocalContainerAgentCopy: Allow the subpath case (return nil instead of error)
  2. copyDirectory: When dst is inside src, skip the destination directory during filepath.WalkDir traversal using filepath.SkipDir, preventing infinite recursion while copying all other source files

Testing

  • Renamed TestCopyDirectory_RefusesToCopyIntoSubtree to TestCopyDirectory_CopiesWithSubpathExclusion
  • Added TestValidateLocalContainerAgentCopy_AllowsSubpath
  • Updated init_copy_test.go subpath test to verify new behavior
  • All existing tests pass

Repro Steps

git clone https://github.com/azure-samples/foundry-samples
cd foundry-samples/samples/python/hosted-agents/agent-framework/responses/01-basic/
azd ai agent init
# Select 'Yes' when prompted about detected manifest
# Enter agent name
# Error: cannot copy agent files: target is inside the manifest directory

Workaround (older versions)

cd ..
azd ai agent init -m ./01-basic/agent.manifest.yaml

@github-actions github-actions Bot added the ext-agents azure.ai.{agents,connections,inspector,projects,routines,skills,toolboxes} extensions label Jun 2, 2026
@v1212 v1212 force-pushed the fix/init-manifest-subpath-copy branch from 62671b1 to 6a768b3 Compare June 2, 2026 08:57
@v1212 v1212 marked this pull request as ready for review June 2, 2026 08:59
Copilot AI review requested due to automatic review settings June 2, 2026 08:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a bug where azd ai agent init failed with "target is inside the manifest directory" when run in a directory that contains an agent.manifest.yaml but no azure.yaml. In that flow, ensureProject creates a project subdirectory under CWD (the manifest dir), and the previous validation/copy logic refused to copy a directory into its own subtree. The fix permits this case and prevents infinite recursion by skipping the destination subtree during the walk.

Changes:

  • validateLocalContainerAgentCopy now returns nil (instead of an error) when the target is a subpath of the manifest directory.
  • copyDirectory allows dst-inside-src and uses filepath.SkipDir to avoid recursing into the destination.
  • Renamed/expanded existing test and added TestValidateLocalContainerAgentCopy_AllowsSubpath.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
cli/azd/extensions/azure.ai.agents/internal/cmd/init_copy.go Allow subpath copy and skip destination directory during walk
cli/azd/extensions/azure.ai.agents/internal/cmd/init_test.go Update copy test to verify subpath exclusion behavior; add validation test for subpath case

@v1212 v1212 added the bug Something isn't working label Jun 2, 2026
@v1212 v1212 force-pushed the fix/init-manifest-subpath-copy branch from 6a768b3 to 5bbe1af Compare June 2, 2026 09:04
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jun 2, 2026

📋 Prioritization Note

Thanks for the contribution! The linked issue isn't in the current milestone yet.
Review may take a bit longer — reach out to @rajeshkamal5050 or @kristenwomack if you'd like to discuss prioritization.

@v1212 v1212 force-pushed the fix/init-manifest-subpath-copy branch 3 times, most recently from 93b20f3 to bd71e65 Compare June 2, 2026 11:13
…et is subpath of manifest dir

When running `azd ai agent init` from a directory containing an
agent.manifest.yaml, the auto-detect flow creates a project subdirectory
via ensureProject. The subsequent copy step then finds that the target is
inside the manifest directory and refuses to proceed.

Fix: instead of rejecting the copy outright, skip the destination
directory during WalkDir traversal to prevent infinite recursion while
still copying the agent source files into the new project structure.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@v1212 v1212 force-pushed the fix/init-manifest-subpath-copy branch from bd71e65 to 3e47a35 Compare June 2, 2026 11:17
@banrahan
Copy link
Copy Markdown
Contributor

banrahan commented Jun 2, 2026

I changed how this was implemented, I undid the changes to init_copy, it was re-creating everything as a sub-dir. Instead I added a check for a local manifest in init.go and refactored the isLocalFilePath so I could use it at this point.

@banrahan banrahan force-pushed the fix/init-manifest-subpath-copy branch from 5356335 to 1dcd314 Compare June 2, 2026 22:16
@trangevi trangevi enabled auto-merge (squash) June 2, 2026 22:30
@trangevi trangevi merged commit 220d15f into main Jun 2, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ext-agents azure.ai.{agents,connections,inspector,projects,routines,skills,toolboxes} extensions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[azure.ai.agents] azd ai agent init fails with 'cannot copy agent files: target is inside the manifest directory' when run from sample directory

4 participants