Skip to content

Commit e3fda7d

Browse files
authored
checker: expand @VEXEROOT, @VMODROOT, DIR, $d(), $env() inside $embed_file(path) too (#26319)
1 parent 125564c commit e3fda7d

3 files changed

Lines changed: 48 additions & 41 deletions

File tree

‎vlib/v/checker/checker.v‎

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,47 +3003,7 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) {
30033003
if flag == 'flag' { // Checks for empty flag
30043004
c.error('no argument(s) provided for #flag', node.pos)
30053005
}
3006-
if flag.contains('@VROOT') {
3007-
// c.note(checker.vroot_is_deprecated_message, node.pos)
3008-
flag = util.resolve_vmodroot(flag.replace('@VROOT', '@VMODROOT'), c.file.path) or {
3009-
c.error(err.msg(), node.pos)
3010-
return
3011-
}
3012-
}
3013-
if flag.contains('@DIR') {
3014-
// expand `@DIR` to its absolute path
3015-
flag = flag.replace('@DIR', c.dir_path())
3016-
}
3017-
if flag.contains('@VEXEROOT') {
3018-
// expand `@VEXEROOT` to its absolute path
3019-
flag = flag.replace('@VEXEROOT', c.pref.vroot)
3020-
}
3021-
if flag.contains('@VMODROOT') {
3022-
flag = util.resolve_vmodroot(flag, c.file.path) or {
3023-
c.error(err.msg(), node.pos)
3024-
return
3025-
}
3026-
}
3027-
if flag.contains('\$env(') {
3028-
flag = util.resolve_env_value(flag, true) or {
3029-
c.error(err.msg(), node.pos)
3030-
return
3031-
}
3032-
}
3033-
if flag.contains('\$d(') {
3034-
flag = util.resolve_d_value(c.pref.compile_values, flag) or {
3035-
c.error(err.msg(), node.pos)
3036-
return
3037-
}
3038-
}
3039-
for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] {
3040-
if flag.contains(deprecated) {
3041-
if !flag.contains('@VMODROOT') {
3042-
c.error('${deprecated} had been deprecated, use @VMODROOT instead.',
3043-
node.pos)
3044-
}
3045-
}
3046-
}
3006+
flag = c.resolve_pseudo_variables(flag, node.pos) or { return }
30473007
c.table.parse_cflag(flag, c.mod, c.pref.compile_defines_all) or {
30483008
c.error(err.msg(), node.pos)
30493009
}
@@ -3065,6 +3025,45 @@ fn (mut c Checker) hash_stmt(mut node ast.HashStmt) {
30653025
}
30663026
}
30673027

3028+
fn (mut c Checker) resolve_pseudo_variables(oflag string, pos token.Pos) ?string {
3029+
mut flag := oflag
3030+
if flag.contains('@VEXEROOT') {
3031+
// expand `@VEXEROOT` to its absolute path
3032+
flag = flag.replace('@VEXEROOT', c.pref.vroot)
3033+
}
3034+
if flag.contains('@VMODROOT') {
3035+
flag = util.resolve_vmodroot(flag, c.file.path) or {
3036+
c.error(err.msg(), pos)
3037+
return none
3038+
}
3039+
}
3040+
if flag.contains('@DIR') {
3041+
// expand `@DIR` to its absolute path
3042+
flag = flag.replace('@DIR', c.dir_path())
3043+
}
3044+
if flag.contains('\$env(') {
3045+
flag = util.resolve_env_value(flag, true) or {
3046+
c.error(err.msg(), pos)
3047+
return none
3048+
}
3049+
}
3050+
if flag.contains('\$d(') {
3051+
flag = util.resolve_d_value(c.pref.compile_values, flag) or {
3052+
c.error(err.msg(), pos)
3053+
return none
3054+
}
3055+
}
3056+
for deprecated in ['@VMOD', '@VMODULE', '@VPATH', '@VLIB_PATH'] {
3057+
if flag.contains(deprecated) {
3058+
if !flag.contains('@VMODROOT') {
3059+
c.error('${deprecated} had been deprecated, use @VMODROOT instead.', pos)
3060+
return none
3061+
}
3062+
}
3063+
}
3064+
return flag
3065+
}
3066+
30683067
fn (mut c Checker) import_stmt(node ast.Import) {
30693068
if node.mod == 'x.vweb' && !c.shown_xvweb_deprecation {
30703069
println('`x.vweb` is now `veb`. The module is no longer experimental. Simply `import veb` instead of `import x.vweb`.')

‎vlib/v/checker/comptime.v‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
6161
node.pos)
6262
return ast.string_type
6363
}
64+
escaped_path = c.resolve_pseudo_variables(escaped_path, node.pos) or {
65+
return ast.string_type
66+
}
6467
abs_path := os.real_path(escaped_path)
6568
// check absolute path first
6669
if !os.exists(abs_path) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn test_logo_can_be_embedded_using_a_path_with_vexeroot() {
2+
logo := $embed_file('@VEXEROOT/examples/assets/logo.png')
3+
assert unsafe { logo.data().vbytes(4) } == [u8(0x89), `P`, `N`, `G`]
4+
assert logo.len > 1000
5+
}

0 commit comments

Comments
 (0)