diff --git a/vlib/os/os.v b/vlib/os/os.v index 4a58b8be34770e..c3076466bd2cb6 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -695,6 +695,12 @@ pub struct MkdirParams { // mkdir_all will create a valid full path of all directories given in `path`. pub fn mkdir_all(opath string, params MkdirParams) ! { + if exists(opath) { + if is_dir(opath) { + return + } + return error('path `${opath}` already exists, and is not a folder') + } other_separator := if path_separator == '/' { '\\' } else { '/' } path := opath.replace(other_separator, path_separator) mut p := if path.starts_with(path_separator) { path_separator } else { '' } diff --git a/vlib/os/os_test.c.v b/vlib/os/os_test.c.v index 74b71d7d5aa8e9..4223f2b5b65251 100644 --- a/vlib/os/os_test.c.v +++ b/vlib/os/os_test.c.v @@ -1016,3 +1016,17 @@ fn test_mv_across_partitions() { fn test_page_size() { assert os.page_size() >= 4096 // this is normal and assumed. } + +fn test_mkdir_at_file_dst() { + f3 := os.join_path('myfolder', 'f1', 'f2', 'f3') + os.mkdir_all(f3)! + assert os.is_dir(f3) + path := os.join_path(f3, 'no_ext_doc') + os.write_file(path, '')! + assert os.exists(path) && os.is_file(path) + os.mkdir_all(path) or { + assert err.msg() == 'path `${path}` already exists, and is not a folder', err.msg() + return + } + assert false +}