Skip to content

Commit

Permalink
os: deprecate os.getwd in favor of os.get_current_dir (part 1) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Elsie19 authored Nov 25, 2024
1 parent 9490582 commit 9300982
Show file tree
Hide file tree
Showing 34 changed files with 60 additions and 46 deletions.
2 changes: 1 addition & 1 deletion cmd/tools/amalgamate.v
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ fn (mut c Context) amalgamate() ! {
if c.config.input_files.len == 0 {
// source += '/* ########## stdin */\n'
// if there are no input files, read from stdin
local_dir := os.getwd()
local_dir := os.get_current_dir()
source += c.handle_includes(local_dir, os.get_raw_lines_joined())!
// source += '/* ########## stdin end */\n'
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/bench/map_clear_runner.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ step := os.args[3] or { '500_000' }.int()
os.chdir(os.dir(@VEXE))!
vcmd := 'v ${flags} cmd/tools/bench/map_clear.v'

println('>> start: ${start} | end: ${end} | step: ${step} | workdir: "${os.getwd()}" | flags: "${flags}" | vcmd: "${vcmd}"')
println('>> start: ${start} | end: ${end} | step: ${step} | workdir: "${os.get_current_dir()}" | flags: "${flags}" | vcmd: "${vcmd}"')
assert os.system(vcmd) == 0

println('running...')
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/fast/fast_job.v
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn main() {
elog('fast_job PATH: ${os.getenv('PATH')}')

os.chdir(fast_dir)!
elog('fast_job start in os.getwd(): ${os.getwd()}')
elog('fast_job start in os.get_current_dir(): ${os.get_current_dir()}')

defer {
elog('fast_job end')
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/modules/testing/common.v
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ pub fn (mut ts TestSession) add(file string) {
pub fn (mut ts TestSession) test() {
// Ensure that .tmp.c files generated from compiling _test.v files,
// are easy to delete at the end, *without* affecting the existing ones.
current_wd := os.getwd()
current_wd := os.get_current_dir()
if current_wd == os.wd_at_startup && current_wd == ts.vroot {
ts.root_relative = true
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/performance_compare.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ mut:

fn new_context() Context {
return Context{
cwd: os.getwd()
cwd: os.get_current_dir()
commit_after: 'master'
warmups: 4
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/test_os_process.v
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn main() {
eprintln('> args: ${args} | context: ${ctx}')
}
if ctx.show_wd {
ctx.println('WORK_DIR=${os.getwd()}')
ctx.println('WORK_DIR=${os.get_current_dir()}')
}
if ctx.show_env {
all := os.environ()
Expand Down
4 changes: 2 additions & 2 deletions cmd/tools/vast/vast.v
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ fn get_abs_path(path string) string {
if os.is_abs_path(path) {
return path
} else if path.starts_with('./') {
return os.join_path(os.getwd(), path[2..])
return os.join_path(os.get_current_dir(), path[2..])
} else {
return os.join_path(os.getwd(), path)
return os.join_path(os.get_current_dir(), path)
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/vbug.v
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn get_vdoctor_output(is_verbose bool) string {
fn get_v_build_output(is_verbose bool, is_yes bool, file_path string) string {
mut vexe := os.getenv('VEXE')
// prepare a V compiler with -g to have better backtraces if possible
wd := os.getwd()
wd := os.get_current_dir()
os.chdir(vroot) or {}
verbose_flag := if is_verbose { '-v' } else { '' }
vdbg_path := $if windows { '${vroot}/vdbg.exe' } $else { '${vroot}/vdbg' }
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/vcover/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn normalize_path(path string) string {

fn main() {
mut ctx := Context{}
ctx.working_folder = normalize_path(os.real_path(os.getwd()))
ctx.working_folder = normalize_path(os.real_path(os.get_current_dir()))
mut fp := flag.new_flag_parser(os.args#[1..])
fp.application('v cover')
fp.version('0.0.2')
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/vcreate/vcreate.v
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn init_project(cmd Command) ! {
mut c := Create{
template: get_template(cmd)
}
dir_name := check_name(os.file_name(os.getwd()))
dir_name := check_name(os.file_name(os.get_current_dir()))
if !os.exists('v.mod') {
mod_dir_has_hyphens := dir_name.contains('-')
c.name = if mod_dir_has_hyphens { dir_name.replace('-', '_') } else { dir_name }
Expand Down
4 changes: 2 additions & 2 deletions cmd/tools/vdoctor.v
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ fn (mut a App) collect_info() {
a.line('OS', '${os_kind}, ${os_details}')
a.line('Processor', arch_details.join(', '))
a.println('')
getwd := os.getwd()
get_current_dir := os.get_current_dir()
vmodules := os.vmodules_dir()
vtmp_dir := os.vtmp_dir()
vexe := os.getenv('VEXE')
vroot := os.dir(vexe)
os.chdir(vroot) or {}
a.line('getwd', getwd)
a.line('get_current_dir', get_current_dir)
a.line('vexe', vexe)
a.line('vexe mtime', time.unix(os.file_last_mod_unix(vexe)).str())
a.println('')
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/vsymlink/vsymlink.v
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ fn setup_symlink_github() {
eprintln(' On windows, use `.\\v.exe symlink` instead.')
exit(1)
}
content += '\n${os.getwd()}\n'
content += '\n${os.get_current_dir()}\n'
os.write_file(os.getenv('GITHUB_PATH'), content) or { panic('Failed to write to GITHUB_PATH.') }
}
2 changes: 1 addition & 1 deletion vlib/os/filepath.v
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub fn is_abs_path(path string) bool {
// with the given `path` (if the `path` is relative)
// and returns the absolute path representation.
pub fn abs_path(path string) string {
wd := getwd()
wd := get_current_dir()
if path == '' {
return wd
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/os/filepath_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn test_norm_path() {
}

fn test_abs_path() {
wd := getwd()
wd := get_current_dir()
wd_w_sep := wd + path_separator
$if windows {
assert abs_path('path/to/file.v') == '${wd_w_sep}path\\to\\file.v'
Expand Down Expand Up @@ -147,7 +147,7 @@ fn test_abs_path() {
}

fn test_existing_path() {
wd := getwd()
wd := get_current_dir()
$if windows {
assert existing_path('') or { '' } == ''
assert existing_path('..') or { '' } == '..'
Expand Down
2 changes: 1 addition & 1 deletion vlib/os/find_abs_path_of_executable_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_find_abs_path_of_executable() {
}

original_path := os.getenv('PATH')
original_wdir := os.getwd()
original_wdir := os.get_current_dir()
defer {
os.chdir(original_wdir) or {}
}
Expand Down
10 changes: 9 additions & 1 deletion vlib/os/os.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,16 @@ pub fn chdir(path string) ! {
}

// getwd returns the absolute path of the current directory.
@[manualfree]
// @[deprecated: 'use `get_current_dir()` instead']
// TODO: uncomment the deprecation here, after 2024-11-27
@[deprecated_after: '2024-12-24']
pub fn getwd() string {
return get_current_dir()
}

// get_current_dir returns the absolute path of the current directory.
@[manualfree]
pub fn get_current_dir() string {
unsafe {
buf := [max_path_buffer_size]u8{}
$if windows {
Expand Down
2 changes: 1 addition & 1 deletion vlib/os/os.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import strings

pub const max_path_len = 4096

pub const wd_at_startup = getwd()
pub const wd_at_startup = get_current_dir()

const f_ok = 0

Expand Down
4 changes: 4 additions & 0 deletions vlib/os/os_js.js.v
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ pub fn read_file(s string) !string {
}

pub fn getwd() string {
return get_current_dir()
}

pub fn get_current_dir() string {
res := ''
#res.str = $process.cwd()

Expand Down
2 changes: 1 addition & 1 deletion vlib/os/os_test.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ fn test_realpath_removes_dots() {
}

fn test_realpath_absolutizes_existing_relative_paths() {
old_wd := os.getwd()
old_wd := os.get_current_dir()
defer {
os.chdir(old_wd) or { panic(err) }
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/os/process_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn test_getpid() {

fn test_set_work_folder() {
new_work_folder := os.real_path(os.temp_dir())
parent_working_folder := os.getwd()
parent_working_folder := os.get_current_dir()
dump(new_work_folder)
dump(parent_working_folder)
if new_work_folder == parent_working_folder {
Expand All @@ -57,7 +57,7 @@ fn test_set_work_folder() {
child_work_folder := output.find_between('stdout, WORK_DIR=', '\n').trim_space()
dump(child_work_folder)
assert child_work_folder == new_work_folder
new_parent_work_folder := os.getwd()
new_parent_work_folder := os.get_current_dir()
dump(new_parent_work_folder)
assert new_parent_work_folder == parent_working_folder
assert new_parent_work_folder != child_work_folder
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/builder/builder.v
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ pub fn (b &Builder) find_module_path(mod string, fpath string) !string {
module_lookup_paths << vmod_file_location.vmod_folder
}
module_lookup_paths << b.module_search_paths
module_lookup_paths << os.getwd()
module_lookup_paths << os.get_current_dir()
// go up through parents looking for modules a folder.
// we need a proper solution that works most of the time. look at vdoc.get_parent_mod
if fpath.contains(os.path_separator + 'modules' + os.path_separator) {
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/builder/cc.v
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ pub fn (mut v Builder) cc() {
vdir := os.dir(vexe)
mut tried_compilation_commands := []string{}
mut tcc_output := os.Result{}
original_pwd := os.getwd()
original_pwd := os.get_current_dir()
for {
// try to compile with the chosen compiler
// if compilation fails, retry again with another
Expand Down Expand Up @@ -1109,7 +1109,7 @@ fn (mut v Builder) build_thirdparty_obj_file(mod string, path string, moduleflag
println(rebuild_reason_message)
}
// prepare for tcc, it needs relative paths to thirdparty/tcc to work:
current_folder := os.getwd()
current_folder := os.get_current_dir()
os.chdir(v.pref.vroot) or {}

mut all_options := []string{}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/builder/compile.v
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn check_if_output_folder_is_writable(pref_ &pref.Preferences) {
// without a folder component, just use the current folder instead:
mut output_folder := odir
if odir.len == pref_.out_name.len {
output_folder = os.getwd()
output_folder = os.get_current_dir()
}
os.ensure_folder_is_writable(output_folder) or {
// An early error here, is better than an unclear C error later:
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/builder/rebuilding.v
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ pub fn (mut b Builder) find_invalidated_modules_by_files(all_files []string) []s
}

fn (mut b Builder) v_build_module(vexe string, imp_path string) {
pwd := os.getwd()
pwd := os.get_current_dir()
defer {
os.chdir(pwd) or {}
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/autocomplete.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn (mut c Checker) ident_autocomplete(node ast.Ident) {
// Mini LS hack (v -line-info "a.v:16")
println(
'checker.ident() info.line_nr=${c.pref.linfo.line_nr} node.line_nr=${node.pos.line_nr} ' +
' pwd="${os.getwd()}" file="${c.file.path}", ' +
' pwd="${os.get_current_dir()}" file="${c.file.path}", ' +
' pref.linfo.path="${c.pref.linfo.path}" node.name="${node.name}" expr="${c.pref.linfo.expr}"')
// Make sure this ident is on the same line as requeste, in the same file, and has the same name
same_line := node.pos.line_nr in [c.pref.linfo.line_nr - 1, c.pref.linfo.line_nr + 1, c.pref.linfo.line_nr]
Expand All @@ -21,7 +21,7 @@ fn (mut c Checker) ident_autocomplete(node ast.Ident) {
if !same_name {
return
}
abs_path := os.join_path(os.getwd(), c.file.path)
abs_path := os.join_path(os.get_current_dir(), c.file.path)
if c.pref.linfo.path !in [c.file.path, abs_path] {
return
}
Expand Down
6 changes: 4 additions & 2 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ pub fn (mut c Checker) check_files(ast_files []&ast.File) {
// After the main checker run, run the line info check, print line info, and exit (if it's present)
if !c.pref.linfo.is_running && c.pref.line_info != '' { //'' && c.pref.linfo.line_nr == 0 {
// c.do_line_info(c.pref.line_info, ast_files)
println('setting is_running=true, pref.path=${c.pref.linfo.path} curdir' + os.getwd())
println('setting is_running=true, pref.path=${c.pref.linfo.path} curdir' +
os.get_current_dir())
c.pref.linfo.is_running = true
for i, file in ast_files {
// println(file.path)
Expand All @@ -432,7 +433,8 @@ pub fn (mut c Checker) check_files(ast_files []&ast.File) {
exit(0)
} else if file.path.starts_with('./') {
// Maybe it's a "./foo.v", linfo.path has an absolute path
abs_path := os.join_path(os.getwd(), file.path).replace('/./', '/') // TODO: join_path shouldn't have /./
abs_path := os.join_path(os.get_current_dir(), file.path).replace('/./',
'/') // TODO: join_path shouldn't have /./
if abs_path == c.pref.linfo.path {
c.check_files([ast_files[i]])
exit(0)
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn (mut p Parser) free_scanner() {
}
}

const normalised_working_folder = (os.real_path(os.getwd()) + os.path_separator).replace('\\',
const normalised_working_folder = (os.real_path(os.get_current_dir()) + os.path_separator).replace('\\',
'/')

pub fn (mut p Parser) set_path(path string) {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/pref/pref.v
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
res.is_o = true
}
if !os.is_abs_path(res.out_name) {
res.out_name = os.join_path(os.getwd(), res.out_name)
res.out_name = os.join_path(os.get_current_dir(), res.out_name)
}
i++
}
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/scanner/scanner.v
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn new_scanner_file(file_path string, comments_mode CommentsMode, pref_ &pre
mut raw_text := util.read_file(file_path) or { return err }
if pref_.line_info != '' {
// Add line info expr to the scanner text
abs_path := os.join_path(os.getwd(), file_path)
abs_path := os.join_path(os.get_current_dir(), file_path)
if pref_.linfo.path in [file_path, abs_path] {
raw_text = pref.add_line_info_expr_to_program_text(raw_text, pref_.linfo)
}
Expand Down
8 changes: 4 additions & 4 deletions vlib/v/slow_tests/repl/runner/runner.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn full_path_to_v(dirs_in int) string {
args := os.args
vreal := os.real_path('v')
myself := os.real_path( os.executable() )
wd := os.getwd()
wd := os.get_current_dir()
println('args are: $args')
println('vreal : $vreal')
println('myself : $myself')
Expand Down Expand Up @@ -90,13 +90,13 @@ pub fn run_prod_file(wd string, vexec string, file string) !string {

pub fn new_options() RunnerOptions {
vexec := full_path_to_v(5)
mut wd := os.getwd()
mut wd := os.get_current_dir()
mut files := []string{}
if os.args.len > 1 {
files = os.args[1..].clone()
} else {
os.chdir(os.dir(vexec)) or {}
wd = os.getwd()
wd = os.get_current_dir()
files = os.walk_ext('.', '.repl')
}
return RunnerOptions{
Expand All @@ -107,7 +107,7 @@ pub fn new_options() RunnerOptions {
}

pub fn new_prod_options() RunnerOptions {
wd := os.getwd()
wd := os.get_current_dir()
vexec := full_path_to_v(4)
mut files := []string{}
if os.args.len > 1 {
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/util/module.v
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ pub fn qualify_module(pref_ &pref.Preferences, mod string, file_path string) str
// TODO: 2022-01-30: That makes lookup dependent on fragile environment factors.
// TODO: 2022-01-30: The lookup should be relative to the folder, in which the current file is,
// TODO: 2022-01-30: *NOT* to the working folder of the compiler, which can change easily.
if clean_file_path.replace(os.getwd() + os.path_separator, '') == mod {
trace_qualify(@FN, mod, file_path, 'module_res 2', mod, 'clean_file_path - getwd == mod, clean_file_path: ${clean_file_path}')
if clean_file_path.replace(os.get_current_dir() + os.path_separator, '') == mod {
trace_qualify(@FN, mod, file_path, 'module_res 2', mod, 'clean_file_path - get_current_dir == mod, clean_file_path: ${clean_file_path}')
return mod
}
if m1 := mod_path_to_full_name(pref_, mod, clean_file_path) {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/util/util.v
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ pub fn launch_tool(is_verbose bool, tool_name string, args []string) {
println('Compiling ${tool_name} with: "${compilation_command}"')
}

current_work_dir := os.getwd()
current_work_dir := os.get_current_dir()
tlog('recompiling ${tool_source}')
lockfile := tool_exe + '.lock'
tlog('lockfile: ${lockfile}')
Expand Down
4 changes: 2 additions & 2 deletions vlib/veb/static_handler.v
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn (mut sh StaticHandler) handle_static(directory_path string, root bool) !b
// ```
pub fn (mut sh StaticHandler) host_handle_static(host string, directory_path string, root bool) !bool {
if !os.exists(directory_path) {
return error('directory `${directory_path}` does not exist. The directory should be relative to the current working directory: ${os.getwd()}')
return error('directory `${directory_path}` does not exist. The directory should be relative to the current working directory: ${os.get_current_dir()}')
}
dir_path := directory_path.trim_space().trim_right('/')
mut mount_path := ''
Expand Down Expand Up @@ -84,7 +84,7 @@ pub fn (mut sh StaticHandler) host_mount_static_folder_at(host string, directory
if mount_path == '' || mount_path[0] != `/` {
return error('invalid mount path! The path should start with `/`')
} else if !os.exists(directory_path) {
return error('directory `${directory_path}` does not exist. The directory should be relative to the current working directory: ${os.getwd()}')
return error('directory `${directory_path}` does not exist. The directory should be relative to the current working directory: ${os.get_current_dir()}')
}

dir_path := directory_path.trim_right('/')
Expand Down
Loading

0 comments on commit 9300982

Please sign in to comment.