Skip to content

Commit 84df208

Browse files
authored
Merge pull request #388 from milas/name-type-panic
2 parents 7bd0f24 + 5dcf485 commit 84df208

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

loader/loader.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ func InvalidProjectNameErr(v string) error {
271271
)
272272
}
273273

274+
// projectName determines the canonical name to use for the project considering
275+
// the loader Options as well as `name` fields in Compose YAML fields (which
276+
// also support interpolation).
277+
//
278+
// TODO(milas): restructure loading so that we don't need to re-parse the YAML
279+
// here, as it's both wasteful and makes this code error-prone.
274280
func projectName(details types.ConfigDetails, opts *Options) (string, error) {
275281
projectName, projectNameImperativelySet := opts.GetProjectName()
276282

@@ -281,10 +287,22 @@ func projectName(details types.ConfigDetails, opts *Options) (string, error) {
281287
for _, configFile := range details.ConfigFiles {
282288
yml, err := ParseYAML(configFile.Content)
283289
if err != nil {
290+
// HACK: the way that loading is currently structured, this is
291+
// a duplicative parse just for the `name`. if it fails, we
292+
// give up but don't return the error, knowing that it'll get
293+
// caught downstream for us
284294
return "", nil
285295
}
286296
if val, ok := yml["name"]; ok && val != "" {
287-
pjNameFromConfigFile = yml["name"].(string)
297+
sVal, ok := val.(string)
298+
if !ok {
299+
// HACK: see above - this is a temporary parsed version
300+
// that hasn't been schema-validated, but we don't want
301+
// to be the ones to actually report that, so give up,
302+
// knowing that it'll get caught downstream for us
303+
return "", nil
304+
}
305+
pjNameFromConfigFile = sVal
288306
}
289307
}
290308
if !opts.SkipInterpolation {

loader/loader_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,3 +2348,9 @@ func TestDeviceWriteBps(t *testing.T) {
23482348
})
23492349

23502350
}
2351+
2352+
func TestInvalidProjectNameType(t *testing.T) {
2353+
p, err := loadYAML(`name: 123`)
2354+
assert.Error(t, err, "name must be a string")
2355+
assert.Assert(t, is.Nil(p))
2356+
}

0 commit comments

Comments
 (0)