@@ -31,6 +31,19 @@ use docker_compose_types::{
3131use eyre:: Result ;
3232use indexmap:: IndexMap ;
3333
34+ /// A host path as a compose bind-mount source. Compose reads a source with no
35+ /// `/` or `./` prefix as a NAMED VOLUME, so a relative path needs the prefix;
36+ /// an absolute one must be passed through, or it is resolved against the
37+ /// project directory and docker creates a root-owned directory there.
38+ fn compose_bind_source ( path : & Path ) -> String {
39+ let source = path. display ( ) . to_string ( ) ;
40+ if path. is_absolute ( ) || source. starts_with ( "./" ) || source. starts_with ( "../" ) {
41+ source
42+ } else {
43+ format ! ( "./{source}" )
44+ }
45+ }
46+
3447/// Name of the docker compose file
3548pub const CB_COMPOSE_FILE : & str = "cb.docker-compose.yml" ;
3649/// Name of the envs file
@@ -91,8 +104,8 @@ pub async fn handle_docker_init(config_path: PathBuf, output_dir: PathBuf) -> Re
91104 let mut service_config = ServiceCreationInfo {
92105 config_info : CommitBoostConfigInfo {
93106 config_volume : Volumes :: Simple ( format ! (
94- "./ {}:{}:ro" ,
95- config_path . display ( ) ,
107+ "{}:{}:ro" ,
108+ compose_bind_source ( & config_path ) ,
96109 CONFIG_DEFAULT
97110 ) ) ,
98111 cb_config : CommitBoostConfig :: from_file ( & config_path) ?,
@@ -954,6 +967,18 @@ mod tests {
954967
955968 // --- get_env_val ---
956969
970+ /// Compose reads a bare source as a named volume, so a relative path keeps
971+ /// the `./` prefix; an absolute one must NOT get it, or compose resolves
972+ /// `.//abs/path` against the project directory and docker creates a
973+ /// root-owned directory there instead of mounting the file.
974+ #[ test]
975+ fn test_compose_bind_source_prefixes_only_relative_paths ( ) {
976+ assert_eq ! ( compose_bind_source( Path :: new( "cb-config.toml" ) ) , "./cb-config.toml" ) ;
977+ assert_eq ! ( compose_bind_source( Path :: new( "./cb-config.toml" ) ) , "./cb-config.toml" ) ;
978+ assert_eq ! ( compose_bind_source( Path :: new( "../cb-config.toml" ) ) , "../cb-config.toml" ) ;
979+ assert_eq ! ( compose_bind_source( Path :: new( "/etc/cb/config.toml" ) ) , "/etc/cb/config.toml" ) ;
980+ }
981+
957982 #[ test]
958983 fn test_get_env_val_returns_string_pair ( ) {
959984 let ( key, val) = get_env_val ( "MY_KEY" , "my_value" ) ;
0 commit comments