From 61fe8750c73b3fcb17041a7876989b4a44b46f27 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 21 Nov 2025 13:46:42 -0500 Subject: [PATCH 1/2] test(config-include): show glob/template syntax isnt disallowed Which we want to disallow to reserve for future. --- tests/testsuite/config_include.rs | 48 +++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/testsuite/config_include.rs b/tests/testsuite/config_include.rs index 06e71f2901d..f2d0cdc15fd 100644 --- a/tests/testsuite/config_include.rs +++ b/tests/testsuite/config_include.rs @@ -684,3 +684,51 @@ Caused by: "#]], ); } + +#[cargo_test] +fn disallow_glob_syntax() { + // Reserved for future extension + write_config_toml("include = 'config-*.toml'"); + let gctx = GlobalContextBuilder::new() + .unstable_flag("config-include") + .build_err(); + assert_error( + gctx.unwrap_err(), + str![[r#" +could not load Cargo configuration + +Caused by: + failed to load config include `config-*.toml` from `[ROOT]/.cargo/config.toml` + +Caused by: + failed to read configuration file `[ROOT]/.cargo/config-*.toml` + +Caused by: + [NOT_FOUND] +"#]], + ); +} + +#[cargo_test] +fn disallow_template_syntax() { + // Reserved for future extension + write_config_toml("include = '{workspace-root}/config.toml'"); + let gctx = GlobalContextBuilder::new() + .unstable_flag("config-include") + .build_err(); + assert_error( + gctx.unwrap_err(), + str![[r#" +could not load Cargo configuration + +Caused by: + failed to load config include `{workspace-root}/config.toml` from `[ROOT]/.cargo/config.toml` + +Caused by: + failed to read configuration file `[ROOT]/.cargo/{workspace-root}/config.toml` + +Caused by: + [NOT_FOUND] +"#]], + ); +} From 80c849ee464562e76f1c266794641a5eb08b13a3 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Fri, 21 Nov 2025 13:53:51 -0500 Subject: [PATCH 2/2] fix(config-include): disallow glob and template syntax This reserves future possibility for us to support globa syntax and templated paths. Addressing https://github.com/rust-lang/cargo/pull/16284#discussion_r2550499932 --- src/cargo/util/context/mod.rs | 23 ++++++++++++++++++++++- tests/testsuite/config_include.rs | 16 ++-------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/cargo/util/context/mod.rs b/src/cargo/util/context/mod.rs index 0503e8ff35c..bc225185c3d 100644 --- a/src/cargo/util/context/mod.rs +++ b/src/cargo/util/context/mod.rs @@ -61,7 +61,6 @@ //! read the environment variable due to ambiguity. (See `ConfigMapAccess` for //! more details.) -use crate::util::cache_lock::{CacheLock, CacheLockMode, CacheLocker}; use std::borrow::Cow; use std::collections::{HashMap, HashSet}; use std::env; @@ -85,9 +84,11 @@ use crate::ops::RegistryCredentialConfig; use crate::sources::CRATES_IO_INDEX; use crate::sources::CRATES_IO_REGISTRY; use crate::util::OnceExt as _; +use crate::util::cache_lock::{CacheLock, CacheLockMode, CacheLocker}; use crate::util::errors::CargoResult; use crate::util::network::http::configure_http_handle; use crate::util::network::http::http_handle; +use crate::util::restricted_names::is_glob_pattern; use crate::util::{CanonicalUrl, closest_msg, internal}; use crate::util::{Filesystem, IntoUrl, IntoUrlWithBase, Rustc}; @@ -1499,6 +1500,26 @@ impl GlobalContext { include.def, ) } + + if let Some(path) = include.path.to_str() { + // Ignore non UTF-8 bytes as glob and template syntax are for textual config. + if is_glob_pattern(path) { + bail!( + "expected a config include path without glob patterns, \ + but found `{}` from `{}`", + include.path.display(), + include.def, + ) + } + if path.contains(&['{', '}']) { + bail!( + "expected a config include path without template braces, \ + but found `{}` from `{}`", + include.path.display(), + include.def, + ) + } + } } Ok(includes) diff --git a/tests/testsuite/config_include.rs b/tests/testsuite/config_include.rs index f2d0cdc15fd..6f1ee7a2688 100644 --- a/tests/testsuite/config_include.rs +++ b/tests/testsuite/config_include.rs @@ -698,13 +698,7 @@ fn disallow_glob_syntax() { could not load Cargo configuration Caused by: - failed to load config include `config-*.toml` from `[ROOT]/.cargo/config.toml` - -Caused by: - failed to read configuration file `[ROOT]/.cargo/config-*.toml` - -Caused by: - [NOT_FOUND] + expected a config include path without glob patterns, but found `config-*.toml` from `[ROOT]/.cargo/config.toml` "#]], ); } @@ -722,13 +716,7 @@ fn disallow_template_syntax() { could not load Cargo configuration Caused by: - failed to load config include `{workspace-root}/config.toml` from `[ROOT]/.cargo/config.toml` - -Caused by: - failed to read configuration file `[ROOT]/.cargo/{workspace-root}/config.toml` - -Caused by: - [NOT_FOUND] + expected a config include path without template braces, but found `{workspace-root}/config.toml` from `[ROOT]/.cargo/config.toml` "#]], ); }