From 44892fd942c6cd97bec59b2c90f1580f829c2155 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 30 Jul 2021 03:14:09 +0300 Subject: [PATCH] os: let `os.dir("/xyz")` return "/" (fixes compiling .v files in /) --- vlib/os/os.v | 3 +++ vlib/os/os_test.v | 2 ++ 2 files changed, 5 insertions(+) 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' }