vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.76k stars 2.16k forks source link

$embed_file() not working properly #12090

Closed axetroy closed 2 years ago

axetroy commented 3 years ago

V version: V 0.2.4 77c18f4 OS: macOS

What did you do?

fn main () {
    embedded_file := $embed_file('./test.txt')

    println(embedded_file.to_string())
}

What did you expect to see?

# working like expect
v run main.v

# build
v -prod -os macos -m64 -o ./bin/test main.v
# remove fille, if not remove it, once removed, it won’t work
rm test.txt
./bin/test

What did you see instead?

runtime error

V panic: EmbedFileData error: files "./test.txt" and "/Users/axetroy/gpm/github.com/axetroy/prune/test.txt" do not exist
v hash: 77c18f4
0   prune                               0x0000000107dd81ab main + 2619
1   prune                               0x0000000107dc3194 start + 52
larpon commented 3 years ago

🤔 why the -os macos flag? Are you cross compiling? Sorry I haven't got my mac setup currently

JalonSolov commented 3 years ago

-os macos is useless (if it's even valid) if you're compiling on the mac, and won't work at all on anything else (mac can cross-compile to others, but others can't cross-compile to mac... stupid Apple).

axetroy commented 3 years ago

I found the -os flag in v help build-c

   -os <os>, -target-os <os>
      Change the target OS that V tries to compile for.
      By default, the target OS is the host system.
      When OS is `cross`, V will attempt to output cross-platform C code.

      Here is a list of the operating systems, supported by V:
      (CI tests runs on every commit/PR for each of these):
            `windows`, `linux`, `macos`

In order to be consistent with other terminals, I will bring these parameters.

Even if this parameter is invalid, it does not affect it.

The keypoint is that $embed_file() panic

JalonSolov commented 3 years ago

Yes, it does appear to be valid, and no it won't have any effect, but... it should be ok to specify it.

And yes, there appears to be a problem with $embed_file.

However, the error is that the file you're trying to embed doesn't exist. V should catch this and give a better error message, instead of letting C catch the error later. I don't know why the compilation doesn't fail, but running the compiled executable does - that should never happen.

larpon commented 3 years ago

No, the file is removed after compilation so the file should've been embedded in the binary and available inside the executable. I'm not sure what's up in this case.

JalonSolov commented 3 years ago

Yep... getting a compilation error at runtime is very weird.