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

feature: faster self-install without rate limit issue #1963

Merged
merged 32 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
d8464ac
Add new hidden option `--self-install`
NobodyXu Nov 6, 2024
0400688
Fix typo
NobodyXu Nov 6, 2024
9c20b3c
Optimize: Only call `LazyJobserverClient::new` when necessary
NobodyXu Nov 6, 2024
38f15ee
`--self-install` should include a path to a binary
NobodyXu Nov 6, 2024
2d68ade
Impl manifest update and basic API of `self_install`
NobodyXu Nov 6, 2024
273a4e8
Add dep atomic-file-install to cargo-binstall
NobodyXu Nov 6, 2024
40a9033
Impl `self_install`
NobodyXu Nov 6, 2024
65ca3d5
Make `--self-install` a boolean flag
NobodyXu Nov 6, 2024
142d53d
Accept no duration in `MainExit::new`
NobodyXu Nov 7, 2024
173cda5
Impl self-install mode in main_impl.rs
NobodyXu Nov 7, 2024
1f211d4
Use `--self-install` mode in install-from-binstall-release.sh
NobodyXu Nov 7, 2024
5107b51
Use `--self-install` in install-from-binstall-release.ps1
NobodyXu Nov 7, 2024
fdc4a1f
Merge branch 'main' into feature/self-install
NobodyXu Nov 7, 2024
90ab7d4
Fix import in mod entry
NobodyXu Nov 7, 2024
91e3b4c
Fix call of `self_install` in `main_impl`
NobodyXu Nov 7, 2024
5e7d54e
Fix `--self-install` clap doc
NobodyXu Nov 7, 2024
5cc8d05
Fix `entry::self_install`
NobodyXu Nov 7, 2024
15e775a
Apply suggestions from code review
NobodyXu Nov 7, 2024
b06d6a2
Fix parsing semver number in entry.rs
NobodyXu Nov 9, 2024
2af41d0
Fix compilation in entry.rs
NobodyXu Nov 9, 2024
e0c646e
fix entry.rs
NobodyXu Nov 9, 2024
36109cb
Fix fmt in bin_util.rs
NobodyXu Nov 9, 2024
2e6fe97
Fix fmt in entry.rs
NobodyXu Nov 9, 2024
49c0833
Use --self-install if supported in unix install script
NobodyXu Nov 9, 2024
234aa2a
Use --self-install if available in powershell install script
NobodyXu Nov 9, 2024
60c6411
Fix install-from-binstall-release.ps1
NobodyXu Nov 9, 2024
0cad38b
Fi install-from-binstall-release.ps1
NobodyXu Nov 9, 2024
5d4ac2e
Fix install-from-binstall-release.ps1
NobodyXu Nov 9, 2024
2afabb9
Create self-install.sh
NobodyXu Nov 9, 2024
a542961
Add e2e-test-self-install to justfile
NobodyXu Nov 9, 2024
19151ad
Fix args::parse() for self-install mode
NobodyXu Nov 9, 2024
53da569
Fix args parsing: Do no require positional arg if --self-install is p…
NobodyXu Nov 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pkg-fmt = "zip"
pkg-fmt = "zip"

[dependencies]
atomic-file-install = { version = "1.0.5", path = "../atomic-file-install" }
binstalk = { path = "../binstalk", version = "0.28.11", default-features = false }
binstalk-manifests = { path = "../binstalk-manifests", version = "0.15.8" }
clap = { version = "4.5.3", features = ["derive", "env"] }
Expand Down
9 changes: 8 additions & 1 deletion crates/bin/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub struct Args {
#[clap(
help_heading = "Package selection",
value_name = "crate[@version]",
required_unless_present_any = ["version", "help"],
required_unless_present_any = ["version", "self_install", "help"],
)]
pub(crate) crate_names: Vec<CrateName>,

Expand Down Expand Up @@ -404,6 +404,9 @@ pub struct Args {
/// This would override the `log_level`.
#[clap(help_heading = "Meta", short, long, conflicts_with("verbose"))]
pub(crate) quiet: bool,

#[clap(long, hide(true))]
pub(crate) self_install: bool,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -515,6 +518,10 @@ pub fn parse() -> (Args, PkgOverride) {
// Load options
let mut opts = Args::parse_from(args);

if opts.self_install {
return (opts, Default::default());
}

if opts.log_level.is_none() {
if let Some(log) = env::var("BINSTALL_LOG_LEVEL")
.ok()
Expand Down
13 changes: 6 additions & 7 deletions crates/bin/src/bin_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ impl Termination for MainExit {
}

impl MainExit {
pub fn new(res: Result<()>, done: Duration) -> Self {
res.map(|()| MainExit::Success(Some(done)))
.unwrap_or_else(|err| {
err.downcast::<BinstallError>()
.map(MainExit::Error)
.unwrap_or_else(MainExit::Report)
})
pub fn new(res: Result<()>, done: Option<Duration>) -> Self {
res.map(|()| MainExit::Success(done)).unwrap_or_else(|err| {
err.downcast::<BinstallError>()
.map(MainExit::Error)
.unwrap_or_else(MainExit::Report)
})
}
}

Expand Down
46 changes: 46 additions & 0 deletions crates/bin/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
time::Duration,
};

use atomic_file_install::atomic_install;
use binstalk::{
errors::{BinstallError, CrateContextError},
fetchers::{Fetcher, GhCrateMeta, QuickInstall, SignaturePolicy},
Expand All @@ -20,16 +21,20 @@ use binstalk::{
resolve::{CrateName, Resolution, ResolutionFetch, VersionReqExt},
CargoTomlFetchOverride, Options, Resolver,
},
TARGET,
};
use binstalk_manifests::{
cargo_config::Config,
cargo_toml_binstall::{PkgOverride, Strategy},
crate_info::{CrateInfo, CrateSource},
crates_manifests::Manifests,
};
use compact_str::CompactString;
use file_format::FileFormat;
use home::cargo_home;
use log::LevelFilter;
use miette::{miette, Report, Result, WrapErr};
use semver::Version;
use tokio::task::block_in_place;
use tracing::{debug, error, info, warn};

Expand Down Expand Up @@ -582,3 +587,44 @@ fn do_install_fetches_continue_on_failure(
Ok(())
})
}

pub fn self_install(args: Args) -> Result<()> {
// Load .cargo/config.toml
let cargo_home = cargo_home().map_err(BinstallError::from)?;
let mut config = Config::load_from_path(cargo_home.join("config.toml"))?;

// Compute paths
let cargo_root = args.root;
let (install_path, manifests, _) = compute_paths_and_load_manifests(
cargo_root.clone(),
args.install_path,
args.no_track,
cargo_home,
&mut config,
)?;

let mut dest = install_path.join("cargo-binstall");
if cfg!(windows) {
assert!(dest.set_extension("exe"));
}

atomic_install(&env::current_exe().map_err(BinstallError::from)?, &dest)
.map_err(BinstallError::from)?;

if let Some(manifests) = manifests {
manifests.update(vec![CrateInfo {
name: CompactString::const_new("cargo-binstall"),
version_req: CompactString::const_new("*"),
current_version: Version::new(
env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap(),
env!("CARGO_PKG_VERSION_MINOR").parse().unwrap(),
env!("CARGO_PKG_VERSION_PATCH").parse().unwrap(),
),
source: CrateSource::cratesio_registry(),
target: CompactString::const_new(TARGET),
bins: vec![CompactString::const_new("cargo-binstall")],
}])?;
}

Ok(())
}
9 changes: 5 additions & 4 deletions crates/bin/src/main_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use crate::{
};

pub fn do_main() -> impl Termination {
// This must be the very first thing to happen
let jobserver_client = LazyJobserverClient::new();

let (args, cli_overrides) = args::parse();

if args.version {
Expand Down Expand Up @@ -46,6 +43,8 @@ rustc-llvm-version: {rustc_llvm_version}"#
println!("{cargo_binstall_version}");
}
MainExit::Success(None)
} else if args.self_install {
MainExit::new(entry::self_install(args), None)
} else {
logging(
args.log_level.unwrap_or(LevelFilter::Info),
Expand All @@ -54,12 +53,14 @@ rustc-llvm-version: {rustc_llvm_version}"#

let start = Instant::now();

let jobserver_client = LazyJobserverClient::new();

let result =
run_tokio_main(|| entry::install_crates(args, cli_overrides, jobserver_client));

let done = start.elapsed();
debug!("run time: {done:?}");

MainExit::new(result, done)
MainExit::new(result, Some(done))
}
}
14 changes: 14 additions & 0 deletions e2e-tests/self-install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -euxo pipefail

unset CARGO_INSTALL_ROOT

CARGO_HOME=$(mktemp -d 2>/dev/null || mktemp -d -t 'cargo-home')
export CARGO_HOME
export PATH="$CARGO_HOME/bin:$PATH"

"./$1" --self-install

cargo binstall --help
cargo install --list
7 changes: 6 additions & 1 deletion install-from-binstall-release.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ $url = "$base_url$arch-pc-windows-msvc.zip"
Invoke-WebRequest $url -OutFile $tmpdir\cargo-binstall.zip
Expand-Archive -Force $tmpdir\cargo-binstall.zip $tmpdir\cargo-binstall
Write-Host ""
Invoke-Expression "$tmpdir\cargo-binstall\cargo-binstall.exe -y --force cargo-binstall"

$ps = Start-Process -PassThru -Wait "$tmpdir\cargo-binstall\cargo-binstall.exe" "--self-install"
if ($ps.ExitCode -ne 0) {
Invoke-Expression "$tmpdir\cargo-binstall\cargo-binstall.exe -y --force cargo-binstall"
}

Remove-Item -Force $tmpdir\cargo-binstall.zip
Remove-Item -Recurse -Force $tmpdir\cargo-binstall
$cargo_home = if ($Env:CARGO_HOME -ne $null) { $Env:CARGO_HOME } else { "$HOME\.cargo" }
Expand Down
2 changes: 1 addition & 1 deletion install-from-binstall-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ else
exit 1
fi

./cargo-binstall -y --force cargo-binstall
./cargo-binstall --self-install || ./cargo-binstall -y --force cargo-binstall

CARGO_HOME="${CARGO_HOME:-$HOME/.cargo}"

Expand Down
3 changes: 2 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ e2e-test-registries: (e2e-test "registries")
e2e-test-signing: (e2e-test "signing")
e2e-test-continue-on-failure: (e2e-test "continue-on-failure")
e2e-test-private-github-repo: (e2e-test "private-github-repo")
e2e-test-self-install: (e2e-test "self-install")

# WinTLS (Windows in CI) does not have TLS 1.3 support
[windows]
Expand All @@ -252,7 +253,7 @@ e2e-test-tls: (e2e-test "tls" "1.2")
[macos]
e2e-test-tls: (e2e-test "tls" "1.2") (e2e-test "tls" "1.3")

e2e-tests: e2e-test-live e2e-test-manifest-path e2e-test-git e2e-test-other-repos e2e-test-strategies e2e-test-version-syntax e2e-test-upgrade e2e-test-tls e2e-test-self-upgrade-no-symlink e2e-test-uninstall e2e-test-subcrate e2e-test-no-track e2e-test-registries e2e-test-signing e2e-test-continue-on-failure e2e-test-private-github-repo
e2e-tests: e2e-test-live e2e-test-manifest-path e2e-test-git e2e-test-other-repos e2e-test-strategies e2e-test-version-syntax e2e-test-upgrade e2e-test-tls e2e-test-self-upgrade-no-symlink e2e-test-uninstall e2e-test-subcrate e2e-test-no-track e2e-test-registries e2e-test-signing e2e-test-continue-on-failure e2e-test-private-github-repo e2e-test-self-install

unit-tests: print-env
cargo test --no-run --target {{target}}
Expand Down
Loading