Skip to content
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

Read $HOME config before (and in addition to) the one in $PWD #320

Closed
wants to merge 1 commit into from

Conversation

stepnem
Copy link

@stepnem stepnem commented Nov 21, 2023

Unlike tools like code formatters or linters where a single
unified config maintained as part of a code repository or
directory tree makes sense, for a search tool it is more
ergonomic to always load user customization as a base for
further directory-specific adjustments.

Before this, one had to copy and maintain common
customizations in each directory-specific .ugrep file.

Now one can put generic settings like coloring, line
numbering etc. in the $HOME .ugrep, and only maintain
dir/repo-specific settings like includes/excludes in
the relevant local .ugrep files.

NB: This is an RFC/PoC: it works for me and the test suite
passes, but I'm not very familiar with either the code base
or C++. I have also only included a minimal doc change, as
there is a lot of redundancy in existing documentation;
if we agree this PR is viable, I can include further
documentation updates.

Unlike tools like code formatters or linters where a single
unified config maintained as part of a code repository or
directory tree makes sense, for a search tool it is more
ergonomic to always load user customization as a base for
further directory-specific adjustments.

Before this, one had to copy and maintain common
customizations in each directory-specific .ugrep file.

Now one can put generic settings like coloring, line
numbering etc. in the $HOME .ugrep, and only maintain
dir/repo-specific settings like includes/excludes in
the relevant local .ugrep files.
@genivia-inc
Copy link
Member

genivia-inc commented Nov 21, 2023

I had thought about this before a while back, but didn't like it. The reason is that --save-config uses the home directory .ugrep file anyway to generate a new .ugrep file in the working directory. Therefore, normally we don't need to read the home dir .ugrep and the working dir .ugrep in these "normal" scenarios.

If ugrep is upgraded with new options (as in 4.0), then it might make a small difference to read both if only the home dir .ugrep is modified. Also if you're updating the home dir .ugrep, then just as likely there will be scenarios in which the working dir .ugrep should not be affected (i.e. don't read both).

@stepnem
Copy link
Author

stepnem commented Nov 21, 2023 via email

@genivia-inc
Copy link
Member

The --save-config is typically used with options supplied to override the current options. That's the simplest way to use ug --save-config OPTIONS (as shown in the docs and on ugrep.com) to enforce the given OPTIONS. Sure, the .ugrep file can be edited if need be, but it is not required and I never needed to do that.

Some options aren't saved by --safe-config because these options impact the search in ways that are likely causing problems, such as conflicting options. For example, option -P is not saved because the user may want to use -Z instead that conflicts.

@genivia-inc
Copy link
Member

One thing we should probably do is renaming of the old .ugrep to .ugrep.old in case the user changes his/her mind, like this:

