Cargo autogenerated code: not work except

I tried to use cargo code generation as described cargo study code generation, it works in such case:

name = "hello_world"
build = ""

where contains:

    let src = Path::new("src/");
    let dst = Path::new(&env::var("OUT_DIR").unwrap()).join("");

    registry.expand("hello_world", &src, &dst).unwrap();

all works fine, cargo found and compile generated

But when I try library instead of binary:
name = "hello_world"
build = ""

name = "test"
crate-type = ["cdylib"]

and replace with compilation failed,
the problem is that cargo not search in "target" directory,

why so and how can I fix this?

Hm, I would say that the first case should not work as well. The basic rule is that you can't generate or files directly because cargo needs them to determine possible builds commands before executing the build script. Here is the recent discussion of this phenomenon: won't run if if missing · Issue #3208 · rust-lang/cargo · GitHub.

So perhaps you need a hand written like this:

include!(concat!(env!("OUT_DIR"), "/"));

and a build script like this:

let out_dir = env::var_os("OUT_DIR").unwrap();
let dst = Path::new(&out_dir).join("");