Skip to content

Commit c53da73

Browse files
leodidoona-agent
andcommitted
fix: resolve builtin variables in PackageInternal (prep, env)
The resolveBuiltinVariables function only resolved builtin variables (${__pkg_version}, ${__git_commit}, ${__git_commit_short}) in Config, but FindUnresolvedArguments checks both PackageInternal and Config. This caused builtin variables in prep or env fields to be flagged as unresolved, failing the build with errors like: cannot build with unresolved argument "${__git_commit_short}" Now resolves builtin variables in PackageInternal before Config. Co-authored-by: Ona <no-reply@ona.com>
1 parent d4094a2 commit c53da73

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

pkg/leeway/package.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,20 @@ func (p *Package) resolveBuiltinVariables() error {
840840
}
841841
}
842842

843+
// Resolve builtin variables in PackageInternal (prep, env, etc.)
844+
pifc, err := yaml.Marshal(p.PackageInternal)
845+
if err != nil {
846+
return err
847+
}
848+
pifc = replaceBuildArguments(pifc, builtinArgs)
849+
var pi PackageInternal
850+
err = yaml.Unmarshal(pifc, &pi)
851+
if err != nil {
852+
return err
853+
}
854+
p.PackageInternal = pi
855+
856+
// Resolve builtin variables in Config
843857
type configOnlyHelper struct {
844858
Config PackageConfig `yaml:"config"`
845859
}

pkg/leeway/package_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,57 @@ func TestResolveBuiltinVariables(t *testing.T) {
172172
}
173173
}
174174

175+
func TestResolveBuiltinVariablesInPackageInternal(t *testing.T) {
176+
tests := []struct {
177+
Name string
178+
Prep [][]string
179+
Env []string
180+
ExpectedPrep [][]string
181+
ExpectedEnv []string
182+
}{
183+
{
184+
Name: "prep with __pkg_version",
185+
Prep: [][]string{{"/bin/bash", "prepare.sh", "${__pkg_version}"}},
186+
ExpectedPrep: [][]string{{"/bin/bash", "prepare.sh", "this-version"}},
187+
},
188+
{
189+
Name: "env with __pkg_version",
190+
Env: []string{"VERSION=${__pkg_version}"},
191+
ExpectedEnv: []string{"VERSION=this-version"},
192+
},
193+
{
194+
Name: "prep and env with __pkg_version",
195+
Prep: [][]string{{"echo", "${__pkg_version}"}},
196+
Env: []string{"BUILD_VERSION=${__pkg_version}"},
197+
ExpectedPrep: [][]string{{"echo", "this-version"}},
198+
ExpectedEnv: []string{"BUILD_VERSION=this-version"},
199+
},
200+
}
201+
202+
for _, test := range tests {
203+
t.Run(test.Name, func(t *testing.T) {
204+
pkg := NewTestPackage("pkg")
205+
pkg.Type = GenericPackage
206+
pkg.Config = GenericPkgConfig{Commands: [][]string{{"echo", "hello"}}}
207+
pkg.PreparationCommands = test.Prep
208+
pkg.Environment = test.Env
209+
210+
err := pkg.resolveBuiltinVariables()
211+
if err != nil {
212+
t.Fatalf("unexpected error: %v", err)
213+
}
214+
215+
if test.ExpectedPrep != nil && !reflect.DeepEqual(pkg.PreparationCommands, test.ExpectedPrep) {
216+
t.Errorf("PreparationCommands mismatch. expected: %v, actual: %v", test.ExpectedPrep, pkg.PreparationCommands)
217+
}
218+
219+
if test.ExpectedEnv != nil && !reflect.DeepEqual(pkg.Environment, test.ExpectedEnv) {
220+
t.Errorf("Environment mismatch. expected: %v, actual: %v", test.ExpectedEnv, pkg.Environment)
221+
}
222+
})
223+
}
224+
}
225+
175226
func TestFindCycles(t *testing.T) {
176227
tests := []struct {
177228
Name string

0 commit comments

Comments
 (0)