Skip to content
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ So `pks check` may report a violation it didn't, or no longer report one, and a
`package_todo.yml` entry for one it no longer reports is stale until you run
`pks update`.

#### `check-unused-dependencies --auto-correct` keeps comments in `package.yml`

Auto-correct used to rewrite every `package.yml` it touched from scratch, which
deleted all of its comments and reordered its keys. It now removes the unused
entries from the `dependencies:` list and leaves the rest of the file as it
was, line endings included. A comment covers the entries below it, up to the
next blank line or comment, and is removed only when all of those entries are.
A commented-out entry such as `# - packs/a` is always kept, along with any
comment directly above it. If no dependencies remain, the whole `dependencies:`
block is removed, including the comments and blank lines inside it.

If a list is written in a form that cannot be edited this way, such as
`dependencies: [packs/a, packs/b]`, the file is rewritten as before and a
warning names the file and the dependencies removed.

## 0.7.0

### Features
Expand Down
44 changes: 35 additions & 9 deletions src/packs/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod dependency;
pub(crate) mod layer;

mod common_test;
mod dependency_removal;
mod folder_privacy;
pub(crate) mod pack_checker;
mod privacy;
Expand Down Expand Up @@ -606,15 +607,40 @@ fn remove_reference_to_dependency(
pack: &Pack,
dependency_names: &[String],
) -> anyhow::Result<()> {
let without_dependency = pack
.dependencies
.iter()
.filter(|dependency| !dependency_names.contains(dependency));
let updated_pack = Pack {
dependencies: without_dependency.cloned().collect(),
..pack.clone()
};
write_pack_to_disk(&updated_pack)?;
let contents = std::fs::read_to_string(&pack.yml).map_err(|e| {
anyhow::Error::new(e)
.context(format!("Failed to read pack {:?}", pack.yml))
})?;

match dependency_removal::remove_dependencies(&contents, dependency_names) {
Some(updated) => std::fs::write(&pack.yml, updated).map_err(|e| {
anyhow::Error::new(e)
.context(format!("Failed to write pack to disk {:?}", pack.yml))
})?,
None => {
eprintln!(
"Warning: could not edit the dependencies list in `{}` in \
place to remove {}, so the file was rewritten: its comments \
were not preserved and its keys may be reordered.",
pack.relative_yml().display(),
dependency_names
.iter()
.map(|name| format!("`{name}`"))
.collect::<Vec<_>>()
.join(", "),
);
let without_dependency = pack
.dependencies
.iter()
.filter(|dependency| !dependency_names.contains(dependency));
let updated_pack = Pack {
dependencies: without_dependency.cloned().collect(),
..pack.clone()
};
write_pack_to_disk(&updated_pack)?;
}
}

Ok(())
}
// Note: Display impl was removed from CheckAllResult. Use write_text() directly with Configuration.
Expand Down
Loading