Skip to content

Commit

Permalink
v: use os.wd_at_startup consistently (which is immutable), rather tha…
Browse files Browse the repository at this point in the history
…n os.getwd()
  • Loading branch information
spytheman committed Jul 2, 2024
1 parent 0682500 commit 8947ed0
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion vlib/v/builder/builder.v
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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.wd_at_startup
}
os.ensure_folder_is_writable(output_folder) or {
// An early error here, is better than an unclear C error later:
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.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]
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.wd_at_startup, c.file.path)
if c.pref.linfo.path !in [c.file.path, abs_path] {
return
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -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')
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 @@ -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++
}
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 @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/util/errors.v
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions vlib/v/util/module.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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 @@ -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}')
Expand Down

0 comments on commit 8947ed0

Please sign in to comment.