Skip to content

Modern Docker Management, Designed for Everyone

fix: gitops sync failing with 'invalid hostPort: /placeholder-undefined' for compose files using variable defaults (#3198)

Kyle Mendell5e72a8901e44
diff --git a/backend/pkg/projects/load.go b/backend/pkg/projects/load.go
index 36daf6db0..71311ae8c 100644
--- a/backend/pkg/projects/load.go
+++ b/backend/pkg/projects/load.go
@@ -2,6 +2,7 @@ package projects
import (
"context"
+ "errors"
"fmt"
"log/slog"
"maps"
@@ -208,12 +209,20 @@ func lenientCastSizeInternal(value string) (any, error) {
return int(n), nil
}
+const lenientUndefinedPlaceholder = "/placeholder-undefined"
+
// ApplyLenientLoaderOptions configures a compose loader.Options for tolerant
// loading: structural validation and consistency checks are skipped, and
-// undefined ${VAR} references are substituted with a placeholder instead of
+// unresolvable ${VAR} references are substituted with a placeholder instead of
// an empty string (which would otherwise produce invalid volume/bind specs
// like ":/path"). Callers use this when a .env file may not yet exist or may
// not define every variable the compose file references.
+//
+// The placeholder is applied at the substitution level, not the lookup level:
+// the lookup must keep reporting undefined variables as missing so compose-go
+// resolves default operators (${VAR:-default}, ${VAR-default}) normally.
+// Reporting the placeholder as the variable's value would suppress those
+// defaults and feed the placeholder into fields like ports host entries.
func ApplyLenientLoaderOptions(ctx context.Context, opts *loader.Options, composeFile string) {
opts.SkipValidation = true
opts.SkipConsistencyCheck = true
@@ -222,16 +231,28 @@ func ApplyLenientLoaderOptions(ctx context.Context, opts *loader.Options, compos
return
}
- realLookup := opts.Interpolate.LookupValue
opts.Interpolate = &interp.Options{
- Substitute: template.Substitute,
+ LookupValue: opts.Interpolate.LookupValue,
TypeCastMapping: wrapTypeCastMappingLenientInternal(opts.Interpolate.TypeCastMapping),
- LookupValue: func(key string) (string, bool) {
- if val, ok := realLookup(key); ok {
- return val, true
- }
- slog.DebugContext(ctx, "compose variable undefined during lenient load, using placeholder", "variable", key, "compose_file", composeFile)
- return "/placeholder-undefined", true
+ Substitute: func(tmpl string, mapping template.Mapping) (string, error) {
+ return template.SubstituteWithOptions(tmpl, mapping,
+ template.WithoutLogging,
+ template.WithReplacementFunction(func(substring string, mapping template.Mapping, cfg *template.Config) (string, error) {
+ value, applied, err := template.DefaultReplacementAppliedFunc(substring, mapping, cfg)
+ var missingErr *template.MissingRequiredError
+ switch {
+ case err == nil && applied:
+ return value, nil
+ case err != nil && !errors.As(err, &missingErr):
+ return "", err
+ default:
+ // Variable is unset with no default (or a ${VAR:?msg}
+ // required variable, which lenient loading tolerates).
+ slog.DebugContext(ctx, "compose variable undefined during lenient load, using placeholder", "expression", substring, "compose_file", composeFile)
+ return lenientUndefinedPlaceholder, nil
+ }
+ }),
+ )
},
}
}
diff --git a/backend/pkg/projects/load_test.go b/backend/pkg/projects/load_test.go
index f0c779be2..da2475514 100644
--- a/backend/pkg/projects/load_test.go
+++ b/backend/pkg/projects/load_test.go
@@ -308,6 +308,36 @@ func TestLoadComposeProjectLenient_ToleratesUndefinedTypedFieldVariables(t *test
assert.Len(t, project.Services, 1)
}
+func TestLoadComposeProjectLenient_AppliesVariableDefaults(t *testing.T) {
+ t.Parallel()
+
+ // The lenient loader used to report the placeholder as every undefined
+ // variable's value, which suppressed
+ // ${VAR:-default} resolution and fed "/placeholder-undefined" into the
+ // ports host entry ("invalid hostPort"). Defaults must resolve normally;
+ // only variables with no default fall back to the placeholder.
+ dir := t.TempDir()
+ composePath := filepath.Join(dir, "compose.yaml")
+ require.NoError(t, os.WriteFile(composePath, []byte(`services:
+ app:
+ image: nginx:alpine
+ ports:
+ - "${ARCANE_TEST_UNSET_PORT:-3169}:3000"
+ environment:
+ - MAX_FILE_SIZE=${ARCANE_TEST_UNSET_SIZE:-104857600}
+`), 0o600))
+
+ project, err := LoadComposeProjectLenient(context.Background(), composePath, "demo", dir, false, nil)
+ require.NoError(t, err)
+ require.NotNil(t, project)
+ require.Len(t, project.Services, 1)
+ svc := project.Services["app"]
+ require.Len(t, svc.Ports, 1)
+ assert.Equal(t, "3169", svc.Ports[0].Published)
+ require.NotNil(t, svc.Environment["MAX_FILE_SIZE"])
+ assert.Equal(t, "104857600", *svc.Environment["MAX_FILE_SIZE"])
+}
+
func TestLoadComposeProject_UsesProjectLevelComposeLabelsForIncludedServices(t *testing.T) {
t.Parallel()