From 60d6543733b310beb2ba226c96370a705b3a61de Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 10 Apr 2020 13:01:06 +0300 Subject: [PATCH] comptime: support @VROOT again --- cmd/tools/vtest-fixed.v | 1 - vlib/v/builder/builder.v | 2 -- vlib/v/parser/comptime.v | 21 +++++++++------------ vlib/v/parser/parser.v | 4 +++- vlib/v/{builder => vmod}/vmod.v | 7 +++++-- 5 files changed, 17 insertions(+), 18 deletions(-) rename vlib/v/{builder => vmod}/vmod.v (97%) diff --git a/cmd/tools/vtest-fixed.v b/cmd/tools/vtest-fixed.v index 6f0369dd57..ca1ece8ae5 100644 --- a/cmd/tools/vtest-fixed.v +++ b/cmd/tools/vtest-fixed.v @@ -40,7 +40,6 @@ const ( 'vlib/v/tests/num_lit_call_method_test.v', 'vlib/v/tests/option_test.v', 'vlib/v/tests/pointers_test.v', - 'vlib/v/tests/project_with_c_code/main_test.v', 'vlib/v/tests/project_with_modules_having_submodules/bin/a_program_under_bin_can_find_mod1_test.v', 'vlib/v/tests/project_with_modules_having_submodules/tests/submodule_test.v', 'vlib/v/tests/repl/repl_test.v', diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index 484d2ded73..815405736f 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -15,7 +15,6 @@ import ( pub struct Builder { pub: - mod_file_cacher &ModFileCacher // used during lookup for v.mod to support @VROOT pref &pref.Preferences table &table.Table checker checker.Checker @@ -33,7 +32,6 @@ pub fn new_builder(pref &pref.Preferences) Builder { compiled_dir := if os.is_dir(rdir) { rdir } else { os.dir(rdir) } table := table.new_table() return builder.Builder{ - mod_file_cacher: new_mod_file_cacher() pref: pref table: table checker: checker.new_checker(table, pref) diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 767f78c773..0520b1b09d 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -6,6 +6,7 @@ module parser import ( v.ast v.pref + v.vmod ) const ( @@ -22,18 +23,14 @@ fn (p mut Parser) hash() ast.HashStmt { mut flag := val[5..] // expand `@VROOT` to its absolute path if flag.contains('@VROOT') { - /* - vmod_file_location := p.v.mod_file_cacher.get( p.file_path_dir ) - if vmod_file_location.vmod_file.len == 0 { - // There was no actual v.mod file found. - p.error_with_token_index('To use @VROOT, you need' + - ' to have a "v.mod" file in ${p.file_path_dir},' + - ' or in one of its parent folders.', - p.cur_tok_index() - 1) - } - flag = flag.replace('@VROOT', vmod_file_location.vmod_folder ) - flag = flag.replace('@VROOT', '/Users/alex/code/v/') -*/ + vmod_file_location := vmod.mod_file_cacher.get( p.file_name_dir ) + if vmod_file_location.vmod_file.len == 0 { + // There was no actual v.mod file found. + p.error('To use @VROOT, you need' + + ' to have a "v.mod" file in ${p.file_name_dir},' + + ' or in one of its parent folders.') + } + flag = flag.replace('@VROOT', vmod_file_location.vmod_folder ) } for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] { if flag.contains(deprecated) { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 5162d584c3..e8c7e4777d 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -16,7 +16,8 @@ import ( struct Parser { scanner &scanner.Scanner - file_name string + file_name string // "/home/user/hello.v" + file_name_dir string // "/home/user" mut: tok token.Token peek_tok token.Token @@ -67,6 +68,7 @@ pub fn parse_file(path string, table &table.Table, comments_mode scanner.Comment scanner: scanner.new_scanner_file(path, comments_mode) table: table file_name: path + file_name_dir: os.dir( path ) pref: pref scope: &ast.Scope{ start_pos: 0 diff --git a/vlib/v/builder/vmod.v b/vlib/v/vmod/vmod.v similarity index 97% rename from vlib/v/builder/vmod.v rename to vlib/v/vmod/vmod.v index 1f62baee19..fe724e7fc1 100644 --- a/vlib/v/builder/vmod.v +++ b/vlib/v/vmod/vmod.v @@ -1,8 +1,7 @@ -module builder +module vmod import os - // This file provides a caching mechanism for seeking quickly whether a // given folder has a v.mod file in it or in any of its parent folders. // @@ -144,3 +143,7 @@ fn (mcache mut ModFileCacher) get_files(cfolder string) []string { mcache.folder_files[ cfolder ] = files return files } + +pub const ( + mod_file_cacher = new_mod_file_cacher() // used during lookup for v.mod to support @VROOT +)