`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 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
冷清
技术栈
rust

调研方向

从 github.com/leighmcculloch/rust-sccache-env-proc-macro-cache-collision 中的 Docker reproducer 开始,并比较环境值发生变化时 env!、include! 和 include_str! 的 dep-info 条目。阅读 MacroString 输入求值和 proc-macro 展开路径,包括 const { let _ = #raw } workaround。当预期的 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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。