Skip to content
Draft
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
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ 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 `dependencies:` key
is kept, with no entries under it, while a comment is on it, directly above it
or still under it; otherwise the whole block is removed. Removing entries one at
a time gives the same file as removing them together.

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.

Only this command keeps comments. `lint-package-yml-files`, `add-dependency`,
`create`, `rm` and `update-dependencies-for-constant` still rewrite the
`package.yml` they change and drop its comments, so running
`lint-package-yml-files` afterwards removes the comments kept here.

## 0.7.0

### Features
Expand Down
47 changes: 38 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,43 @@ 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 => {
let mut sorted_names: Vec<&String> =
dependency_names.iter().collect();
sorted_names.sort();
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(),
sorted_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