From 8947ed08cd438f64b5137ab8b43fe19857a0dfa9 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 2 Jul 2024 10:09:59 +0300 Subject: [PATCH] v: use os.wd_at_startup consistently (which is immutable), rather than os.getwd() --- vlib/v/builder/builder.v | 2 +- vlib/v/builder/compile.v | 2 +- vlib/v/checker/autocomplete.v | 4 ++-- vlib/v/checker/checker.v | 4 ++-- vlib/v/parser/parser.v | 7 ++----- vlib/v/pref/pref.v | 2 +- vlib/v/scanner/scanner.v | 2 +- vlib/v/util/errors.v | 4 ++-- vlib/v/util/module.v | 6 +++--- vlib/v/util/util.v | 2 +- 10 files changed, 16 insertions(+), 19 deletions(-) diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index 264de9e79c8c58..1b9bd9d2164ba8 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -399,7 +399,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.wd_at_startup // 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) { diff --git a/vlib/v/builder/compile.v b/vlib/v/builder/compile.v index 52bac1ebc8ee2f..39d669b522ba74 100644 --- a/vlib/v/builder/compile.v +++ b/vlib/v/builder/compile.v @@ -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.wd_at_startup } os.ensure_folder_is_writable(output_folder) or { // An early error here, is better than an unclear C error later: diff --git a/vlib/v/checker/autocomplete.v b/vlib/v/checker/autocomplete.v index fbfcf9afbaa707..84c09cd6aba9a5 100644 --- a/vlib/v/checker/autocomplete.v +++ b/vlib/v/checker/autocomplete.v @@ -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.wd_at_startup}" 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] @@ -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.wd_at_startup, c.file.path) if c.pref.linfo.path !in [c.file.path, abs_path] { return } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7f32ba3e3123d3..e812e4fa41ca77 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -403,7 +403,7 @@ 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.wd_at_startup}') c.pref.linfo.is_running = true for i, file in ast_files { // println(file.path) @@ -413,7 +413,7 @@ 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.wd_at_startup, file.path).replace('/./', '/') // TODO: join_path shouldn't have /./ if abs_path == c.pref.linfo.path { c.check_files([ast_files[i]]) exit(0) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 55180f7eb85d20..029db0533cd5bc 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -192,14 +192,11 @@ fn (mut p Parser) free_scanner() { } } -const normalised_working_folder = (os.real_path(os.getwd()) + os.path_separator).replace('\\', - '/') - pub fn (mut p Parser) set_path(path string) { p.file_path = path p.file_base = os.base(path) - p.file_display_path = os.real_path(p.file_path).replace_once(parser.normalised_working_folder, - '').replace('\\', '/') + p.file_display_path = os.real_path(p.file_path).replace('\\', '/').replace_once(util.normalised_workdir, + '') p.inside_vlib_file = os.dir(path).contains('vlib') p.inside_test_file = p.file_base.ends_with('_test.v') || p.file_base.ends_with('_test.vv') || p.file_base.all_before_last('.v').all_before_last('.').ends_with('_test') diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index 9b4e3b0f7afe79..b244533c85985a 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -861,7 +861,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.wd_at_startup, res.out_name) } i++ } diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index ec56a76e7b14b5..154e4055630a00 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -112,7 +112,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.wd_at_startup, 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) } diff --git a/vlib/v/util/errors.v b/vlib/v/util/errors.v index 54d9d10e747d1e..95e75480176e03 100644 --- a/vlib/v/util/errors.v +++ b/vlib/v/util/errors.v @@ -11,6 +11,8 @@ import v.errors import v.token import v.mathutil as mu +pub const normalised_workdir = os.wd_at_startup.replace('\\', '/') + '/' + // The filepath:line:col: format is the default C compiler error output format. // It allows editors and IDE's like emacs to quickly find the errors in the // output and jump to their source with a keyboard shortcut. @@ -69,8 +71,6 @@ pub fn color(kind string, msg string) string { return term.magenta(msg) } -const normalised_workdir = os.wd_at_startup.replace('\\', '/') + '/' - const verror_paths_absolute = os.getenv('VERROR_PATHS') == 'absolute' // path_styled_for_error_messages converts the given file `path`, into one suitable for displaying diff --git a/vlib/v/util/module.v b/vlib/v/util/module.v index 31a68d61e2ca90..6f985ed1c6cad7 100644 --- a/vlib/v/util/module.v +++ b/vlib/v/util/module.v @@ -56,15 +56,15 @@ pub fn qualify_module(pref_ &pref.Preferences, mod string, file_path string) str trace_qualify(@FN, mod, file_path, 'module_res 1', mod, 'main') return mod } - clean_file_path := file_path.all_before_last(os.path_separator) + clean_file_path := file_path.all_before_last(os.path_separator).replace('\\', '/') // relative module (relative to working directory) // TODO: find most stable solution & test with -usecache // - // TODO: 2022-01-30: Using os.getwd() here does not seem right *at all* imho. + // TODO: 2022-01-30: Using os.getwd() or normalised_workdir, here does not seem right *at all* imho. // 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 { + if clean_file_path.replace(normalised_workdir, '') == mod { trace_qualify(@FN, mod, file_path, 'module_res 2', mod, 'clean_file_path - getwd == mod, clean_file_path: ${clean_file_path}') return mod } diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index bcab7ccf8b6de4..b74f2ea1b02b4a 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -251,7 +251,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.wd_at_startup tlog('recompiling ${tool_source}') lockfile := tool_exe + '.lock' tlog('lockfile: ${lockfile}')