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

introduce no-path-check for install command #14274

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use cargo::ops;
use cargo::util::IntoUrl;
use cargo::util::VersionExt;
use cargo::CargoResult;
use clap::parser::ValueSource;
use itertools::Itertools;
use semver::VersionReq;

Expand Down Expand Up @@ -69,6 +70,7 @@ pub fn cli() -> Command {
)
.arg(opt("root", "Directory to install packages into").value_name("DIR"))
.arg(flag("force", "Force overwriting existing crates or binaries").short('f'))
.arg(flag("no-path-check", "Do not perform a PATH check"))
.arg(flag("no-track", "Do not save tracking information"))
.arg(flag(
"list",
Expand Down Expand Up @@ -176,6 +178,12 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {

let root = args.get_one::<String>("root").map(String::as_str);

let no_path_check = if args.value_source("no-path-check") != Some(ValueSource::DefaultValue) {
args.flag("no-path-check")
} else {
gctx.install_config()?.no_path_check.is_some_and(|f| f)
};

// We only provide workspace information for local crate installation from
// one of the following sources:
// - From current working directory (only work for edition 2015).
Expand Down Expand Up @@ -213,6 +221,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
&compile_opts,
args.flag("force"),
args.flag("no-track"),
no_path_check,
)?;
}
Ok(())
Expand Down
8 changes: 7 additions & 1 deletion src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ pub fn install(
opts: &ops::CompileOptions,
force: bool,
no_track: bool,
no_path_check: bool,
) -> CargoResult<()> {
let root = resolve_root(root, gctx)?;
let dst = root.join("bin").into_path_unlocked();
Expand Down Expand Up @@ -739,7 +740,12 @@ pub fn install(
(!succeeded.is_empty(), !failed.is_empty())
};

if installed_anything {
let skip_path_check = match gctx.get_env_os("CARGO_INSTALL_NO_PATH_CHECK") {
Some(v) => Some(v == "1"),
None => Some(no_path_check),
};

if installed_anything && !skip_path_check.is_some_and(|f| f) {
// Print a warning that if this directory isn't in PATH that they won't be
// able to run these commands.
let path = gctx.get_env_os("PATH").unwrap_or_default();
Expand Down
13 changes: 13 additions & 0 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ pub struct GlobalContext {
future_incompat_config: LazyCell<CargoFutureIncompatConfig>,
net_config: LazyCell<CargoNetConfig>,
build_config: LazyCell<CargoBuildConfig>,
install_config: LazyCell<CargoInstallConfig>,
target_cfgs: LazyCell<Vec<(String, TargetCfgConfig)>>,
doc_extern_map: LazyCell<RustdocExternMap>,
progress_config: ProgressConfig,
Expand Down Expand Up @@ -318,6 +319,7 @@ impl GlobalContext {
future_incompat_config: LazyCell::new(),
net_config: LazyCell::new(),
build_config: LazyCell::new(),
install_config: LazyCell::new(),
target_cfgs: LazyCell::new(),
doc_extern_map: LazyCell::new(),
progress_config: ProgressConfig::default(),
Expand Down Expand Up @@ -1824,6 +1826,11 @@ impl GlobalContext {
.try_borrow_with(|| self.get::<CargoBuildConfig>("build"))
}

pub fn install_config(&self) -> CargoResult<&CargoInstallConfig> {
self.install_config
.try_borrow_with(|| self.get::<CargoInstallConfig>("install"))
}

pub fn progress_config(&self) -> &ProgressConfig {
&self.progress_config
}
Expand Down Expand Up @@ -2617,6 +2624,12 @@ pub struct CargoBuildConfig {
pub artifact_dir: Option<ConfigRelativePath>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CargoInstallConfig {
pub no_path_check: Option<bool>,
}

/// Configuration for `build.target`.
///
/// Accepts in the following forms:
Expand Down
6 changes: 6 additions & 0 deletions src/doc/man/cargo-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ useful if something has changed on the system that you want to rebuild with,
such as a newer version of `rustc`.
{{/option}}

{{#option "`--no-path-check`" }}
By default, Cargo will check if installed programs are available in the
configured `PATH` and generate a warning if this is not the case. This flag
tells Cargo not to perform any path checks.
{{/option}}

{{#option "`--no-track`" }}
By default, Cargo keeps track of the installed packages with a metadata file
stored in the installation root directory. This flag tells Cargo not to use or
Expand Down
6 changes: 6 additions & 0 deletions src/doc/src/commands/cargo-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ useful if something has changed on the system that you want to rebuild with,
such as a newer version of <code>rustc</code>.</dd>


<dt class="option-term" id="option-cargo-install---no-path-check"><a class="option-anchor" href="#option-cargo-install---no-path-check"></a><code>--no-path-check</code></dt>
<dd class="option-desc">By default, Cargo will check if installed programs are available in the
configured <code>PATH</code> and generate a warning if this is not the case. This flag
tells Cargo not to perform any path checks.</dd>


<dt class="option-term" id="option-cargo-install---no-track"><a class="option-anchor" href="#option-cargo-install---no-track"></a><code>--no-track</code></dt>
<dd class="option-desc">By default, Cargo keeps track of the installed packages with a metadata file
stored in the installation root directory. This flag tells Cargo not to use or
Expand Down
14 changes: 14 additions & 0 deletions src/doc/src/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,20 @@ string that includes Cargo's version.

The `[install]` table defines defaults for the [`cargo install`] command.

#### `install.no-path-check`
* Type: boolean
* Default: false
* Environment: `CARGO_INSTALL_NO_PATH_CHECK`

If this is `true`, then Cargo will not perform any checks if an installed
command is not available on the configured `PATH`.

Setting this to `true` can be helpful when the install root is configured
to an interim location (e.g. cross-compiling) and an installer wishes to
suppress any path checks warnings not relevant for their use case.

Can be overridden with the `--no-path-check` command-line option.

#### `install.root`
* Type: string (path)
* Default: Cargo's home directory
Expand Down
2 changes: 2 additions & 0 deletions src/doc/src/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ In summary, the supported environment variables are:
* `CARGO_HTTP_LOW_SPEED_LIMIT` --- The HTTP low-speed limit, see [`http.low-speed-limit`].
* `CARGO_HTTP_MULTIPLEXING` --- Whether HTTP/2 multiplexing is used, see [`http.multiplexing`].
* `CARGO_HTTP_USER_AGENT` --- The HTTP user-agent header, see [`http.user-agent`].
* `CARGO_INSTALL_NO_PATH_CHECK` --- Disables any path checks when using [`cargo install`], see [`install.no-path-check`].
* `CARGO_INSTALL_ROOT` --- The default directory for [`cargo install`], see [`install.root`].
* `CARGO_NET_RETRY` --- Number of times to retry network errors, see [`net.retry`].
* `CARGO_NET_GIT_FETCH_WITH_CLI` --- Enables the use of the `git` executable to fetch, see [`net.git-fetch-with-cli`].
Expand Down Expand Up @@ -176,6 +177,7 @@ In summary, the supported environment variables are:
[`http.low-speed-limit`]: config.md#httplow-speed-limit
[`http.multiplexing`]: config.md#httpmultiplexing
[`http.user-agent`]: config.md#httpuser-agent
[`install.no-path-check`]: config.md#installno-path-check
[`install.root`]: config.md#installroot
[`net.retry`]: config.md#netretry
[`net.git-fetch-with-cli`]: config.md#netgit-fetch-with-cli
Expand Down
2 changes: 1 addition & 1 deletion src/etc/cargo.bashcomp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ _cargo()
local opt__generate_lockfile="$opt_common $opt_mani $opt_lock"
local opt__help="$opt_help"
local opt__init="$opt_common $opt_lock --bin --lib --name --vcs --edition --registry"
local opt__install="$opt_common $opt_feat $opt_parallel $opt_lock $opt_force --bin --bins --branch --debug --example --examples --git --list --path --rev --root --tag --version --registry --target --profile --no-track --ignore-rust-version"
local opt__install="$opt_common $opt_feat $opt_parallel $opt_lock $opt_force --bin --bins --branch --debug --example --examples --git --list --path --rev --root --tag --version --registry --target --profile --no-path-check --no-track --ignore-rust-version"
local opt__locate_project="$opt_common $opt_mani $opt_lock --message-format --workspace"
local opt__login="$opt_common $opt_lock --registry"
local opt__metadata="$opt_common $opt_feat $opt_mani $opt_lock --format-version=1 --no-deps --filter-platform"
Expand Down
Loading