Skip to content

Commit a5ee1ee

Browse files
committed
builder: "v run src" and "v run ./scr" behaves differently (fixes #24205)
1 parent 365ea7b commit a5ee1ee

3 files changed

Lines changed: 66 additions & 18 deletions

File tree

01KKEFK1PV8GPPZGSR1K41WMB6

147 KB
Binary file not shown.

vlib/v/builder/builder_test.v

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ fn testsuite_end() {
1313
os.rmdir_all(test_path) or {}
1414
}
1515

16+
fn run_v_ok(command string) string {
17+
res := os.execute(command)
18+
if res.exit_code != 0 {
19+
eprintln('> failing cmd: ${command}')
20+
eprintln('> output:\n${res.output}')
21+
assert res.exit_code == 0
22+
}
23+
return res.output
24+
}
25+
1626
fn test_conditional_executable_removal() {
1727
os.chdir(test_path)!
1828
os.mkdir_all('src')!
@@ -82,3 +92,30 @@ pub struct BS{}
8292
assert executable in after_compile_file_list
8393
assert os.execute('./${executable}').output.trim_space() == 'AS{}=>BS{}'
8494
}
95+
96+
fn test_run_explicit_src_directory_uses_project_root_lookup() {
97+
os.chdir(test_path)!
98+
project_dir := os.join_path(test_path, 'run_src_project')
99+
defer {
100+
os.chdir(test_path) or {}
101+
}
102+
os.mkdir_all(os.join_path(project_dir, 'src'))!
103+
os.mkdir_all(os.join_path(project_dir, 'modules', 'somemoduletwo'))!
104+
os.write_file(os.join_path(project_dir, 'src', 'main.v'), 'module main
105+
import somemoduletwo
106+
107+
fn main() {
108+
println(somemoduletwo.name())
109+
}
110+
')!
111+
os.write_file(os.join_path(project_dir, 'modules', 'somemoduletwo', 'somemoduletwo.v'),
112+
'module somemoduletwo
113+
114+
pub fn name() string {
115+
return "somemoduletwo"
116+
}
117+
')!
118+
os.chdir(project_dir)!
119+
assert run_v_ok('${os.quoted_path(vexe)} run src').trim_space() == 'somemoduletwo'
120+
assert run_v_ok('${os.quoted_path(vexe)} run ./src').trim_space() == 'somemoduletwo'
121+
}

vlib/v/builder/compile.v

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module builder
66
import os
77
import v.pref
88
import v.util
9+
import v.vmod
910

1011
pub type FnBackend = fn (mut b Builder)
1112

@@ -227,33 +228,22 @@ pub fn (mut v Builder) set_module_lookup_paths() {
227228
// By default, these are what (3) contains:
228229
// 3.1) search in vlib/
229230
// 3.2) search in ~/.vmodules/ (i.e. modules installed with vpm)
231+
lookup_root := v.module_lookup_root()
230232
v.module_search_paths = []
231233
if v.pref.is_test {
232234
v.module_search_paths << os.dir(v.compiled_dir) // pdir of _test.v
233235
}
234-
v.module_search_paths << v.compiled_dir
235-
mut source_root := ''
236-
src_root := os.join_path(v.compiled_dir, 'src')
237-
if os.exists(src_root) && !v.pref.is_vsh {
238-
root_files := os.ls(v.compiled_dir) or { []string{} }
239-
if v.pref.should_compile_filtered_files(v.compiled_dir, root_files).len == 0 {
240-
source_root = src_root
241-
v.module_search_paths << source_root
242-
}
243-
}
244-
x := os.join_path(v.compiled_dir, 'modules')
236+
v.module_search_paths << lookup_root
237+
x := os.join_path(lookup_root, 'modules')
245238
if v.pref.is_verbose {
246239
println('x: "${x}"')
247240
}
248241

249-
if source_root != '' && os.exists(os.join_path(source_root, 'modules')) {
250-
v.module_search_paths << os.join_path(source_root, 'modules')
251-
}
252-
if source_root == '' && os.exists(os.join_path(v.compiled_dir, 'src/modules')) {
253-
v.module_search_paths << os.join_path(v.compiled_dir, 'src/modules')
242+
if os.exists(os.join_path(lookup_root, 'src/modules')) {
243+
v.module_search_paths << os.join_path(lookup_root, 'src/modules')
254244
}
255-
if os.exists(os.join_path(v.compiled_dir, 'modules')) {
256-
v.module_search_paths << os.join_path(v.compiled_dir, 'modules')
245+
if os.exists(os.join_path(lookup_root, 'modules')) {
246+
v.module_search_paths << os.join_path(lookup_root, 'modules')
257247
}
258248

259249
v.module_search_paths << v.pref.lookup_path
@@ -263,6 +253,27 @@ pub fn (mut v Builder) set_module_lookup_paths() {
263253
}
264254
}
265255

256+
fn (v &Builder) module_lookup_root() string {
257+
if os.file_name(v.compiled_dir) != 'src' {
258+
return v.compiled_dir
259+
}
260+
project_dir := os.dir(v.compiled_dir)
261+
if project_dir == v.compiled_dir {
262+
return v.compiled_dir
263+
}
264+
mut mcache := vmod.get_cache()
265+
if mcache.get_by_folder(v.compiled_dir).vmod_folder == project_dir {
266+
return project_dir
267+
}
268+
if os.real_path(os.getwd()) == project_dir {
269+
return project_dir
270+
}
271+
if os.is_dir(os.join_path(project_dir, 'modules')) {
272+
return project_dir
273+
}
274+
return v.compiled_dir
275+
}
276+
266277
pub fn (v Builder) get_builtin_files() []string {
267278
if v.pref.no_builtin {
268279
v.log('v.pref.no_builtin is true, get_builtin_files == []')

0 commit comments

Comments
 (0)