Skip to content

Modern Docker Management, Designed for Everyone

fix: interpolate variables in Compose include paths (#4298)

Kyle Mendelld9e7eb4d2df3
diff --git a/backend/pkg/projects/custom_labels.go b/backend/pkg/projects/custom_labels.go
index dbd194646..d3d9836ea 100644
--- a/backend/pkg/projects/custom_labels.go
+++ b/backend/pkg/projects/custom_labels.go
@@ -19,7 +19,6 @@ import (
projecttypes "github.com/getarcaneapp/arcane/types/v2/project"
kit "go.getarcane.app/kit/pkg"
updaterlabels "go.getarcane.app/updater/labels"
- "go.yaml.in/yaml/v4"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/iconcatalog"
@@ -144,22 +143,16 @@ func parseArcaneComposeMetadataFromFileInternal(ctx context.Context, composeFile
// it declares, even where the inherited environment overrides those values.
meta.EnvFiles = append([]string{filepath.Join(workdir, EffectiveEnvFileName)}, resolveComposeEnvFilesInternal(workdir, siblingEnv)...)
- includePaths, err := parseIncludePaths(absPath)
+ // Resolve include paths with the environment the loader interpolated, including COMPOSE_ENV_FILES.
+ includes, err := ParseIncludes(absPath, project.Environment, false)
if err != nil {
return meta, err
}
- for _, includePath := range includePaths {
- if includePath == "" {
- continue
- }
- resolvedPath := includePath
- if !filepath.IsAbs(resolvedPath) {
- resolvedPath = filepath.Join(workdir, resolvedPath)
- }
- includedMeta, parseArcaneComposeMetadataFromFileErr := parseArcaneComposeMetadataFromFileInternal(ctx, resolvedPath, mergedEnv, visited)
+ for _, include := range includes {
+ includedMeta, parseArcaneComposeMetadataFromFileErr := parseArcaneComposeMetadataFromFileInternal(ctx, include.Path, mergedEnv, visited)
if parseArcaneComposeMetadataFromFileErr != nil {
- return meta, fmt.Errorf("load included Compose metadata %s: %w", resolvedPath, parseArcaneComposeMetadataFromFileErr)
+ return meta, fmt.Errorf("load included Compose metadata %s: %w", include.Path, parseArcaneComposeMetadataFromFileErr)
}
mergeArcaneComposeMetadata(ctx, &meta, includedMeta)
}
@@ -403,64 +396,6 @@ func mergeEnvFromDotEnv(envMap map[string]string, workdir string) (map[string]st
return merged, fileEnv
}
-func parseIncludePaths(composeFilePath string) ([]string, error) {
- // os.ReadFile rather than acfs: compose files may be symlinks resolving
- // outside any confinement root, including imported projects.
- content, err := os.ReadFile(composeFilePath)
- if err != nil {
- return nil, fmt.Errorf("read compose file: %w", err)
- }
-
- composeData := map[string]any{}
- if unmarshalErr := yaml.Unmarshal(content, &composeData); unmarshalErr != nil {
- return nil, fmt.Errorf("parse compose file: %w", unmarshalErr)
- }
-
- rawIncludes, ok := composeData["include"]
- if !ok {
- return nil, nil
- }
-
- var includeItems []any
- switch v := rawIncludes.(type) {
- case []any:
- includeItems = v
- case []string:
- for _, item := range v {
- includeItems = append(includeItems, item)
- }
- case string:
- includeItems = []any{v}
- default:
- return nil, nil
- }
-
- paths := make([]string, 0, len(includeItems))
- for _, item := range includeItems {
- switch v := item.(type) {
- case string:
- paths = append(paths, v)
- case map[string]any:
- if p, localOk := v["path"]; localOk {
- switch pathValue := p.(type) {
- case string:
- paths = append(paths, pathValue)
- case []any:
- for _, entry := range pathValue {
- if s, localOk2 := entry.(string); localOk2 {
- paths = append(paths, s)
- }
- }
- case []string:
- paths = append(paths, pathValue...)
- }
- }
- }
- }
-
- return paths, nil
-}
-
// FindArcaneIconSet attempts to locate Arcane icon labels within service labels.
// It supports both map[string]string and []string label formats.
func FindArcaneIconSet(labels any) IconSet {
diff --git a/backend/pkg/projects/includes.go b/backend/pkg/projects/includes.go
index 12025497e..8a1b2a993 100644
--- a/backend/pkg/projects/includes.go
+++ b/backend/pkg/projects/includes.go
@@ -9,21 +9,12 @@ import (
"path/filepath"
"strings"
+ "github.com/compose-spec/compose-go/v2/template"
"github.com/samber/mo"
"go.getarcane.app/acfs"
"go.yaml.in/yaml/v4"
)
-// expandEnvVarsInternal expands ${VAR} and $VAR references in a string using the provided env map.
-func expandEnvVarsInternal(s string, envMap EnvMap) string {
- return os.Expand(s, func(key string) string {
- if val, ok := envMap[key]; ok {
- return val
- }
- return ""
- })
-}
-
// Security Model for Include Files:
// - READ: Docker Compose's spec allows include files from anywhere (parent dirs,
// absolute paths). ParseIncludes does NOT enforce containment; it returns whatever
@@ -234,10 +225,15 @@ func resolveIncludeFileInternal(includePath, baseDir string, envMap EnvMap, incl
return IncludeFile{}, errors.New("empty include path")
}
- // Expand environment variables in the include path (e.g., ${PROJECT_STACK_DIR})
- if len(envMap) > 0 {
- includePath = expandEnvVarsInternal(includePath, envMap)
+ // Interpolate with Compose semantics so ${VAR:-default} and ${VAR:?err} resolve.
+ expanded, err := template.SubstituteWithOptions(includePath, func(key string) (string, bool) {
+ value, ok := envMap[key]
+ return value, ok
+ }, template.WithoutLogging)
+ if err != nil {
+ return IncludeFile{}, fmt.Errorf("interpolate include path %s: %w", includePath, err)
}
+ includePath = expanded
fullPath := includePath
if !filepath.IsAbs(includePath) {