diff --git a/vlib/os/os.c.v b/vlib/os/os.c.v index 581c381f86..4416543016 100644 --- a/vlib/os/os.c.v +++ b/vlib/os/os.c.v @@ -744,11 +744,10 @@ pub fn is_link(path string) bool { } // chdir changes the current working directory to the new directory in `path`. -pub fn chdir(path string) { - $if windows { - C._wchdir(path.to_wide()) - } $else { - _ = C.chdir(&char(path.str)) +pub fn chdir(path string) ? { + ret := $if windows { C._wchdir(path.to_wide()) } $else { C.chdir(&char(path.str)) } + if ret == -1 { + return error_with_code(posix_get_error_msg(C.errno), C.errno) } } diff --git a/vlib/os/os_test.v b/vlib/os/os_test.v index 7107a34f2b..88cdd584be 100644 --- a/vlib/os/os_test.v +++ b/vlib/os/os_test.v @@ -17,7 +17,7 @@ fn testsuite_begin() { os.rmdir_all(tfolder) or {} assert !os.is_dir(tfolder) os.mkdir_all(tfolder) or { panic(err) } - os.chdir(tfolder) + os.chdir(tfolder) or {} assert os.is_dir(tfolder) // println('args_at_start: $args_at_start') assert args_at_start.len > 0 @@ -25,7 +25,7 @@ fn testsuite_begin() { } fn testsuite_end() { - os.chdir(os.wd_at_startup) + os.chdir(os.wd_at_startup) or {} os.rmdir_all(tfolder) or {} assert !os.is_dir(tfolder) // eprintln('testsuite_end , tfolder = $tfolder removed.') @@ -326,9 +326,9 @@ fn test_realpath_removes_dots() { fn test_realpath_absolutizes_existing_relative_paths() { old_wd := os.getwd() defer { - os.chdir(old_wd) + os.chdir(old_wd) or { panic(err) } } - os.chdir(@VEXEROOT) + os.chdir(@VEXEROOT) or { panic(err) } examples_folder := os.join_path('vlib', 'v', '..', '..', 'cmd', '.', '..', 'examples') real_path_of_examples_folder := os.real_path(examples_folder) assert os.is_abs_path(real_path_of_examples_folder) diff --git a/vlib/szip/szip_test.v b/vlib/szip/szip_test.v index a20f638569..e9d64128aa 100644 --- a/vlib/szip/szip_test.v +++ b/vlib/szip/szip_test.v @@ -11,7 +11,7 @@ const ( ) fn test_szip_create_temp_files() ? { - os.chdir(os.temp_dir()) + os.chdir(os.temp_dir()) or {} os.rmdir_all(test_path) or {} os.mkdir(test_path) ? os.write_file(fpath1, 'file one') ? @@ -45,7 +45,7 @@ fn test_reading_zipping_files() ? { file_name_list << 'file_${i:02}.txt' } - os.chdir(os.temp_dir()) + os.chdir(os.temp_dir()) or {} os.rmdir_all(test_path) or {} os.mkdir(test_path) ? for c, f_name in file_name_list { diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index 11ca40fea7..214f73bce9 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -132,7 +132,7 @@ fn (mut v Builder) rebuild_cached_module(vexe string, imp_path string) string { // do run `v build-module x` always in main vfolder; x can be a relative path pwd := os.getwd() vroot := os.dir(vexe) - os.chdir(vroot) + os.chdir(vroot) or {} boptions := v.pref.build_options.join(' ') rebuild_cmd := '$vexe $boptions build-module $imp_path' vcache.dlog('| Builder.' + @FN, 'vexe: $vexe | imp_path: $imp_path | rebuild_cmd: $rebuild_cmd') @@ -140,7 +140,7 @@ fn (mut v Builder) rebuild_cached_module(vexe string, imp_path string) string { rebuilded_o := v.pref.cache_manager.exists('.o', imp_path) or { panic('could not rebuild cache module for $imp_path, error: $err.msg') } - os.chdir(pwd) + os.chdir(pwd) or {} return rebuilded_o } return res @@ -638,7 +638,7 @@ fn (mut v Builder) cc() { } } // - os.chdir(vdir) + os.chdir(vdir) or {} tried_compilation_commands << cmd v.show_cc(cmd, response_file, response_file_content) // Run @@ -649,7 +649,7 @@ fn (mut v Builder) cc() { if v.pref.show_c_output { v.show_c_compiler_output(res) } - os.chdir(original_pwd) + os.chdir(original_pwd) or {} vcache.dlog('| Builder.' + @FN, '> v.pref.use_cache: $v.pref.use_cache | v.pref.retry_compilation: $v.pref.retry_compilation') vcache.dlog('| Builder.' + @FN, '> cmd res.exit_code: $res.exit_code | cmd: $cmd') vcache.dlog('| Builder.' + @FN, '> response_file_content:\n$response_file_content') @@ -957,7 +957,7 @@ fn (mut v Builder) build_thirdparty_obj_file(path string, moduleflags []cflag.CF // // prepare for tcc, it needs relative paths to thirdparty/tcc to work: current_folder := os.getwd() - os.chdir(os.dir(pref.vexe_path())) + os.chdir(os.dir(pref.vexe_path())) or {} // mut all_options := []string{} all_options << v.pref.third_party_option @@ -970,7 +970,7 @@ fn (mut v Builder) build_thirdparty_obj_file(path string, moduleflags []cflag.CF println('>>> build_thirdparty_obj_files cmd: $cmd') } res := os.execute(cmd) - os.chdir(current_folder) + os.chdir(current_folder) or {} if res.exit_code != 0 { eprintln('failed thirdparty object build cmd:\n$cmd') verror(res.output) diff --git a/vlib/v/gen/c/coutput_test.v b/vlib/v/gen/c/coutput_test.v index 516dc374fd..b8e272961b 100644 --- a/vlib/v/gen/c/coutput_test.v +++ b/vlib/v/gen/c/coutput_test.v @@ -14,7 +14,7 @@ const diff_cmd = diff.find_working_diff_command() or { '' } fn test_out_files() ? { println(term.colorize(term.green, '> testing whether .out files match:')) - os.chdir(vroot) + os.chdir(vroot) or {} output_path := os.join_path(os.temp_dir(), 'coutput', 'out') os.mkdir_all(output_path) ? defer { @@ -82,7 +82,7 @@ fn test_out_files() ? { fn test_c_must_have_files() ? { println(term.colorize(term.green, '> testing whether `.c.must_have` files match:')) - os.chdir(vroot) + os.chdir(vroot) or {} output_path := os.join_path(os.temp_dir(), 'coutput', 'c_must_have') os.mkdir_all(output_path) ? defer { diff --git a/vlib/v/gen/native/macho_test.v b/vlib/v/gen/native/macho_test.v index 9da074d71d..2909cad522 100644 --- a/vlib/v/gen/native/macho_test.v +++ b/vlib/v/gen/native/macho_test.v @@ -4,7 +4,7 @@ import v.pref import v.ast fn test_macho() { - os.chdir(os.temp_dir()) + os.chdir(os.temp_dir()) or {} mut g := native.Gen{ pref: &pref.Preferences{} out_name: 'test.bin' diff --git a/vlib/v/tests/inout/compiler_test.v b/vlib/v/tests/inout/compiler_test.v index 88178ce876..140dfd7bd7 100644 --- a/vlib/v/tests/inout/compiler_test.v +++ b/vlib/v/tests/inout/compiler_test.v @@ -13,7 +13,7 @@ fn test_all() { mut total_errors := 0 vexe := os.getenv('VEXE') vroot := os.dir(vexe) - os.chdir(vroot) + os.chdir(vroot) or {} diff_cmd := diff.find_working_diff_command() or { '' } dir := 'vlib/v/tests/inout' files := os.ls(dir) or { panic(err) } diff --git a/vlib/v/tests/profile/profile_test.v b/vlib/v/tests/profile/profile_test.v index f802c7bd5b..f8c99b45a2 100644 --- a/vlib/v/tests/profile/profile_test.v +++ b/vlib/v/tests/profile/profile_test.v @@ -10,7 +10,7 @@ fn test_vexe_exists() { } fn test_v_profile_works() { - os.chdir(vroot) + os.chdir(vroot) or {} program_source := os.join_path(vroot, 'vlib/v/tests/profile/profile_test_1.v') res := os.execute('"$vexe" -profile - run $program_source') // eprintln('res: $res') diff --git a/vlib/v/tests/repl/runner/runner.v b/vlib/v/tests/repl/runner/runner.v index 54376765fe..45a8f07400 100644 --- a/vlib/v/tests/repl/runner/runner.v +++ b/vlib/v/tests/repl/runner/runner.v @@ -117,7 +117,7 @@ pub fn new_options() RunnerOptions { if os.args.len > 1 { files = os.args[1..] } else { - os.chdir(os.dir(vexec)) + os.chdir(os.dir(vexec)) or {} wd = os.getwd() files = os.walk_ext('.', '.repl') } diff --git a/vlib/v/tests/vmod_parser_test.v b/vlib/v/tests/vmod_parser_test.v index 8d75f1b8e9..10f48141c8 100644 --- a/vlib/v/tests/vmod_parser_test.v +++ b/vlib/v/tests/vmod_parser_test.v @@ -2,7 +2,7 @@ import v.vmod import os fn test_from_file() { - os.chdir(os.dir(os.getenv('VEXE'))) + os.chdir(os.dir(os.getenv('VEXE'))) or {} data := vmod.from_file('./v.mod') or { panic(err) } assert data.name == 'V' assert data.description == 'The V programming language.' diff --git a/vlib/v/vcache/vcache_test.v b/vlib/v/vcache/vcache_test.v index 23ecda9a5d..31ecfbd798 100644 --- a/vlib/v/vcache/vcache_test.v +++ b/vlib/v/vcache/vcache_test.v @@ -95,7 +95,7 @@ fn test_readme_exists_and_is_readable() { } fn testsuite_end() { - os.chdir(os.wd_at_startup) + os.chdir(os.wd_at_startup) or {} os.rmdir_all(vcache_folder) or {} assert !os.is_dir(vcache_folder) } diff --git a/vlib/vweb/tests/vweb_test.v b/vlib/vweb/tests/vweb_test.v index b10dad113c..02908f4199 100644 --- a/vlib/vweb/tests/vweb_test.v +++ b/vlib/vweb/tests/vweb_test.v @@ -18,7 +18,7 @@ const ( // setup of vweb webserver fn testsuite_begin() { - os.chdir(vroot) + os.chdir(vroot) or {} if os.exists(serverexe) { os.rm(serverexe) or {} }