Skip to content

Commit 72d4554

Browse files
committed
fix: correctly modify volumes when normalizing services in a subdir
This fixes relative paths when using `extends` to include another service from a different directory Signed-off-by: Dominik Süß <dominik@suess.wtf>
1 parent ac5150c commit 72d4554

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

podman_compose.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ def is_list(list_object: Any) -> bool:
5454
)
5555

5656

57+
def is_relative_ref(path: str) -> bool:
58+
return (
59+
path.startswith("./")
60+
or path.startswith(".:")
61+
or path.startswith("../")
62+
or path.startswith("..:")
63+
)
64+
65+
5766
# identity filter
5867
def filteri(a: list[str]) -> list[str]:
5968
return list(filter(lambda i: i, a))
@@ -1810,6 +1819,19 @@ def normalize_service(service: dict[str, Any], sub_dir: str = "") -> dict[str, A
18101819
for k, v in deps.items():
18111820
v.setdefault('condition', 'service_started')
18121821
service["depends_on"] = deps
1822+
if "volumes" in service and sub_dir:
1823+
new_volumes = []
1824+
for v in service["volumes"]:
1825+
if isinstance(v, str):
1826+
if is_relative_ref(v):
1827+
v = os.path.join(sub_dir, v)
1828+
elif isinstance(v, dict):
1829+
source = v["source"]
1830+
if is_relative_ref(source):
1831+
v["source"] = os.path.join(sub_dir, source)
1832+
1833+
new_volumes.append(v)
1834+
service["volumes"] = new_volumes
18131835
return service
18141836

18151837

tests/unit/test_normalize_service.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,37 @@ def test_simple(self, input: dict[str, Any], expected: dict[str, Any]) -> None:
4747
{"build": {"context": "./dir-1", "dockerfile": "dockerfile-1"}},
4848
{"build": {"context": "./sub_dir/dir-1", "dockerfile": "dockerfile-1"}},
4949
),
50+
(
51+
{"volumes": ["./nested/relative:/mnt", "../dir-in-parent:/mnt", "..:/mnt", ".:/mnt"]},
52+
{
53+
"volumes": [
54+
"./sub_dir/./nested/relative:/mnt",
55+
"./sub_dir/../dir-in-parent:/mnt",
56+
"./sub_dir/..:/mnt",
57+
"./sub_dir/.:/mnt",
58+
]
59+
},
60+
),
61+
(
62+
{
63+
"volumes": [
64+
{
65+
"type": "bind",
66+
"source": "./nested/relative",
67+
"target": "/mnt",
68+
}
69+
]
70+
},
71+
{
72+
"volumes": [
73+
{
74+
"type": "bind",
75+
"source": "./sub_dir/./nested/relative",
76+
"target": "/mnt",
77+
}
78+
]
79+
},
80+
),
5081
])
5182
def test_normalize_service_with_sub_dir(
5283
self, input: dict[str, Any], expected: dict[str, Any]

0 commit comments

Comments
 (0)