fix(prepare): store temporary query files inside the workspace #2398
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves #2395
Thanks to @cycraig for the advice here: #2395 (comment)
The PR to implement one file per query: #2363 implemented logic to write queries to a temp file and then persist them to
SQLX_OFFLINE_DIR
here:sqlx/sqlx-macros-core/src/query/data.rs
Lines 163 to 170 in c03926c
However, the docs for
NamedTempFile::persist
mention the following:On Linux, the
/tmp
directory is commonly mounted astmpfs
which means the call totmp_file.persist
will result in the following error:failed to move query file: PersistError(Os { code: 18, kind: CrossesDevices, message: "Invalid cross-device link" })
.To prevent this, we can't rely on using the default temp directory. This PR changes the logic to use
NamedTempFile::new_in
to create the temporary files in a new variable calledSQLX_TMP
. When run fromcargo sqlx prepare
, this is set to$TARGET_DIR/sqlx-tmp
.The point of the
SQLX_TMP
variable is to ensure the temporary files are written to a directory that isn't picked up by git. As long as everything completes successfully, this shouldn't really matter as the temp files will get cleaned up automatically on a clean exit. Ideally, we wouldn't need this and could instead just reference the target directory directly, but that would require a call tocargo metadata
fromsqlx-macros-core
.I'm not too familiar with this logic so please let me know if there's a better way to handle this. Just thought I'd take a stab at it, thanks!