Skip to content

Commit 41321ac

Browse files
committed
inputs: support removal of deeply nested follows
Handle `rm disko.nixpkgs` when the follows uses the attrset form: inputs = { nixpkgs = { follows = "nixpkgs"; }; };
1 parent ab5a1fd commit 41321ac

File tree

5 files changed

+91
-5
lines changed

5 files changed

+91
-5
lines changed

src/walk/inputs.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -991,12 +991,49 @@ fn handle_input_attr_set(
991991

992992
if leaf.to_string().starts_with("inputs") {
993993
let id = child.prev_sibling().unwrap();
994-
let context = id.to_string().into();
995-
if let Some(replacement) =
996-
walk_inputs(inputs, child.clone(), &Some(context), change)
997-
{
994+
let context: Context = id.to_string().into();
995+
let ctx_some = Some(context);
996+
if let Some(replacement) = walk_inputs(inputs, child.clone(), &ctx_some, change) {
998997
return Some(substitute_child(node, child.index(), &replacement));
999998
}
999+
1000+
// Handle deeply nested inputs attrset removal:
1001+
// `inputs = { nixpkgs = { follows = "nixpkgs"; }; }`
1002+
if leaf.to_string() == "inputs"
1003+
&& change.is_remove()
1004+
&& let Some(inputs_attrset) =
1005+
attr.children().find(|c| c.kind() == SyntaxKind::NODE_ATTR_SET)
1006+
{
1007+
for nested_entry in inputs_attrset.children() {
1008+
if nested_entry.kind() != SyntaxKind::NODE_ATTRPATH_VALUE {
1009+
continue;
1010+
}
1011+
let Some(nested_path) = nested_entry.first_child() else {
1012+
continue;
1013+
};
1014+
let Some(nested_id) = nested_path.first_child() else {
1015+
continue;
1016+
};
1017+
if should_remove_nested_input(
1018+
change,
1019+
&ctx_some,
1020+
&nested_id.to_string(),
1021+
) {
1022+
let new_inputs_attrset = remove_child_with_whitespace(
1023+
&inputs_attrset,
1024+
&nested_entry,
1025+
nested_entry.index(),
1026+
);
1027+
let new_attr = substitute_child(
1028+
&attr,
1029+
inputs_attrset.index(),
1030+
&new_inputs_attrset,
1031+
);
1032+
let new_child = substitute_child(child, attr.index(), &new_attr);
1033+
return Some(substitute_child(node, child.index(), &new_child));
1034+
}
1035+
}
1036+
}
10001037
}
10011038
}
10021039
}

tests/edit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ fn test_remove_input_walker(#[case] fixture: &str, #[case] input_id: &str) {
185185
#[case("root", "rust-overlay.flake-utils")]
186186
#[case("completely_flat_toplevel", "crane.rust-overlay")]
187187
#[case("one_level_nesting_flat", "rust-overlay.flake-utils")]
188+
#[case("deeply_nested_inputs", "disko.nixpkgs")]
188189
fn test_remove_nested_input(#[case] fixture: &str, #[case] input_id: &str) {
189190
let content = load_flake(fixture);
190191
let mut flake_edit = FlakeEdit::from_text(&content).unwrap();

tests/snapshots/cli__remove@deeply_nested_inputs_nixpkgs.snap

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ exit_code: 0
1616
----- stdout -----
1717
--- original
1818
+++ modified
19-
@@ -2,7 +2,6 @@
19+
@@ -2,14 +2,10 @@
2020
description = "Deeply nested inputs attrset";
2121

2222
inputs = {
2323
- nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
2424

2525
disko = {
2626
url = "github:nix-community/disko";
27+
inputs = {
28+
- nixpkgs = {
29+
- follows = "nixpkgs";
30+
- };
31+
};
32+
};
33+
};
2734

2835
----- stderr -----

tests/snapshots/cli__remove@root_alt_nixpkgs.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,21 @@ exit_code: 0
2424

2525
flake-utelinos.url = "github:numtide/flake-utils";
2626

27+
@@ -9,7 +8,6 @@
28+
rust-overlay = {
29+
url = "github:oxalica/rust-overlay";
30+
inputs = {
31+
- nixpkgs.follows = "nixpkgs";
32+
flake-utils.follows = "flake-utils";
33+
};
34+
};
35+
@@ -16,7 +14,6 @@
36+
crane = {
37+
url = "github:ipetkov/crane";
38+
inputs = {
39+
- nixpkgs.follows = "nixpkgs";
40+
rust-overlay.follows = "rust-overlay";
41+
flake-utils.follows = "flake-utils";
42+
};
2743

2844
----- stderr -----
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
source: tests/edit.rs
3+
expression: result
4+
info:
5+
flake_nix: ""
6+
changes:
7+
- Remove:
8+
ids:
9+
- disko.nixpkgs
10+
---
11+
{
12+
description = "Deeply nested inputs attrset";
13+
14+
inputs = {
15+
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
16+
17+
disko = {
18+
url = "github:nix-community/disko";
19+
inputs = {
20+
};
21+
};
22+
};
23+
24+
outputs = _: { };
25+
}

0 commit comments

Comments
 (0)