`env!` / `include!` / `include_str!` do not end up in dep-info entries resulting in stale artifacts under rustc-wrapper caches like sccache

オープン
#29 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
静か
技術スタック
rust

調査の方向性

github.com/leighmcculloch/rust-sccache-env-proc-macro-cache-collision の Docker reproducer から始め、変更した環境値にわたって env!、include!、include_str! の dep-info エントリを比較します。const { let _ = #raw } workaround を含め、MacroString 入力の評価と proc-macro 展開パスを読みます。期待される dep-info の動作が確立され、安全な MacroString の設計変更があればその範囲が明確に定義され、reproducer でカバーされていれば完了です。

索引モデルが issue の本文から書いたものです。

説明

I've been debugging a situation with a procmacro that uses macro-string where changes to an environment variable used in it via env! and passed through a macro-string resulted in stale build artifacts rather than the latest environment value being embedded.

pub const GIT_REVISION: &str = mymacrolib::baked!(env!("GIT_REVISION"));

I created a Docker image that reproduces the problem with sccache setup, and a repo with a procmacro using macro-string:

My understanding so far is that this is happening because macro-string evaluates and expands the env! within the procmacro meaning that rustc never sees the env! in the crate and so is unaware that it is a dependency and the env value doesn't get included in the build artifact's dep-info. When rebuilding the build artifact with a different env! sccache will return the earlier cached artifact because it cannot distinguish between them since the env is not included in the dep-info.

As a work-around I've found that having my procmacro emit a const { let _ = #input } can resurface the env! back in the context of the crate unexpanded. For example:

#[proc_macro]
pub fn baked(input: TokenStream) -> TokenStream {
    let raw = proc_macro2::TokenStream::from(input.clone());
    let macro_string = parse_macro_input!(input as MacroString);
    let s = macro_string.eval().unwrap();
    let lit = proc_macro2::Literal::string(&s);
    quote! {
        {
            const { let _ = #raw; };  // surface env!()/include!() to rustc's dep-info
            #lit
        }
    }
    .into()
}

Is the dep-info loss of env!/include!/include_str! expected / known issue?

Because macro-string is an embedded detail in macros it can be difficult to know if it is safe to use sccache for a build or not with a rust library, because internally or somewhere in the dependency tree it may be in use in a way that poisons the cache.

Would it be reasonable for MacroString to somehow encourage re-exporting of the input as a way to reduce the occurrence of this bug? It's not obvious to me how this would be achieved so I expect my question is naive at best.

主要言語
Rust
スター
46
フォーク
2
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。