Closed MinisculeGirraffe closed 1 year ago
This might sound dumb, but I just replaced that line in the build script with the recommendation from the compiler error message and that fixed it.
I'm able to use the "empty" prisma-codegen crate without having to write any files to /src
, and the contents automatically get re-generated whenever I modify the schema file.
main.rs
from the application workspace crate:
use prisma_codegen::{new_client_with_url, PrismaClient};
use color_eyre::Result;
/// Create a new prisma client, and apply any pending migrations
async fn prisma_client(url: &str) -> Result<PrismaClient> {
let client = new_client_with_url(url).await?;
#[cfg(debug_assertions)]
client._db_push().await?;
#[cfg(not(debug_assertions))]
client._migrate_deploy().await?;
Ok(client)
}
build.rs
after modifications:
use std::io::{BufRead, BufReader};
use std::{env, path::Path, process::Command};
use std::{fs, fs::File};
fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let codegen_dest_path = Path::new(&out_dir).join("generated.rs");
env::set_var("PRISMA_OUT_FILE", &codegen_dest_path);
env::set_var("CARGO_TARGET_DIR", &out_dir);
let output = Command::new("cargo")
.args(&["run", "-p", "prisma-cli", "generate"])
.output()
.expect("Failed run to codegen command");
let mut lines = lines_from_file(&codegen_dest_path);
for line in &mut lines {
if line.contains("#!") {
*line = line.replace("#!", "#");
}
}
fs::write(&codegen_dest_path, lines.join("\n")).expect("Failed to update file");
println!("cargo:rerun-if-changed=/prisma");
}
fn lines_from_file(filename: impl AsRef<Path>) -> Vec<String> {
let file = File::open(filename).expect("no such file");
let buf = BufReader::new(file);
buf.lines()
.map(|l| l.expect("Could not parse line"))
.collect()
}
Huh, never knew that include!
worked like that. I also never knew that if you put #[allow(...)]
on a mod ...;
declaration it'll do the same as the inner attributes I'm currently using.
I think I'm going to get rid of them all together and recommend that users add the attributes themselves, this way you can either add them on the mod ...;
or inject them at the top of the file in your build script kind of like the text replacement you're already doing.
FWIW I've had absolutely zero issues with the build script since then. I think it's the preferable way to go since every cargo-check will make sure you've got the latest codegen from the prisma schema. One less thing to worry about being fragile.
Appreciate the change. Hopefully I can remove that extra bit from the build script 😅.
Howdy.
I was working on testing running code-generation in a build script, which I feel is preferable for multiple reasons. You can have the code automatically re-run at build time every time a file in the /prisma folder changes. With the benefit of it not being included in your actual /src folder so there's no way you can git commit or modify the file that gets code-generated.
I've got two crates in my workspace:
prisma-cli
: The standard CLI setup from the docs.prisma-codegen
: A lib crate containing the/prisma/schema.prisma
file and a build.rs script.In the build script, we're executing the prisma-cli command to codegen, and setting an env variable to change the output of the generated file to be in the
/target
directory.build.rs
schema.prisma
lib.rs
This all works. The problem is that the generated code uses
#![allow(warnings, unused)]
which isn't allowed when using aninclude!
to bring the generated code into our libraries context. Compiler error for reference:Seems like this issue has happened with other projects that use code generation as well
https://github.com/google/flatbuffers/issues/6261
I think the fix would be to just replace it.