Skip to content

Commit 40b7c0a

Browse files
Copilotalexec
andauthored
Add file paths to error messages (#173)
* Initial plan * Add file paths to error messages for better debugging Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Alex Collins <alexec@users.noreply.github.com> Co-authored-by: alexec <1142830+alexec@users.noreply.github.com>
1 parent 34747c0 commit 40b7c0a

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

pkg/codingcontext/context.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func (cc *Context) findTask(taskName string) error {
167167
if shouldExpandParams(frontMatter.ExpandParams) {
168168
textContent, err = cc.expandParams(textContent, nil)
169169
if err != nil {
170-
return fmt.Errorf("failed to expand parameters: %w", err)
170+
return fmt.Errorf("failed to expand parameters in task file %s: %w", path, err)
171171
}
172172
}
173173
finalContent.WriteString(textContent)
@@ -231,7 +231,7 @@ func (cc *Context) findCommand(commandName string, params taskparser.Params) (st
231231
if shouldExpandParams(frontMatter.ExpandParams) {
232232
processedContent, err = cc.expandParams(md.Content, params)
233233
if err != nil {
234-
return fmt.Errorf("failed to expand parameters: %w", err)
234+
return fmt.Errorf("failed to expand parameters in command file %s: %w", path, err)
235235
}
236236
} else {
237237
processedContent = md.Content
@@ -475,15 +475,15 @@ func (cc *Context) findExecuteRuleFiles(ctx context.Context, homeDir string) err
475475
var frontmatter markdown.RuleFrontMatter
476476
md, err := markdown.ParseMarkdownFile(path, &frontmatter)
477477
if err != nil {
478-
return fmt.Errorf("failed to parse markdown file: %w", err)
478+
return fmt.Errorf("failed to parse markdown file %s: %w", path, err)
479479
}
480480

481481
// Expand parameters only if expand is not explicitly set to false
482482
var processedContent string
483483
if shouldExpandParams(frontmatter.ExpandParams) {
484484
processedContent, err = cc.expandParams(md.Content, nil)
485485
if err != nil {
486-
return fmt.Errorf("failed to expand parameters: %w", err)
486+
return fmt.Errorf("failed to expand parameters in file %s: %w", path, err)
487487
}
488488
} else {
489489
processedContent = md.Content

pkg/codingcontext/markdown/markdown.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type RuleMarkdown = Markdown[RuleFrontMatter]
2727
func ParseMarkdownFile[T any](path string, frontMatter *T) (Markdown[T], error) {
2828
fh, err := os.Open(path)
2929
if err != nil {
30-
return Markdown[T]{}, fmt.Errorf("failed to open file: %w", err)
30+
return Markdown[T]{}, fmt.Errorf("failed to open file %s: %w", path, err)
3131
}
3232
defer fh.Close()
3333

@@ -68,13 +68,13 @@ func ParseMarkdownFile[T any](path string, frontMatter *T) (Markdown[T], error)
6868
}
6969

7070
if err := s.Err(); err != nil {
71-
return Markdown[T]{}, fmt.Errorf("failed to scan file: %w", err)
71+
return Markdown[T]{}, fmt.Errorf("failed to scan file %s: %w", path, err)
7272
}
7373

7474
// Parse frontmatter if we collected any
7575
if frontMatterBytes.Len() > 0 {
7676
if err := yaml.Unmarshal(frontMatterBytes.Bytes(), frontMatter); err != nil {
77-
return Markdown[T]{}, fmt.Errorf("failed to unmarshal frontmatter: %w", err)
77+
return Markdown[T]{}, fmt.Errorf("failed to unmarshal frontmatter in file %s: %w", path, err)
7878
}
7979
}
8080

pkg/codingcontext/markdown/markdown_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package markdown
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67
"testing"
78
)
89

@@ -101,6 +102,57 @@ func TestParseMarkdownFile_FileNotFound(t *testing.T) {
101102
if err == nil {
102103
t.Error("ParseMarkdownFile() expected error for non-existent file, got nil")
103104
}
105+
// Verify error message includes file path
106+
if err != nil && !strings.Contains(err.Error(), "/nonexistent/file.md") {
107+
t.Errorf("ParseMarkdownFile() error should contain file path, got: %v", err)
108+
}
109+
}
110+
111+
func TestParseMarkdownFile_ErrorsIncludeFilePath(t *testing.T) {
112+
tests := []struct {
113+
name string
114+
content string
115+
want string // substring that should appear in error
116+
}{
117+
{
118+
name: "invalid YAML in frontmatter",
119+
content: `---
120+
invalid: yaml: : syntax
121+
---
122+
Content here`,
123+
want: "failed to unmarshal frontmatter in file",
124+
},
125+
}
126+
127+
for _, tt := range tests {
128+
t.Run(tt.name, func(t *testing.T) {
129+
// Create a temporary file
130+
tmpDir := t.TempDir()
131+
tmpFile := filepath.Join(tmpDir, "test.md")
132+
if err := os.WriteFile(tmpFile, []byte(tt.content), 0o644); err != nil {
133+
t.Fatalf("failed to create temp file: %v", err)
134+
}
135+
136+
// Parse the file
137+
var frontmatter BaseFrontMatter
138+
_, err := ParseMarkdownFile(tmpFile, &frontmatter)
139+
140+
// Check that we got an error
141+
if err == nil {
142+
t.Fatalf("ParseMarkdownFile() expected error for %s, got nil", tt.name)
143+
}
144+
145+
// Check that error contains expected substring
146+
if !strings.Contains(err.Error(), tt.want) {
147+
t.Errorf("ParseMarkdownFile() error should contain %q, got: %v", tt.want, err)
148+
}
149+
150+
// Check that error contains file path
151+
if !strings.Contains(err.Error(), tmpFile) {
152+
t.Errorf("ParseMarkdownFile() error should contain file path %q, got: %v", tmpFile, err)
153+
}
154+
})
155+
}
104156
}
105157

106158
func TestParseMarkdownFile_CustomStruct(t *testing.T) {

0 commit comments

Comments
 (0)