fix: body-level {{#import shared/X.md}} rewritten with wrong cross-repo path in gh aw add#23817
fix: body-level {{#import shared/X.md}} rewritten with wrong cross-repo path in gh aw add#23817
Conversation
- Use resolveImportPath in processIncludesWithWorkflowSpec so body-level
{{#import shared/X.md}} paths are resolved relative to the workflow
file's directory (.github/workflows/) rather than the repo root.
Before: owner/repo/shared/config.md@SHA
After: owner/repo/.github/workflows/shared/config.md@SHA
- Add localWorkflowDir parameter to processIncludesWithWorkflowSpec so
imports whose target files already exist locally are preserved as-is,
matching the behaviour already in processIncludesInContent (update cmd).
- Update add_command.go to pass githubWorkflowsDir as localWorkflowDir
so consuming repos with their own local shared/ files are not forced
onto cross-repo references after every gh aw add.
- Add 3 new tests: PathResolution, PreservesLocalIncludes,
RewritesBodyWhenLocalMissing.
Closes #(issue)
Agent-Logs-Url: https://github.com/github/gh-aw/sessions/106643c3-a705-4a25-9619-35c55a98d102
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
|
@copilot review and add tests to try various types of import paths |
Add two table-driven tests covering every meaningful import-path variant:
TestProcessIncludesWithWorkflowSpec_PathTypes (15 sub-tests):
- simple relative (shared/config.md)
- optional flag preserved ({{#import?}})
- deep nested path
- absolute path (/tools/config.md)
- path with section reference (#Section)
- optional + section
- already a workflowspec (passed through)
- already workflowspec with section
- section-only reference (#SectionName)
- parent dir (../shared/config.md)
- current dir (./config.md)
- legacy @include syntax
- legacy optional @include? syntax
- three-segment path (no @ sign, treated as local)
- plain filename
TestProcessImportsWithWorkflowSpec_PathTypes (8 sub-tests):
- same path categories for the frontmatter imports: field
Agent-Logs-Url: https://github.com/github/gh-aw/sessions/47f3fec7-c300-4ecb-9103-6c1d1a96903a
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Added two table-driven tests in commit
|
There was a problem hiding this comment.
Pull request overview
Fixes gh aw add rewriting of body-level {{#import ...}} directives by resolving paths relative to the workflow file location and preserving local imports when the consuming repo already has the referenced files.
Changes:
- Update
processIncludesWithWorkflowSpecto (a) skip already-workflowspec paths, (b) resolve include paths relative toworkflow.WorkflowPath, and (c) preserve local includes whenlocalWorkflowDircontains the target. - Wire
localWorkflowDirthroughadd_command.gosogh aw addpreserves locally-present shared files. - Add regression + table-driven tests for body includes and frontmatter
imports:path variants; add a changeset entry.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
pkg/cli/imports.go |
Fixes path resolution for body imports, adds local-preservation behavior, and avoids rewriting already-workflowspec imports. |
pkg/cli/imports_test.go |
Adds regression and table-driven coverage for import path variants and preservation behavior. |
pkg/cli/add_command.go |
Passes .github/workflows as localWorkflowDir to preserve local body imports during gh aw add. |
.changeset/patch-fix-body-import-path-resolution.md |
Documents the patch-level behavior change for release notes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
pkg/cli/imports.go
Outdated
| // Mark as visited | ||
| visited[filePath] = true | ||
|
|
There was a problem hiding this comment.
Because visited[filePath] = true is set before emitting the rewritten directive, subsequent occurrences of the same include will hit the earlier if visited[filePath] { continue } and be omitted from the output entirely. This changes workflow content by dropping repeated include lines. Consider changing the cycle-detection behavior to still write the original line (or rewritten directive) while only skipping re-enqueueing/nested traversal for already-visited paths.
pkg/cli/imports_test.go
Outdated
| if err := os.MkdirAll(tmpDir+"/shared", 0755); err != nil { | ||
| t.Fatalf("Failed to create shared dir: %v", err) | ||
| } | ||
| if err := os.WriteFile(tmpDir+"/shared/config.md", []byte("# Local config"), 0644); err != nil { | ||
| t.Fatalf("Failed to create local config file: %v", err) | ||
| } |
There was a problem hiding this comment.
In these tests, building paths via string concatenation (e.g. tmpDir+"/shared") can be brittle on Windows. Prefer filepath.Join(tmpDir, "shared") / filepath.Join(tmpDir, "shared", "config.md") and add the path/filepath import to keep the tests OS-agnostic.
|
@copilot review comments |
- Fix cycle-detection in processIncludesWithWorkflowSpec: mark visited and
skip re-enqueueing only on second occurrence, but still write the rewritten
directive so duplicate {{#import}} lines are preserved in the output
- Add TestProcessIncludesWithWorkflowSpec_DuplicateInclude to cover the fix
- Replace tmpDir+"/..." string concatenation in tests with filepath.Join(...)
for OS-agnosticism (addresses code review feedback)
Agent-Logs-Url: https://github.com/github/gh-aw/sessions/f99a3393-1a0c-42bb-acb7-68920cee5eb6
Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Both review comments addressed in commit
|
|
Hey Contribution Guidelines Check — ✅ All clear
This PR looks well-structured and ready for maintainer review. The test coverage across all import path variants (
|
- Replace fileToProcess struct with []string queue in processIncludesWithWorkflowSpec - Extract splitImportPath helper using strings.Cut, eliminating 3 duplicate section-split blocks - Extract writeImportDirective helper, eliminating 2 duplicate directive-write blocks - Merge two adjacent if opts.Verbose blocks in addWorkflowWithTracking No behavioral changes; all tests pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
gh aw addhad two bugs when rewriting body-level{{#import shared/X.md}}directives: it resolved the path from the repo root instead of the workflow file's directory (.github/workflows/), and it unconditionally rewrote imports even when the consuming repo already had the file locally.Changes
processIncludesWithWorkflowSpec— path resolution fix: Replace barefilePathwithresolveImportPath(filePath, workflow.WorkflowPath)when building the cross-repo ref, matching whatprocessIncludesInContent(used byupdate) already does.processIncludesWithWorkflowSpec— local preservation: Add alocalWorkflowDirparameter (same pattern asprocessIncludesInContent). Imports whose targets exist underlocalWorkflowDirare left unchanged rather than pinned to the source repo.add_command.go: PassgithubWorkflowsDiraslocalWorkflowDirso consuming repos with their ownshared/files don't get those imports overwritten on everygh aw add.Tests: Three targeted regression tests (
PathResolution,PreservesLocalIncludes,RewritesBodyWhenLocalMissing) plus two comprehensive table-driven tests covering every import path variant:TestProcessIncludesWithWorkflowSpec_PathTypes(15 sub-tests): simple relative, optional flag, deep nested, absolute (/tools/…), path+section, already a workflowspec (pass-through), section-only, parent dir (../), current dir (./), legacy@includesyntax, three-segment path, and plain filename.TestProcessImportsWithWorkflowSpec_PathTypes(8 sub-tests): same path categories for the frontmatterimports:field processor.