diesel-rs / diesel

A safe, extensible ORM and Query Builder for Rust
https://diesel.rs
Apache License 2.0
12.53k stars 1.05k forks source link

Add `directives` option to diesel.toml to allow for schema.rs directives #2004

Open smithwinston opened 5 years ago

smithwinston commented 5 years ago

Proposal

Add a new directives option to diesel.toml to allow for compiler directives to be injected into schema.rs during infer_schema:

directives = [ "#![allow(unused_imports)]" ]

Background

It seems that many people end up with warnings about unused imports when building; the current solution is to create a schema.patch file as follows:

--- /tmp/schema.rs  2019-03-04 09:58:14.000000000 -0000
+++ src/schema.rs   2019-03-04 09:58:19.000000000 -0000
@@ -1,3 +1,5 @@
+#![allow(unused_imports)]
+
 table! {
     use diesel::sql_types::*;
     use crate::models::*;

And include in the diesel.toml as follow:

patch_file = "src/schema.patch"

As per:
https://gitter.im/diesel-rs/diesel?at=5c74378a66e2b3118b186be7

However, when reverting migrations, when the schema.rs file becomes empty, errors are reported because it cannot apply the patch to an empty file:

Rolling back migration 00000000000000_diesel_initial_setup
Failed to apply schema patch. stdout: patching file src/schema.rs
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file src/schema.rs.rej
 stderr:

While these errors don't matter (aside from littering the directory with .orig and .rej files), it could break automation / scripting as the resulting value of $? after the diesel migration revert is now 1 instead of 0 indicating success.

sgrif commented 5 years ago

If nothing else, we should definitely fix the empty schema file issue.

malobre commented 3 years ago

FYI, I ran in the same problem (unused_imports warning with custom import_types). I fixed it by using a patch file, like suggested, but I manually wrote a "cleaner" one:

--- src/schema.rs
+++ src/schema.rs
@@ -0,0 +0,2 @@
+#![allow(unused_imports)]
+

I customized the patch file a little more to add some comments about the file being auto-generated, it's working great !

jshxr commented 2 years ago

Not sure if this covers all cases, but adding a module-scoped lint to the module declaration like so

// lib.rs

// ...
#[allow(unused_imports)]
mod schema;
// ...

solved the issue for me without the need to edit schema.rs at all.

weiznich commented 2 years ago

@jshxr Yes that's the correct solution for any lint showing warnings inside of schema.rs. The other issue mentioned in the bug report remains unfixed and should probably be fixed. PR's are welcome there.