diff --git a/vlib/os/os.v b/vlib/os/os.v index a87a25450f..94cef15733 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -170,6 +170,9 @@ pub fn dir(path string) string { return '.' } pos := path.last_index(path_separator) or { return '.' } + if pos == 0 && path_separator == '/' { + return '/' + } return path[..pos] } diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 0c2c78824c..fcc6d698df 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -534,6 +534,8 @@ fn test_dir() { assert os.dir('C:\\a\\b\\c') == 'C:\\a\\b' assert os.dir('C:\\a\\b\\') == 'C:\\a\\b' } $else { + assert os.dir('/') == '/' + assert os.dir('/abc') == '/' assert os.dir('/var/tmp/foo') == '/var/tmp' assert os.dir('/var/tmp/') == '/var/tmp' }