-
Notifications
You must be signed in to change notification settings - Fork 486
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
fix: Change init to default to --config if --config-path is absent #3489
Conversation
Arguably an error check similar to the newly introduced one could also be done in case there is no Btw, another benefit of this PR would be that the |
The PR still needs work (see points below) but I need your input. First of all to know whether you think this can be brought into a state where the added value is worth the extra complexity/ugliness. And, if yes, what your recommendations are to get there. I'm not very familiar with github and pull requests but I guess that I have to mark the PR as "Ready for review" to get your attention. I will do that after this comment. Here are points that still need work/answers:
|
Firstly, thank you for your work and this excellent PR! To answer some of your questions:
Yes, absolutely! Users care about the interface, not the implementation, and this is a key usability improvement for people using multiple chezmoi configs. It's definitely worth the extra complexity, the ugliness is not visible, and as long as we have tests to check the user-facing behavior for common cases we can freely change the implementation to be more beautiful in the future.
Yes, but this falls foul of the complexities I described. Specifically, how do distinguish between "no
Yes. chezmoi's users are highly technical so by default report meaningful, actionable errors when anything is inconsistent.
Yup, the names you propose are better.
Yes, but this is an internal detail. I would suggest chezmoi does try to keep functions in some kind of mix of hierarchical and alphabetical order, so no need to move the function right now. At some point, I'll write a linter to check function order, but, for this PR, meaningful diffs are more important than function order so there is no need to move functions.
Please split the tests over multiple files. This both separates intent and allows the tests to run in less wallclock time (
Thanks again for this excellent PR! |
Yes, absolutely! Users care about the interface, not the implementation,
and this is a key usability improvement for people using multiple chezmoi
configs. It's definitely worth the extra complexity, the ugliness is not
visible, and as long as we have tests to check the user-facing behavior for
common cases we can freely change the implementation to be more beautiful
in the future.
Great to hear that :-) I'll first clean up the code and then write the
tests. The former should be relatively quick, the latter probably less so
as I'll first have to understand how txtar files work. I found testscript
<https://pkg.go.dev/github.com/rogpeppe/go-internal/testscript>, which
describes them. Is there anything else I should know about tests?
*Before I proceed, I have one question though: how am I supposed to
contribute the updates?* This is my first PR on github and it's not
entirely clear to me how I should go forward. Here is my current setup:
- I cloned your repo on github.
- I cloned my clone on my local machine.
- I created a commit on my local machine and pushed it to my clone on
github.
- On github I used the commit to create the PR.
My current understanding of how I should go forward is the following:
- Create one or more new commits on my local machine.
- Push the commits to my clone on github. They will auto-magically be added
to the PR.
- Rinse and repeat until we agree on a final state.
- You integrate the PR into your repo.
If this is indeed the correct way, how should I name and describe my
commits? Should I use "chore:", "fix:", or some other prefix? How important
are the descriptions? Are they just for you to understand what I'm
changing/adding to the PR or will they survive the PR and should therefore
also make sense for future readers of your repo? As you can see, one thing
that isn't clear to me is whether the extra commits will survive the PR or
whether they will all be merged into a single commit on your repo.
Yes, but this falls foul of the complexities I described
<#3127 (comment)>.
Specifically, how do distinguish between "no --config option was given"
and "a --config option was given but was set to the default value".
That should now be possible. That's why I split the old configFileAbsPath field
into two fields, configFileAbsPathDefault and configFileAbsPathExplicit,
and one function configFileAbsPath(). The former field only ever contains
the default value computed by defaultConfigFile(), the latter field only
ever contains the value of the --config option, or Empty, if none was
given. The function combines the two fields and returns the value that was
previously stored in the configFileAbsPath field.
This assumes that the --config option is the only way to explicitly specify
the config file (to load). My understanding is that the XDG specification
only allows to specify where (in which directories) to look for a config
file but not the config file itself. Did I overlook something?
…On Thu, Jan 18, 2024 at 1:19 AM Tom Payne ***@***.***> wrote:
Firstly, thank you for your work and this excellent PR! To answer some of
your questions:
First of all to know whether you think this can be brought into a state
where the added value is worth the extra complexity/ugliness.
Yes, absolutely! Users care about the interface, not the implementation,
and this is a key usability improvement for people using multiple chezmoi
configs. It's definitely worth the extra complexity, the ugliness is not
visible, and as long as we have tests to check the user-facing behavior for
common cases we can freely change the implementation to be more beautiful
in the future.
Should the new error check also be done when no --config option was given
(see previous comment)?
Yes, but this falls foul of the complexities I described
<#3127 (comment)>.
Specifically, how do distinguish between "no --config option was given"
and "a --config option was given but was set to the default value".
Elsewhere in the codebase I've used a sentinel value to handle this
<https://github.com/twpayne/chezmoi/blob/80717f4f1c35862d512e8b5415058cdc5cdcdc97/internal/cmd/config.go#L61>,
but I don't know if a similar approach could be used here.
In case of a conflict, should an error be reported?
Yes. chezmoi's users are highly technical so by default report meaningful,
actionable errors when anything is inconsistent.
Better names are needed for the Config fields.
Yup, the names you propose are better.
Should the new function configFileAbsPath() be renamed and/or moved?
Yes, but this is an internal detail. I would suggest getConfigFileAbsPath
to make the distinction between a value (e.g. configFileAbsPath) and a
potentially-expensive function that returns a value (
getConfigFileAbsPath()).
chezmoi does try to keep functions in some kind of mix of hierarchical and
alphabetical order, so no need to move the function right now. At some
point, I'll write a linter to check function order, but, for this PR,
meaningful diffs are more important than function order so there is no need
to move functions.
- A number of scenarios/journeys should be tested. Should I try to
squeeze everything into an issue3127.txtar or could the tests be split
over multiple files?
Please split the tests over multiple files. This both separates intent and
allows the tests to run in less wallclock time (go test can run them in
parallel). Specifically:
- For changes to behavior that should be obvious defaults - like this,
where removing the --config-path hack is a clear improvement, please
update internal/cmd/testdata/scripts/init.txtar with the new behavior.
- For exhaustive testing of the changes, please create a new
initconfig.txtar script with as many tests as you can think of to this
script, especially edge cases.
- For specific issues, like the original report in #3127
<#3127>, please do create an
issue3127.txtar that tests the reported case directly. These are
extremely helpful as high-level behavioral checks.
- Finally, if there are edge cases that you can think of that the
current code doesn't pass, please include them anyway, but commented out or
prefixed with [skip] so they don't run. Such cases are extremely
helpful to future developers, and it's OK that the code doesn't pass them
now.
Thanks again for this excellent PR!
—
Reply to this email directly, view it on GitHub
<#3489 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AXTPHFFG3JQABUBNMBUJCE3YPBTAJAVCNFSM6AAAAABB5REJ7CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJXGU2DEMBZGE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Firstly, there are answers to most of your questions in the contributing docs. You also see this link when you open a PR.
Please follow the style of the existing tests. Note that you can't easily use a debugger to step through testscript tests, so you might have to do some printf-style debugging. Useful commands for this include:
I think you are correct. It will be great to verify edge cases with tests. |
tl;dr: Rather than extend the new error check to more cases I will drop it. The case that it signals is a corner case that isn't much different from similar already existing corner cases that aren't signaled either. I still think that it would make sense to signal these cases but doing so turns out to be complex enough to warrant its own issue and pull request. Long VersionThe new error check that I added in my first version of the code tries to ensure that the updated config is written into a file of the right format. More precisely, it tries to enforce that if the config template is of format FORMAT, then the config is written into a file with a The check tries to prevent chezmoi from corrupting its own config. The For users that rely on a config file in a custom location, things are a bit different. They have to run all their chezmoi commands with the With the change proposed in this PR and assuming that the init option Fortunately the case looks very much like a corner case. The scenario described above leading to it, while possible, is certainly not one happening very often. I'm not aware of other scenarios leading to the same case. I can't exclude that others exist but they too most likely occur only rarely. Thus, there is no urgency in detecting and signaling this case. Furthermore, the problem exists already and the change in the PR doesn't make it significantly worse, the logic to detect the case isn't completely trivial, and finally even how to signal it isn't necessarily completely obvious. For all these reasons, it looks more appropriate to leave the detection and signaling of this case for a separate PR with its own issue. |
This PR is looking great. Please can you add some tests. The right place to add them is probably Thanks for the detailed explanations. It's certainly a very messy area with multiple interacting, possibly conflicting, sources of truth (e.g. multiple XDG config directories, On the up side, people changing config file formats is a relatively rare thing (although it does happen, see #1430), so I'm OK if chezmoi does not handle all edge cases correctly. |
I'm working on the tests. I was pondering on what exactly to include and also how to express it with testscript. They should soon be ready. |
Please don't hesitate to ask questions or brainstorm about tests. Generally speaking, the more the better, and it doesn't matter if they're ugly. We can always tidy them up later. |
I have now cleaned up the code and added tests. In I published here a proposal on how format conflicts could be detected and signaled. It's much more involved than the error check that I initially had in this PR. The code path for cases where |
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.
Looks excellent.
Please follow the existing style of testscripts, specifically with respect to removing empty lines between comments and the commands they refer to as they have a meaning to testscript.
|
||
# check that the last operation broke chezmoi | ||
! exec chezmoi status | ||
cmp stdout /dev/null |
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.
Use
! stdout .
! stderr .
to check that there was no output on stdout or stderr. Comparing with /dev/null
doesn't work on Windows. This should allow the tests to pass on Windows.
The latest error is a bit weird. First I thought that it was because I should have used $/ instead of / but after looking more closely, it looks like that chezmoi prints UNIX-like paths (with /) even when it runs on Windows and the problem is that $CHEZMOICONFIGDIR, $HOME, and $WORK contain Windows-like paths (with ). Is there a way to rewrite variables when they are expanded by testscript? |
Sadly it's really tricky to get tests that test paths to pass on Windows. chezmoi uses forward slashes internally on all platforms, but testscript uses the native path separator, i.e. backward slashes on Windows. When you mix chezmoi paths with testscript paths then you get a mix of the two. In the short term, please add
as the first line of the failing testscript tests. I hope to clean this up sometime in the future. Once that's done, please also squash all commits into a single one using this PR's title as the commit message, and then I think this PR is good to merge :) Thank you for your patience on this one. |
This is a fantastic PR that fixes a long-standing bug in chezmoi. Thank you so much! |
Here is an updated version of my previous PR. I have checked that it does what's expected and that it doesn't break any existing tests.
It should still be considered as a proof of concept rather than something ready to be submitted. My claim is that it contains the required logic to achieve the desired goal without breaking any existing behavior but that's about it. I haven't tried to add tests, yet. More importantly some changes, like in particular the replacement of
configFileAbsPath
withconfigFileAbsPathDefault
andconfigFileAbsPathExplicit
, look ugly. Note also that I have no experience programming in Go.The main benefit of this change is that it becomes possible to use
chezmoi
with an alternate config simply by defining an alias like the following one:alias chezmoi2="chezmoi --config=<path-to-alternate-config>"
This works for all commands, including the initial
init
command.Fixes #3127.