-
-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Inner Attributes makes it impossible to include code generation from build script. #306
Comments
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
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)
}
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 |
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
google/flatbuffers#6261
I think the fix would be to just replace it.
The text was updated successfully, but these errors were encountered: