Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DocsRS build fails when dependency requires building build info #21

Open
lexi-the-cute opened this issue Mar 13, 2024 · 6 comments
Open

Comments

@lexi-the-cute
Copy link

rust-lang/docs.rs#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
Copy link
Author

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

@lexi-the-cute
Copy link
Author

using this build script, i managed to isolate the problem to

#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
Copy link
Author

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
Copy link
Author

lexi-the-cute commented Mar 17, 2024

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}");
    }
}

@lexi-the-cute
Copy link
Author

this appears to no longer be relevant as build-info now appears to work without having to fake the data

@lexi-the-cute
Copy link
Author

Turns out I was wrong. Apparently the build-info-build crate still requires an internet connection according to my docs.rs build

[INFO] [stderr] --- stderr
[INFO] [stderr] thread 'main' panicked at /opt/rustwide/cargo-home/registry/src/index.crates.io-6f17d22bba15001f/build-info-build-0.0.39/src/build_script_options/crate_info.rs:101:10:
[INFO] [stderr] called Result::unwrap() on an Err value: CargoMetadata { stderr: " Updating crates.io index\nwarning: spurious network error (3 tries remaining): [6] Could not resolve hostname (Could not resolve host: index.crates.io)\nwarning: spurious network error (2 tries remaining): [6] Could not resolve hostname (Could not resolve host: index.crates.io)\nwarning: spurious network error (1 tries remaining): [6] Could not resolve hostname (Could not resolve host: index.crates.io)\nerror: failed to get build-info as a dependency of package catgirl-engine-server v0.14.39 (/opt/rustwide/cargo-home/registry/src/index.crates.io-6f17d22bba15001f/catgirl-engine-server-0.14.39)\n\nCaused by:\n download of config.json failed\n\nCaused by:\n failed to download from https://index.crates.io/config.json\n\nCaused by:\n [6] Could not resolve hostname (Could not resolve host: index.crates.io)\n" }

@lexi-the-cute lexi-the-cute reopened this Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant