volumes: error when using relative volumes sources in compose (#353)

Signed-off-by: Miek Gieben <miek@miek.nl>
Co-authored-by: Pasha Sviderski <me@psviderski.name>
This commit is contained in:
Miek Gieben
2026-06-17 13:54:43 +10:00
committed by GitHub
co-authored by Pasha Sviderski
parent 73d515bb40
commit cffa03007b
2 changed files with 48 additions and 1 deletions
+18
View File
@@ -22,6 +22,7 @@ var registerComposeOverrides sync.Once
func LoadProject(ctx context.Context, paths []string, opts ...composecli.ProjectOptionsFn) (*types.Project, error) {
registerComposeOverrides.Do(func() {
transform.RegisterDefaultValue("services.*.deploy.update_config", setUpdateConfigDefaults)
transform.RegisterDefaultValue("services.*.volumes.*.source", checkRelativeVolumeMount)
})
defaultOpts := []composecli.ProjectOptionsFn{
@@ -119,3 +120,20 @@ func setUpdateConfigDefaults(data any, _ tree.Path, _ bool) (any, error) {
}
return data, nil
}
// checkRelativeVolumeMount check if a volume mount uses a relative path.
func checkRelativeVolumeMount(data any, _ tree.Path, _ bool) (any, error) {
source, ok := data.(string)
if !ok || filepath.IsAbs(source) {
return data, nil
}
// Only check actual paths, not volumes _names_
if strings.HasPrefix(source, ".") || strings.HasPrefix(source, "~") {
// uc run also warns against this
return nil, fmt.Errorf("invalid volume mount: bind mount source '%s' is relative. If you intended to pass a host "+
"directory or file, use absolute path, or configs might be better suited for this use case. "+
"See https://uncloud.run/docs/concepts/configs", source)
}
return data, nil
}