builder: show file:line when import fails (#8537)
							parent
							
								
									112c652ace
								
							
						
					
					
						commit
						27239db427
					
				| 
						 | 
				
			
			@ -2,6 +2,7 @@ module builder
 | 
			
		|||
 | 
			
		||||
import os
 | 
			
		||||
import v.ast
 | 
			
		||||
import v.token
 | 
			
		||||
import v.table
 | 
			
		||||
import v.pref
 | 
			
		||||
import v.util
 | 
			
		||||
| 
						 | 
				
			
			@ -97,7 +98,7 @@ pub fn (mut b Builder) parse_imports() {
 | 
			
		|||
		for imp in ast_file.imports {
 | 
			
		||||
			mod := imp.mod
 | 
			
		||||
			if mod == 'builtin' {
 | 
			
		||||
				verror('cannot import module "builtin"')
 | 
			
		||||
				error_with_pos('cannot import module "builtin"', ast_file.path, imp.pos)
 | 
			
		||||
				break
 | 
			
		||||
			}
 | 
			
		||||
			if mod in done_imports {
 | 
			
		||||
| 
						 | 
				
			
			@ -106,20 +107,23 @@ pub fn (mut b Builder) parse_imports() {
 | 
			
		|||
			import_path := b.find_module_path(mod, ast_file.path) or {
 | 
			
		||||
				// v.parsers[i].error_with_token_index('cannot import module "$mod" (not found)', v.parsers[i].import_table.get_import_tok_idx(mod))
 | 
			
		||||
				// break
 | 
			
		||||
				verror('cannot import module "$mod" (not found)')
 | 
			
		||||
				error_with_pos('cannot import module "$mod" (not found)', ast_file.path,
 | 
			
		||||
					imp.pos)
 | 
			
		||||
				break
 | 
			
		||||
			}
 | 
			
		||||
			v_files := b.v_files_from_dir(import_path)
 | 
			
		||||
			if v_files.len == 0 {
 | 
			
		||||
				// v.parsers[i].error_with_token_index('cannot import module "$mod" (no .v files in "$import_path")', v.parsers[i].import_table.get_import_tok_idx(mod))
 | 
			
		||||
				verror('cannot import module "$mod" (no .v files in "$import_path")')
 | 
			
		||||
				error_with_pos('cannot import module "$mod" (no .v files in "$import_path")',
 | 
			
		||||
					ast_file.path, imp.pos)
 | 
			
		||||
			}
 | 
			
		||||
			// Add all imports referenced by these libs
 | 
			
		||||
			parsed_files := parser.parse_files(v_files, b.table, b.pref, b.global_scope)
 | 
			
		||||
			for file in parsed_files {
 | 
			
		||||
				if file.mod.name != mod {
 | 
			
		||||
					// v.parsers[pidx].error_with_token_index('bad module definition: ${v.parsers[pidx].file_path} imports module "$mod" but $file is defined as module `$p_mod`', 1
 | 
			
		||||
					verror('bad module definition: $ast_file.path imports module "$mod" but $file.path is defined as module `$file.mod.name`')
 | 
			
		||||
					error_with_pos('bad module definition: $ast_file.path imports module "$mod" but $file.path is defined as module `$file.mod.name`',
 | 
			
		||||
						ast_file.path, imp.pos)
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			b.parsed_files << parsed_files
 | 
			
		||||
| 
						 | 
				
			
			@ -390,6 +394,12 @@ struct FunctionRedefinition {
 | 
			
		|||
	f       ast.FnDecl
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn error_with_pos(s string, fpath string, pos token.Position) {
 | 
			
		||||
	ferror := util.formatted_error('builder error:', s, fpath, pos)
 | 
			
		||||
	eprintln(ferror)
 | 
			
		||||
	exit(1)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn verror(s string) {
 | 
			
		||||
	util.verror('builder error', s)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1 +1,5 @@
 | 
			
		|||
builder error: cannot import module "notexist" (not found)
 | 
			
		||||
vlib/v/checker/tests/import_not_found_err.vv:1:1: builder error: cannot import module "notexist" (not found)
 | 
			
		||||
    1 | import notexist
 | 
			
		||||
      | ~~~~~~~~~~~~~~~
 | 
			
		||||
    2 | fn main() {
 | 
			
		||||
    3 |     println(notexist.name)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,7 +20,7 @@ fn test_compiling_without_vmodules_fails() {
 | 
			
		|||
	os.setenv('VMODULES', '', true)
 | 
			
		||||
	res := os.exec('"$vexe" run "$mainvv"') or { panic(err) }
 | 
			
		||||
	assert res.exit_code == 1
 | 
			
		||||
	assert res.output.trim_space() == 'builder error: cannot import module "yyy" (not found)'
 | 
			
		||||
	assert res.output.trim_space().contains('builder error: cannot import module "yyy" (not found)')
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn test_compiling_with_vmodules_works() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,7 @@
 | 
			
		|||
import v.tests.modules.acommentedmodule
 | 
			
		||||
===output===
 | 
			
		||||
builder error: bad module definition: .entire_commented_module.repl.vrepl_temp.v imports module "v.tests.modules.acommentedmodule" but vlib/v/tests/modules/acommentedmodule/commentedfile.v is defined as module `main`
 | 
			
		||||
    1 | import v.tests.modules.acommentedmodule
 | 
			
		||||
      | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 | 
			
		||||
    2 | import os
 | 
			
		||||
    3 |
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -63,12 +63,12 @@ pub fn run_repl_file(wd string, vexec string, file string) ?string {
 | 
			
		|||
		file_expected := '${file}.expected.txt'
 | 
			
		||||
		os.write_file(file_result, result) or { panic(err) }
 | 
			
		||||
		os.write_file(file_expected, output) or { panic(err) }
 | 
			
		||||
		diff := diff_files(file_result, file_expected)
 | 
			
		||||
		diff := diff_files(file_expected, file_result)
 | 
			
		||||
		return error('Difference found in REPL file: $file
 | 
			
		||||
====> Got      :
 | 
			
		||||
|$result|
 | 
			
		||||
====> Expected :
 | 
			
		||||
|$output|
 | 
			
		||||
====> Got      :
 | 
			
		||||
|$result|
 | 
			
		||||
====> Diff     :
 | 
			
		||||
$diff
 | 
			
		||||
		')
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue