forge
Explore
Log in

Don't you dare go Hollow

Implement include optional=true (#3022)

Browse files at this commit

7a237e519c69ec493851ffac169abb3aa917a7b3UnverifiedJohn Rinehart and Ivan MolodetskikhParent: c4462d0
* feat(niri): support `include optional=true "filename.kdl"`

* chore: warn if optional include ENOENT

* chore: validate include directive arguments and properties

Add proper validation to reject:
- Extra arguments beyond the path
- Unknown properties (other than "optional")
- Unexpected child nodes

* docs: implement suggested typographical/prose changes

* fixes

---------
docs/wiki/Configuration:-Include.mdmodified+24 -0
@@ -114,6 +114,30 @@ window-rule {
114 }114 }
115 ```115 ```
116 116
117+### Optional includes
118+
119+<sup>Since: next release</sup>
120+
121+By default, including a nonexistent file will cause an error.
122+You can allow nonexistent includes by setting `optional=true`:
123+
124+```kdl,must-fail
125+// Won't fail if this file doesn't exist.
126+include optional=true "optional-config.kdl"
127+
128+// Regular include, will fail if the file doesn't exist.
129+include "required-config.kdl"
130+```
131+
132+When an optional include file is missing, niri will emit a warning in the logs on every config reload.
133+This reminds you that the file is missing while still loading the config successfully.
134+
135+The optional file is still watched for changes, so if you create it later, the config will automatically reload and apply the new settings.
136+
137+Note that `optional` only affects whether a missing file causes an error.
138+If the file exists but contains invalid syntax or other errors, those errors will still cause a parsing failure.
139+
140+
117 ### Merging141 ### Merging
118 142
119 Most config sections are merged between includes, meaning that you can set only a few properties, and only those properties will change.143 Most config sections are merged between includes, meaning that you can set only a few properties, and only those properties will change.
niri-config/src/lib.rsmodified+55 -5
@@ -291,7 +291,51 @@ where
291 }291 }
292 292
293 "include" => {293 "include" => {
294- let path: PathBuf = utils::parse_arg_node("include", node, ctx)?;294+ // Parse the path argument
295+ let mut iter_args = node.arguments.iter();
296+ let path_val = iter_args.next().ok_or_else(|| {
297+ DecodeError::missing(
298+ node,
299+ "additional argument for include path is required",
300+ )
301+ })?;
302+ let path: PathBuf = knuffel::traits::DecodeScalar::decode(path_val, ctx)?;
303+
304+ // Check for extra arguments
305+ if let Some(val) = iter_args.next() {
306+ ctx.emit_error(DecodeError::unexpected(
307+ &val.literal,
308+ "argument",
309+ "unexpected argument",
310+ ));
311+ }
312+
313+ // Parse the optional property
314+ let mut optional = false;
315+ for (name, val) in &node.properties {
316+ match &***name {
317+ "optional" => {
318+ optional = knuffel::traits::DecodeScalar::decode(val, ctx)?;
319+ }
320+ name_str => {
321+ ctx.emit_error(DecodeError::unexpected(
322+ name,
323+ "property",
324+ format!("unexpected property `{}`", name_str.escape_default()),
325+ ));
326+ }
327+ }
328+ }
329+
330+ // Check for unexpected children
331+ for child in node.children() {
332+ ctx.emit_error(DecodeError::unexpected(
333+ child,
334+ "node",
335+ format!("unexpected node `{}`", child.node_name.escape_default()),
336+ ));
337+ }
338+
295 let base = ctx.get::<BasePath>().unwrap();339 let base = ctx.get::<BasePath>().unwrap();
296 let path = base.0.join(path);340 let path = base.0.join(path);
297 341
@@ -369,10 +413,16 @@ where
369 }413 }
370 }414 }
371 Err(err) => {415 Err(err) => {
372- ctx.emit_error(DecodeError::missing(416+ if optional && err.kind() == std::io::ErrorKind::NotFound {
373- node,417+ // Warn about missing optional includes
374- format!("failed to read included config from {path:?}: {err}"),418+ warn!("optional include not found: {path:?}");
375- ));419+ } else {
420+ // Report all other errors normally
421+ ctx.emit_error(DecodeError::missing(
422+ node,
423+ format!("failed to read included config from {path:?}: {err}"),
424+ ));
425+ }
376 }426 }
377 }427 }
378 }428 }