danielschemmel / build-info

Collects build-information of your Rust crate
https://crates.io/crates/build-info
24 stars 11 forks source link

DocsRS build fails when dependency requires building build info #21

Open lexi-the-cute opened 7 months ago

lexi-the-cute commented 7 months ago

https://github.com/rust-lang/docs.rs/issues/2454

When building for DocsRS, the build fails for all crates that require building the build info for dependencies. For example, catgirl-engine, catgirl-engine-client, and catgirl-engine-server all depend on other crates to build the build-info, however, catgirl-engine-utils does not require building a dependency with build-info and therefore compiles successfully.

When it comes to DocsRS, they don't enable network access and I don't need the build-info itself to actually be built for the purpose of docs, I just need it to generate the function, but keep the data empty when the DOCS_RS environment variable is detected.

Is there a way to tell build-info-build to pseudo build the build dependencies so it'll succeed regardless of having access to the network?

lexi-the-cute commented 7 months ago

this is the failed build log for catgirl-engine https://docs.rs/crate/catgirl-engine/0.12.7/builds/1155878

lexi-the-cute commented 7 months ago

using this build script, i managed to isolate the problem to https://github.com/danielschemmel/build-info/blob/6039911685b74a543caf46fc5cfcbbf392f7efea/build-info-build/src/build_script_options/crate_info.rs#L101

#echo "Delete target directory"
#rm -r target

# Comment Out If Building From Source
rm -r catgirl-engine-*
echo "Downloading catgirl-engine"
# cargo install cargo-dl
cargo dl -e catgirl-engine
cd catgirl-engine-*

export CARGO_HOME=$(mktemp -d)
echo "CARGO_HOME is set to $CARGO_HOME"

echo "Fetching dependencies"
cargo fetch

echo "Creating blank target directory"
mkdir target

echo "Building project in isolation"
export RUST_BACKTRACE=full
export CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true
bwrap --ro-bind / / --dev /dev --tmpfs /tmp --ro-bind $PWD $PWD --ro-bind $CARGO_HOME $CARGO_HOME --bind $PWD/target $PWD/target --unshare-net -- cargo rustdoc
lexi-the-cute commented 7 months ago

i tried using feature flags to disable this for DOCS_RS only, but it seems the DOCS_RS flag gets ignored when compiling a dependency reliant on build-info. https://docs.rs/crate/catgirl-engine-client/0.12.13/builds/1160595

lexi-the-cute commented 7 months ago

i managed to get around this issue by faking the BUILD_INFO data

use build_info_build::DependencyDepth;
use std::env;

fn main() {
    // Generate build info
    generate_build_info();
}

fn matches_environment_var(key: &str, value: &str) -> bool {
    let environment_var: Result<String, env::VarError> = env::var(key);
    environment_var.is_ok() && environment_var.unwrap() == value
}

fn generate_build_info() {
    let mut depth: DependencyDepth = DependencyDepth::Depth(0);

    // Track environment for rebuilds
    println!("cargo:rerun-if-env-changed=RUST_ANALYZER");
    println!("cargo:rerun-if-env-changed=DOCS_RS");

    // Custom environment variable to speed up writing code
    let rust_analyzer: bool = matches_environment_var("RUST_ANALYZER", "true");
    let docs_rs: bool = env::var("DOCS_RS").is_ok();
    if rust_analyzer || docs_rs {
        depth = DependencyDepth::None;
    }

    if !docs_rs {
        build_info_build::build_script().collect_runtime_dependencies(depth);
    } else {
        // Waiting for https://github.com/danielschemmel/build-info/pull/22
        let fake_data: &str = "{\"version\":\"0.0.36\",\"string\":\"KLUv/QCIfQUAYgkfGVDVAwMdwRLXXHpu1nWhFFma/2dL1xlougUumP6+APJ9j7KUcySnJLNNYnIltvVKqeC/kGIndHF1BHBIK4wv5CwLsGwLAIbYKL23nt62NWU9rV260vtN+lC7Gc6hQ88VJDnBTTvK2A2OlclP+nFC6Qv9pXpT45P+5vu7IxUg8C5MIG6uRGrJdMrMEWkifBPLCOMAwA1Yz4S7cwMRQhcZnAnHBXwkhgMFxxsKFg==\"}";
        println!("cargo:rustc-env=BUILD_INFO={fake_data}");
    }
}