From e3fda7d7e77fe29404d192ca0af6f3e84ba8ccb8 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sun, 11 Jan 2026 09:36:42 +0200 Subject: [PATCH] checker: expand @VEXEROOT, @VMODROOT, DIR, $d(), $env() inside $embed_file(path) too (#26319) --- vlib/v/checker/checker.v | 81 +++++++++---------- vlib/v/checker/comptime.v | 3 + .../tests/embed_v_logo_using_vexeroot_test.v | 5 ++ 3 files changed, 48 insertions(+), 41 deletions(-) create mode 100644 vlib/v/embed_file/tests/embed_v_logo_using_vexeroot_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 43d277f3e..46cab1265 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3003,47 +3003,7 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) { if flag == 'flag' { // Checks for empty flag c.error('no argument(s) provided for #flag', node.pos) } - if flag.contains('@VROOT') { - // c.note(checker.vroot_is_deprecated_message, node.pos) - flag = util.resolve_vmodroot(flag.replace('@VROOT', '@VMODROOT'), c.file.path) or { - c.error(err.msg(), node.pos) - return - } - } - if flag.contains('@DIR') { - // expand `@DIR` to its absolute path - flag = flag.replace('@DIR', c.dir_path()) - } - if flag.contains('@VEXEROOT') { - // expand `@VEXEROOT` to its absolute path - flag = flag.replace('@VEXEROOT', c.pref.vroot) - } - if flag.contains('@VMODROOT') { - flag = util.resolve_vmodroot(flag, c.file.path) or { - c.error(err.msg(), node.pos) - return - } - } - if flag.contains('\$env(') { - flag = util.resolve_env_value(flag, true) or { - c.error(err.msg(), node.pos) - return - } - } - if flag.contains('\$d(') { - flag = util.resolve_d_value(c.pref.compile_values, flag) or { - c.error(err.msg(), node.pos) - return - } - } - for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] { - if flag.contains(deprecated) { - if !flag.contains('@VMODROOT') { - c.error('${deprecated} had been deprecated, use @VMODROOT instead.', - node.pos) - } - } - } + flag = c.resolve_pseudo_variables(flag, node.pos) or { return } c.table.parse_cflag(flag, c.mod, c.pref.compile_defines_all) or { c.error(err.msg(), node.pos) } @@ -3065,6 +3025,45 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) { } } +fn (mut c Checker) resolve_pseudo_variables(oflag string, pos token.Pos) ?string { + mut flag := oflag + if flag.contains('@VEXEROOT') { + // expand `@VEXEROOT` to its absolute path + flag = flag.replace('@VEXEROOT', c.pref.vroot) + } + if flag.contains('@VMODROOT') { + flag = util.resolve_vmodroot(flag, c.file.path) or { + c.error(err.msg(), pos) + return none + } + } + if flag.contains('@DIR') { + // expand `@DIR` to its absolute path + flag = flag.replace('@DIR', c.dir_path()) + } + if flag.contains('\$env(') { + flag = util.resolve_env_value(flag, true) or { + c.error(err.msg(), pos) + return none + } + } + if flag.contains('\$d(') { + flag = util.resolve_d_value(c.pref.compile_values, flag) or { + c.error(err.msg(), pos) + return none + } + } + for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] { + if flag.contains(deprecated) { + if !flag.contains('@VMODROOT') { + c.error('${deprecated} had been deprecated, use @VMODROOT instead.', pos) + return none + } + } + } + return flag +} + fn (mut c Checker) import_stmt(node ast.Import) { if node.mod == 'x.vweb' && !c.shown_xvweb_deprecation { println('`x.vweb` is now `veb`. The module is no longer experimental. Simply `import veb` instead of `import x.vweb`.') diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 29eb015a3..68a42b15a 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -61,6 +61,9 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type { node.pos) return ast.string_type } + escaped_path = c.resolve_pseudo_variables(escaped_path, node.pos) or { + return ast.string_type + } abs_path := os.real_path(escaped_path) // check absolute path first if !os.exists(abs_path) { diff --git a/vlib/v/embed_file/tests/embed_v_logo_using_vexeroot_test.v b/vlib/v/embed_file/tests/embed_v_logo_using_vexeroot_test.v new file mode 100644 index 000000000..b726a093b --- /dev/null +++ b/vlib/v/embed_file/tests/embed_v_logo_using_vexeroot_test.v @@ -0,0 +1,5 @@ +fn test_logo_can_be_embedded_using_a_path_with_vexeroot() { + logo := $embed_file('@VEXEROOT/examples/assets/logo.png') + assert unsafe { logo.data().vbytes(4) } == [u8(0x89), `P`, `N`, `G`] + assert logo.len > 1000 +} -- 2.39.5