parser: fix mod_path_to_full_name to be compliant with --path option (#10149)

pull/10199/head
R cqls 2021-05-25 06:46:40 +02:00 committed by GitHub
parent f3274700cd
commit 5b8402bccb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 7 deletions

View File

@ -2544,7 +2544,7 @@ fn (mut p Parser) module_decl() ast.Module {
} }
module_pos = attrs_pos.extend(name_pos) module_pos = attrs_pos.extend(name_pos)
} }
full_name := util.qualify_module(name, p.file_name) full_name := util.qualify_module(p.pref, name, p.file_name)
p.mod = full_name p.mod = full_name
p.builtin_mod = p.mod == 'builtin' p.builtin_mod = p.mod == 'builtin'
mod_node = ast.Module{ mod_node = ast.Module{

View File

@ -15,20 +15,20 @@ pub fn qualify_import(pref &pref.Preferences, mod string, file_path string) stri
for search_path in mod_paths { for search_path in mod_paths {
try_path := os.join_path(search_path, mod_path) try_path := os.join_path(search_path, mod_path)
if os.is_dir(try_path) { if os.is_dir(try_path) {
if m1 := mod_path_to_full_name(mod, try_path) { if m1 := mod_path_to_full_name(pref, mod, try_path) {
trace_mod_path_to_full_name(@LINE, mod, try_path, m1) trace_mod_path_to_full_name(@LINE, mod, try_path, m1)
return m1 return m1
} }
} }
} }
if m1 := mod_path_to_full_name(mod, file_path) { if m1 := mod_path_to_full_name(pref, mod, file_path) {
trace_mod_path_to_full_name(@LINE, mod, file_path, m1) trace_mod_path_to_full_name(@LINE, mod, file_path, m1)
return m1 return m1
} }
return mod return mod
} }
pub fn qualify_module(mod string, file_path string) string { pub fn qualify_module(pref &pref.Preferences, mod string, file_path string) string {
if mod == 'main' { if mod == 'main' {
return mod return mod
} }
@ -38,7 +38,7 @@ pub fn qualify_module(mod string, file_path string) string {
if clean_file_path.replace(os.getwd() + os.path_separator, '') == mod { if clean_file_path.replace(os.getwd() + os.path_separator, '') == mod {
return mod return mod
} }
if m1 := mod_path_to_full_name(mod, clean_file_path) { if m1 := mod_path_to_full_name(pref, mod, clean_file_path) {
trace_mod_path_to_full_name(@LINE, mod, clean_file_path, m1) trace_mod_path_to_full_name(@LINE, mod, clean_file_path, m1)
return m1 return m1
} }
@ -50,10 +50,15 @@ pub fn qualify_module(mod string, file_path string) string {
// * if possible split this function in two, one which gets the // * if possible split this function in two, one which gets the
// parent module path and another which turns it into the full name // parent module path and another which turns it into the full name
// * create shared logic between these fns and builder.find_module_path // * create shared logic between these fns and builder.find_module_path
pub fn mod_path_to_full_name(mod string, path string) ?string { pub fn mod_path_to_full_name(pref &pref.Preferences, mod string, path string) ?string {
// TODO: explore using `pref.lookup_path` & `os.vmodules_paths()` // TODO: explore using `pref.lookup_path` & `os.vmodules_paths()`
// absolute paths instead of 'vlib' & '.vmodules' // absolute paths instead of 'vlib' & '.vmodules'
vmod_folders := ['vlib', '.vmodules', 'modules'] mut vmod_folders := ['vlib', '.vmodules', 'modules']
for base in pref.lookup_path.map(os.base(it)) {
if !(base in vmod_folders) {
vmod_folders << base
}
}
mut in_vmod_path := false mut in_vmod_path := false
for vmod_folder in vmod_folders { for vmod_folder in vmod_folders {
if path.contains(vmod_folder + os.path_separator) { if path.contains(vmod_folder + os.path_separator) {