Skip to content
Open
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
28 changes: 20 additions & 8 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ impl GlobalContext {
) -> CargoResult<()> {
let includes = self.include_paths(cv, false)?;
for include in includes {
let Some(abs_path) = include.resolve_path(self) else {
let Some(abs_path) = include.resolve_path(self)? else {
continue;
};

Expand Down Expand Up @@ -1413,7 +1413,7 @@ impl GlobalContext {
// Accumulate all values here.
let mut root = CV::Table(HashMap::new(), value.definition().clone());
for include in includes {
let Some(abs_path) = include.resolve_path(self) else {
let Some(abs_path) = include.resolve_path(self)? else {
continue;
};

Expand Down Expand Up @@ -2305,9 +2305,9 @@ impl ConfigInclude {
/// For CLI based include (e.g., `--config 'include = "foo.toml"'`),
/// it is relative to the current working directory.
///
/// Returns `None` if this is an optional include and the file doesn't exist.
/// Otherwise returns `Some(PathBuf)` with the absolute path.
fn resolve_path(&self, gctx: &GlobalContext) -> Option<PathBuf> {
/// Returns `Ok(None)` if this is an optional include and the file doesn't exist.
/// Otherwise returns `Ok(Some(PathBuf))` with the absolute path.
fn resolve_path(&self, gctx: &GlobalContext) -> CargoResult<Option<PathBuf>> {
let abs_path = match &self.def {
Definition::Path(p) | Definition::Cli(Some(p)) => p.parent().unwrap(),
Definition::Environment(_) | Definition::Cli(None) | Definition::BuiltIn => gctx.cwd(),
Expand All @@ -2321,10 +2321,22 @@ impl ConfigInclude {
self.def,
abs_path.display(),
);
None
} else {
Some(abs_path)
return Ok(None);
}

let reserved_paths = ["config.toml.d", "user.config.toml", "user.config.toml.d"];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatives for user configuration:

  • local.config.toml
  • config.local.toml
  • config.user.toml

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we can decide at this point, we can reserve all of them

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better if we reserved the whole namespace (crates/*), at least temporarily. It would move the concern of what to reserve to a later date.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about us having to guess what we might want in the future.

Would be interested in seeing an updated stabilization report as that might provide some context to help with this (e.g. what have we said about globs/directory previously?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For configuration fragments:

  • conf.d
  • includes
  • Can't think of others

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These actually doesn't need to be reserved, and they don't have .toml extension so aren't allowed already.

for reserved in &reserved_paths {
let suffix = Path::new(".cargo").join(reserved);
if abs_path.ends_with(&suffix) {
bail!(
"`{}` is a reserved configuration path, but was included in `{}`",
suffix.display(),
self.def,
)
}
}

Ok(Some(abs_path))
}
}

Expand Down
93 changes: 93 additions & 0 deletions tests/testsuite/config_include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,3 +684,96 @@ Caused by:
"#]],
);
}

#[cargo_test]
fn user_config_toml_reserved() {
write_config_at(
".cargo/config.toml",
"
[[include]]
path = 'user.config.toml'
",
);

write_config_at(
".cargo/user.config.toml",
"
key1 = 1
",
);

let gctx = GlobalContextBuilder::new()
.unstable_flag("config-include")
.build_err();
assert_error(
gctx.unwrap_err(),
str![[r#"
could not load Cargo configuration

Caused by:
`.cargo/user.config.toml` is a reserved configuration path, but was included in `[ROOT]/.cargo/config.toml`
"#]],
);
}

#[cargo_test]
fn config_toml_fragment_dir_reserved() {
write_config_at(
".cargo/config.toml",
"
[[include]]
path = 'config.toml.d'
",
);

write_config_at(
".cargo/config.toml.d/00-myconfig.toml",
"
key1 = 1
",
);

let gctx = GlobalContextBuilder::new()
.unstable_flag("config-include")
.build_err();
assert_error(
gctx.unwrap_err(),
str![[r#"
could not load Cargo configuration

Caused by:
expected a config include path ending with `.toml`, but found `config.toml.d` from `[ROOT]/.cargo/config.toml`
"#]],
);
}

#[cargo_test]
fn user_config_toml_fragment_dir_reserved() {
write_config_at(
".cargo/config.toml",
"
[[include]]
path = 'user.config.toml.d'
",
);

write_config_at(
".cargo/user.config.toml.d/00-myconfig.toml",
"
key1 = 1
",
);

let gctx = GlobalContextBuilder::new()
.unstable_flag("config-include")
.build_err();
assert_error(
gctx.unwrap_err(),
str![[r#"
could not load Cargo configuration

Caused by:
expected a config include path ending with `.toml`, but found `user.config.toml.d` from `[ROOT]/.cargo/config.toml`
"#]],
);
}