-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
$env.config
now always holds a record with only valid values
#7309
Conversation
$env.config
now always holds a record with only valid values
Thanks for the PR! I find the new behaviour a little unintuitive. It's surprising to me that Maybe I haven't fully wrapped my head around this problem 🤔 |
I'm also a little concerned that it's getting harder to add new config points; I assume that any new config will now need to be added to Not necessarily a dealbreaker, maybe that's a cost we have to pay, just something that jumped out at me. |
I called out this in our discord, |
// NOTE: currently (Dec 2022) the subrecords of config.menus, config.keybindings and config.hooks | ||
// are NOT reconstructed by config_to_nu_record(), but restored from self.config as-is. | ||
// As such, invalid values may persist on them (as into_config() doesn't remove them. | ||
if let Ok(config) = v.clone().into_config() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This into_config
will still return a new Config
, eventually, you will have another default config and the only change to that updated field
I'm not a big fan of this change because it adds 400+ lines of code to maintain and as Reilly said, adding new config options would involve one more step. Currently, invalid config options print out error messages which I find sufficient. I imagine that with the improvements to the type system, we could be able to tighten it better. For example, allowing the config to only be a record and we could even go as far as enforcing all the record key names and their types. @Kangaxx-0 I think this PR is solving a bit different issue but you're right, what you showed on Discord didn't seem right. |
Yes, I was talking a different issue, however, due to the change of this commit , things got more complicated, and I wished this pr could get my work easier. Allow me to talk through the example
To reuse the existing config, nu needs to know which config That being said, for user's input
If we could have the single source of truth to represent and save |
abce5a4
to
0910b9b
Compare
OK, I've changed it now so that
I suppose I could rework this so that
My take is that while this is the most efficient way of doing this, it's still the most brittle. What I would do instead is:
|
Do you have a rough idea how it works? I could not come up with a clean solution due to the new grouped structure This is the evaluation code,
To fix this, we somehow need to re-construct a proper record (probably nested, depends on the structure) which matches the group config change, otherwise, nu will never know which config field to update. The old structure would not be a big problem because it was one-layered record, the casting or conversion would not be too messy |
I've had a look at the code section you've pointed out (thank you very much) and I've devised a solution in a separate PR: #7318 |
@webbedspace We chatted with the core team and agreed we're not comfortable merging this because of the extra overhead for developers when changing the config. We could still keep the One way forward would be improving the |
That's fine. I'll do it when I get time. |
OK, I'll put it as draft for now, then. |
d60b7c3
to
4344c8f
Compare
OK, new approach - I reworked this so that |
OK, this looks better, you don't need to add the config option 2x now. I think we can merge it. One thing I would still change is instead of It would also be good to add test for these. One way to do it might be to use the "REPL simulator" (like this but the test bin might need updating with the |
Ah, makes sense. Then, instead of |
First problem I can see with that is that invalid config values should NOT result in |
Yeah, that makes sense. Then, |
Actually, I had to change it entirely because in order to create good errors, I needed each value's span. ☠ Thanks for the jinx. Tests will be added… very soon. |
7c0020b
to
e33ef4f
Compare
e33ef4f
to
b20ff73
Compare
This got more complicated. I realised a few things while making the tests: one, I could add more macros to make the By the way, I'll fix the failing tests when I get some more time (very shortly). |
b20ff73
to
d2253e4
Compare
I think more macros are not necessary. Especially if the macro is used only once, then there is no point in having the macro. The problem is not the number of LOC but what was there initially that you'd need to define config entries in two places instead of only one (now eliminated). So I wouldn't go too crazy with the macros because they can actually decrease the readability if overused. You introduced some type checking and also mentioned checking the values for only the valid ones. My hope is that the type checking and key matching would eventually be handled by our type system so I wouldn't put any more effort into that. Making sure only valid values are inserted would be good to check (unless we implement enums in the language) but I'd leave that for a next PR and try to land this one first. It's already quite big. One comment: The message The examples in the PR descriptions are super helpful, thanks for that. |
I like to think I chose relatively good things to encapsulate in macros.
This is impossible because PR will be done as soon as I stop having a devil of a time figuring out why the test code doesn't seem to cause into_config() to fire at all, even though it works in the REPL… |
OK, yeah, sounds reasonable. There is not much we can do about the key spans right now. REPL operates in parse-eval cycles followed by |
You might need to add an extra empty line to the test code to trigger the last |
I couldn't make the last 3 tests work because they depend on observing Nushell behaviour after a config-setting error has occurred (see each for details) which isn't currently possible to test with nu_repl() because it exits after the first error, so I've marked them |
OK, thanks! We can figure out how to extend the test runner to continue after errors later, there could be a flag or something. |
Thanks for putting in a lot of work to get this over the line, @webbedspace and @kubouch! |
Description
Closes #7059. Rather than generate a new Record each time $env.config is accessed (as described in that issue), instead
$env.config =
now A) parses the input record, then B) un-parses it into a clean Record with only the valid values, and stores that as an env-var. The reasoning for this is that I believeconfig_to_nu_record()
(the method that performs step B) will be useful in later PRs. (See below)As a result, this also "fixes" the following "bug":
Instead,$env.config = 'butts'
now turns$env.config
into the default (not the default config.nu, butConfig::default()
, which notably has empty keybindings, color_config, menus and hooks vecs).This doesn't attempt to fix #7110. cc @Kangaxx-0
Example of new behaviour
OLD:
NEW:
Example of new errors
OLD:
NEW:
User-Facing Changes
See above.
Tests + Formatting
Don't forget to add tests that cover your changes.
Make sure you've run and fixed any issues with these commands:
cargo fmt --all -- --check
to check standard code formatting (cargo fmt --all
applies these changes)cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect
to check that you're using the standard code stylecargo test --workspace
to check that all tests passAfter Submitting
If your PR had any user-facing changes, update the documentation after the PR is merged, if necessary. This will help us keep the docs up to date.