// save a configuration file
static void save_config()
{
  FILE *file = NULL;

  // rename old .ugrep to .ugrep.old when present
  if (strcmp(flag_save_config, ".ugrep") == 0 && std::rename(".ugrep", ".ugrep.old") == 0)
  {
    errno = EEXIST;
    warning("renamed existing .ugrep to .ugrep.old", NULL);
  }

  // if not saved to standard output ("-"), then inform user
  if (!flag_no_messages && strcmp(flag_save_config, "-") != 0)
    fprintf(stderr, "ugrep: saving configuration file %s\n", flag_save_config);

This also reports saving the new configuration file.

I really would like to get suggestions and feedback on these kind of little things that can make a difference.

@stepnem
Copy link
Author

stepnem commented Nov 22, 2023 via email

@genivia-inc
Copy link
Member

If anyone really wants to inherit .ugrep configurations, why not add an #include directive that reads another configuration file? It's not difficult to add such a feature. Or perhaps allow config=FILE in a .ugrep file to load another config FILE. But we need to watch out for duplicate/circular references to handle them properly and also decide what to do when a config file fails to open (probably abort).

On Tue, 21 Nov 2023 08:10:56 -0800 Robert van Engelen wrote: The --save-config is typically used with options supplied to override the current options. That's the simplest way to use ug --save-config OPTIONS (as shown in the docs and on ugrep.com) to enforce the given OPTIONS. Sure, the .ugrep file can be edited if need be, but it is not required and I never needed to do that. Some options aren't saved by --safe-config because these options impact the search in ways that are likely causing problems, such as conflicting options. For example, option -P is not saved because the user may want to use -Z instead that conflicts.

Thank you for the explanation. This sounds rather opaque and complex, basically a kind of hard-coded DWIM logic.

If running ug with an option that abort with an error message and requires editing the .ugrep file to make ug work again, how is that a bad ("opaque") thing? Using conflicting options fails or aborts with an error message, like GNU grep. Some options can interfere negatively.

If we allow --save-config to save certain options that can conflict with others, then we will probably see a lot of issues opened in this repo that complain that something with ugrep is not working in some dir but in other dirs work fine.

It can be worse if files are no longer found and are unexpectedly silently ignored, because of interference, e.g. specifying conflicting patterns or file types to (not) search for example.

You can explicitly specify anything in a .ugrep file if you want, since any of the long options can be specified, but we should not do this automatically with --save-config. It is better to be cautious than to be overly adventurous.

If you want transparency, then I invite you to add warning messages when options are specified on the command line that are not saved to .ugrep.

@stepnem
Copy link
Author

stepnem commented Nov 22, 2023 via email

@genivia-inc
Copy link
Member

There are alternate designs in this case, all with pros and cons. It's all about balancing the pros and cons. It's not about who is right or wrong. Inheriting config files sounds great to me, but it is easy to overlook the (unintended) consequences. For one, when ug no longer works as expected with this approach, then it becomes much harder to figure out why when two config files are involved in combination with the command-line options passed to the failing ug invocation.

Note that sift uses config files that are saved with an option and only saves a subset of options (I guess for the same reasons I've outlined.) But sift does have two configs, which in my opinion is unnecessary when saving a stand-alone config file. By contrast, I wanted the config file to be portable to mounted drives and to other user's drives and not make things more complicated. The sift --write-config option is supposed to be used for this but the config file won't be stand-alone (editing a sift config file is possible, but it's JSON and not expected). Sift also has --print-config to figure out what options are actually enabled when the two configs are read. Keeping things as simple as possible is better IMHO.

Yeah, I'd say that being explicit about it is the least the tool should do, but given how alien the whole --save-config concept is to me I'd hesitate to play the judge here. Perhaps there is user demographic that welcomes the current behavior?

Right. I totally lost you after this one. No need to be sarcastic or rude. A serious technical conversation doesn't benefit from this kind of attitude. My motto is to be helpful in a respectful way and not be confrontational on matters of opinion.

@stepnem
Copy link
Author

stepnem commented Nov 22, 2023 via email

genivia-inc added a commit that referenced this pull request Nov 24, 2023
- ug no longer quits with an error message when no default .ugrep config file was found
- allow config file importing in config files using config=FILE (does not permit recursive imports) see also #320
- fix the output of + separators by no longer using them #317
- fix -v with -ABC context #319
- fix configuration file option arguments that may got lost and causes option argument errors in some cases after parsing a config file, such as colors=
@genivia-inc
Copy link
Member

I've added the ability to the ugrep v4.3.4 release to specify config and config=FILE in config files to import a config file. See also the v4.3.4 release notes.

The manual is updated to explain the use of this approach as an alternative instead of --save-config. Also the explanation and use of --save-config is updated in the manual.

I believe this update offers the best of both worlds.

Rationale: changing the behavior of --config and the ug command is not something to be done without a very good reason. Others may use config files in ways that may break after an update, when the update reads two config files by default.

@stepnem
Copy link
Author

stepnem commented Nov 25, 2023

I've added the ability to the ugrep v4.3.4 release to specify config and config=FILE in config files to import a config file. See also the v4.3.4 release notes.

That works for me, thank you!

@stepnem stepnem closed this Nov 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants