move-language / move

Apache License 2.0
2.24k stars 677 forks source link

[Bug] Abi cannot be generated when the script function name is different from the file name #1037

Open jewelzms opened 1 year ago

jewelzms commented 1 year ago

🐛 Bug

Description

  1. Abi cannot be generated when the script function name is different from the file name
  2. When multiple functions exist in one file, all function ABI content is the ABI content of the function with the same file name.

To reproduce

build generate ABI

# move build --abi

sample code filename: run_hello.move, function: main

// scripts/run_hello.move
script {
    use std::debug;
    fun main() {
        debug::print<u8>(&10);
    }
}

Stack trace/error message

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', language/move-prover/move-abigen/src/abigen.rs:238:20
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected Behavior

Generate ABI file successfully, path: build/hello_world/abis/main.abi

Reason

Wrong way to get function bytecode by script file name, the script function bytecode should be obtained using the module name.

Error code segment

// language/move-prover/move-abigen/src/abigen.rs
    fn load_compiled_bytes(&self, module_env: &ModuleEnv<'env>) -> anyhow::Result<Vec<u8>> {
        match &self.options.in_memory_bytes {
            Some(map) => {
                let path =
                    PathBuf::from(module_env.get_source_path().to_string_lossy().to_string())
                        .file_stem()
                        .expect("file stem")
                        .to_string_lossy()
                        .to_string();
                Ok(map.get(&path).unwrap().clone())
        ...
    }

Fix suggestion

    fn load_compiled_bytes(&self, module_env: &ModuleEnv<'env>) -> anyhow::Result<Vec<u8>> {
        match &self.options.in_memory_bytes {
            Some(map) => {
                let name = module_env.get_full_name_str();
                Ok(map.get(&name).unwrap().clone())
            }
        ...
    }
ksolana commented 10 months ago

can you provide a full reproducer. how did you point run_hello.move file to the move compiler?