Skip to content

Commit 069cfca

Browse files
authored
Update FAQ to include building on docs.rs (#1497)
Thanks to @jplatte in the sqlx Discord for providing this solution.
1 parent 62b57f0 commit 069cfca

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

FAQ.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,20 @@ for _every_ database we intend to support.
181181

182182
Even Sisyphus would pity us.
183183

184-
---------
184+
----
185+
186+
### Why does my project using sqlx query macros not build on docs.rs?
187+
188+
Docs.rs doesn't have access to your database, so it needs to be provided a `sqlx-data.json` file and be instructed to set the `SQLX_OFFLINE` environment variable to true while compiling your project. Luckily for us, docs.rs creates a `DOCS_RS` environment variable that we can access in a custom build script to achieve this functionality.
189+
190+
To do so, first, make sure that you have run `cargo sqlx prepare` to generate a `sqlx-data.json` file in your project.
191+
192+
Next, create a file called `build.rs` in the root of your project directory (at the same level as `Cargo.toml`). Add the following code to it:
193+
```rs
194+
fn main() {
195+
// When building in docs.rs, we want to set SQLX_OFFLINE mode to true
196+
if std::env::var_os("DOCS_RS").is_some() {
197+
println!("cargo:rustc-env=SQLX_OFFLINE=true");
198+
}
199+
}
200+
```

0 commit comments

Comments
 (0)