diesel icon indicating copy to clipboard operation
diesel copied to clipboard

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

Open smithwinston opened this issue 5 years ago • 4 comments

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.

smithwinston avatar Mar 05 '19 16:03 smithwinston

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

sgrif avatar Mar 07 '19 22:03 sgrif

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 !

malobre avatar Jun 03 '21 16:06 malobre

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.

jshxr avatar Apr 05 '22 14:04 jshxr

@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.

weiznich avatar Apr 05 '22 16:04 weiznich