Skip to content

Commit 86bd986

Browse files
authored
Merge pull request #334 from lets-cli/codex/fix-malformed-mixin-yaml-crash
Fix crash on malformed local mixin YAML
2 parents 473e724 + a5bb0ce commit 86bd986

4 files changed

Lines changed: 40 additions & 1 deletion

File tree

docs/docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ title: Changelog
2323
* `[Fixed]` Resolve `go to definition` from command references such as `ref: build` to the referenced command in `lets self lsp`.
2424
* `[Added]` Load local mixin files into LSP storage and command index so mixin commands are available for navigation.
2525
* `[Changed]` Replace the top-level `--upgrade` flag with the `lets self upgrade` command.
26+
* `[Fixed]` Return a normal parse error when a local mixin contains malformed YAML instead of crashing.
2627

2728
## [0.0.59](https://github.com/lets-cli/lets/releases/tag/v0.0.59)
2829

internal/config/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (c *Config) readMixins(mixins []*Mixin) error {
292292

293293
for _, mixin := range mixins {
294294
if err := c.readMixin(mixin); err != nil {
295-
return fmt.Errorf("failed to read remote mixin config '%s': %w", mixin.Remote.URL, err)
295+
return fmt.Errorf("failed to read mixin config '%s': %w", mixin.Source(), err)
296296
}
297297
}
298298

internal/config/config/mixin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,11 @@ func (m *Mixin) UnmarshalYAML(unmarshal func(any) error) error {
190190
func (m *Mixin) IsRemote() bool {
191191
return m.Remote != nil
192192
}
193+
194+
func (m *Mixin) Source() string {
195+
if m.IsRemote() {
196+
return m.Remote.URL
197+
}
198+
199+
return m.FileName
200+
}

internal/config/load_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package config
22

33
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
47
"testing"
58
)
69

@@ -11,4 +14,31 @@ func TestLoadConfig(t *testing.T) {
1114
t.Errorf("can not load test config: %s", err)
1215
}
1316
})
17+
18+
t.Run("returns error for malformed local mixin", func(t *testing.T) {
19+
tempDir := t.TempDir()
20+
21+
mainConfig := "shell: bash\nmixins: [mixin.yaml]\ncommands:\n ok:\n cmd: echo ok\n"
22+
if err := os.WriteFile(filepath.Join(tempDir, "lets.yaml"), []byte(mainConfig), 0o644); err != nil {
23+
t.Fatalf("write main config: %v", err)
24+
}
25+
26+
mixinConfig := "commands:\n test1:\n xxx\n cmd: echo Test\n"
27+
if err := os.WriteFile(filepath.Join(tempDir, "mixin.yaml"), []byte(mixinConfig), 0o644); err != nil {
28+
t.Fatalf("write mixin config: %v", err)
29+
}
30+
31+
_, err := Load("", tempDir, "0.0.0-test")
32+
if err == nil {
33+
t.Fatal("expected malformed mixin error")
34+
}
35+
36+
if !strings.Contains(err.Error(), "failed to read mixin config 'mixin.yaml'") {
37+
t.Fatalf("unexpected error: %v", err)
38+
}
39+
40+
if !strings.Contains(err.Error(), "can not parse mixin config mixin.yaml") {
41+
t.Fatalf("unexpected error: %v", err)
42+
}
43+
})
1444
}

0 commit comments

Comments
 (0)