cornucopia-rs / cornucopia

Generate type-checked Rust from your PostgreSQL.
Other
835 stars 38 forks source link

unresolved import `cornucopia` #158

Closed iodapson closed 2 years ago

iodapson commented 2 years ago

The following error message occurs on VS Code: use of undeclared crate or module cornucopiarustcE0432

The above error was traced back to a 'build.rs' file with code to auto build for a docker container

LouisGariepy commented 2 years ago

Hi @iodapson,

Would it be possible to provide a repo link? Its hard to find a solution without seeing the code :)

If not, here are some questions:

iodapson commented 2 years ago

Okay, sure. Here and thanks in advance

LouisGariepy commented 2 years ago

Thanks for providing the info :smile:

Right, so from looking at your build.rs, your cornucopia module will be generated in src/cornucopia.rs, but your main.rs file doesn't actually include that module (there's no mod cornucopia;). Instead, you use include!(concat!(env!("OUT_DIR"), "/cornucopia.rs"));, which is presumably not what you want ("OUT_DIR" will typically be the target folder, not src).

What I would recommend is to

  1. Remove the line include!(concat!(env!("OUT_DIR"), "/cornucopia.rs")); from main.rs
  2. Make sure your cornucopia.rs is generated correctly (according to your build.rs file, it should be generated in src/cornucopia.rs, make sure its there).
  3. Add mod cornucopia; to your main.rs

Otherwise, you can follow the rust-on-nails-demo project's way of doing things:

  1. Leave your main.rs as is.
  2. Use something like
    let destination = env::var_os("OUT_DIR").unwrap();

    and use that destination in generate_managed.

The difference between the two methods is that in the first method, your generated cornucopia.rs file is part of your project as a regular module. In the second method, it is generated as a build artifact in the target folder (i.e. not directly part of your project).

Please let me know if you need further assistance.

iodapson commented 2 years ago

I have tried both ways but the error still persists. It is as if cargo (and bare rustc) can't resolve cornucopia's existence at crates.io Maybe there could be some other way to point the code to corunucopia's source code or something. Or maybe I'm doing something just outright wrong.

LouisGariepy commented 2 years ago

The cornucopia crate is not found in your build.rs because you need a build-dependencies section in your Cargo.toml. Like so:

[build-dependencies]
cornucopia = "0.8.1"

But more importantly, the demo, as found here https://github.com/purton-tech/rust-on-nails/blob/main/examples/rust-on-nails-demo/app/Cargo.toml, is using a deprecated version of Cornucopia, while you are trying to use the latest version. This will lead to many problems down the line.

I would highly suggest you use this as a base instead https://github.com/cornucopia-rs/cornucopia/tree/main/examples/auto_build. (Read the README!). This is the official way of working with Cornucopia auto-build, and is verified to work out-of-the-box. It shouldn't be too hard to adapt this to the rust-on-nails-demo. NOTE: You'll need to change the local paths in the Cargo.toml to version numbers instead.

In any case I'll try to open a PR on Ian Purton's repo to update the demos.

iodapson commented 2 years ago

Ah incredible! That worked. For some odd reason I tried [dev-dependencies] instead of [build-dependencies]. Thank you.

LouisGariepy commented 2 years ago

@iodapson No problem! Thanks for using Cornucopia :smile: I'll close the issue for now, but don't hesitate if you need further assistance.

iodapson commented 2 years ago

Alright 😁

iodapson commented 2 years ago

Thanks for building Cornucopia.