Repository: gohugoio/hugo (language: go)

Issue:
[snap] `hugo server` fails if ~/.gitconfig includes other gitconfig files
<!-- Please answer these questions before submitting your issue. Thanks! -->

### What version of Hugo are you using (`hugo version`)?

<pre>
$ hugo version
hugo v0.104.1-8958b8741f552c8024af5194330fbf031544a826+extended linux/amd64 BuildDate=2022-09-26T17:05:45Z VendorInfo=snap:0.104.1

</pre>

### Does this issue reproduce with the latest release?

Yes (on snap, Ubuntu 20.04 LTS)

### Steps to reproduce

1. On Ubuntu 20.04 LTS, install hugo with `snap install hugo --channel=extended`
1. Replace `~/.gitconfig` with:
  ```ini
  [include]
  	path = .gitconfig.local
  ```
2. Add the following to `~/.gitconfig.local`:
  ```ini
  [user]
  	name = John Smith
  	email = user@example.com
  ```
3. `chmod a+r ~/.gitconfig.local ~/.gitconfig`
3. Run `hugo server`
4. The following error message appears:
  ```
  hugo v0.104.1-8958b8741f552c8024af5194330fbf031544a826+extended linux/amd64 BuildDate=2022-09-26T17:05:45Z VendorInfo=snap:0.104.1
  ERROR 2022/09/29 12:36:01 Failed to read Git log: warning: unable to access '/home/[REDACTED]/.gitconfig.local': Permission denied
  fatal: bad config line 5 in file /home/[REDACTED]/.gitconfig
  WARN 2022/09/29 12:36:01 Expand shortcode is deprecated. Use 'details' instead.
  Error: Error building site: logged 1 error(s)
  Built in 732 ms
  ```

  ### Workaround

  Follow reproduce steps, but replace `hugo server` with `HUGO_ENABLEGITINFO=false hugo server`.

  Failure does not occur.


Discussion / hints:
Is it specific to snap ? Can you reproduce in a docker so we have a easily actionnable reproduction?

Can't reproduce on latest so either it is specific to snap or just fixed.
@Et7f3 Yes, it is specific to Snap.  We use the [`home` interface](https://snapcraft.io/docs/home-interface) which "allows access to **non-hidden** files owned by the user in the to be user's home ($HOME) directory", and recently added the `personal-files` interface](https://snapcraft.io/docs/personal-files-interface) to allow access to `$HOME/.gitconfig` and `$HOME/.config/git/config` which are both **hidden**:

https://github.com/gohugoio/hugo/blob/58b824581360148f2d91f5cc83f69bd22c1aa331/snap/snapcraft.yaml#L22-L31

`$HOME/.gitconfig.local` is not in that list, in snapcraft.yaml, thus it is not readable.

@pandruszkow Instead of including an arbitrarily named `$HOME/.gitconfig.local` file, how about placing its content (e.g. `[user]` section) into `$HOME/.config/git/config` instead?  From the `git config` man page:

> $XDG_CONFIG_HOME/git/config
>            Second user-specific configuration file. If $XDG_CONFIG_HOME is not
>            set or empty, `$HOME/.config/git/config` will be used. Any
>            single-valued variable set in this file will be overwritten by
>            whatever is in `~/.gitconfig`.

Let me know if this works for you or not!  Many thanks!

It affected me as well. I end up remove hugo from snap and installed through apt.
Having installed hugo through snap (on PopOS! 22.04 LTS) I experienced the same issue.
@anthonyfok I am using `$HOME/.config/git/config` which doesn't work either

```sh
$ hugo
Start building sites … 
hugo v0.109.0-47b12b83e636224e5e601813ff3e6790c191e371+extended linux/amd64 BuildDate=2022-12-23T10:38:11Z VendorInfo=snap:0.109.0
ERROR 2022/12/26 11:59:17 Failed to read Git log: warning: unable to access '/home/[user]/.config/git/config': Permission denied
warning: unable to access '/home/[user]/.config/git/config': Permission denied
fatal: unknown error occurred while reading the configuration files
Error: Error building site: logged 1 error(s)
Total in 343 ms```
> @pandruszkow Instead of including an arbitrarily named $HOME/.gitconfig.local file, how about placing its content (e.g. [user] section) into $HOME/.config/git/config instead? From the git config man page:

`$HOME/.gitconfig.local` is no more more arbitrary than `$HOME/.gitconfig` or `$HOME/.config/git/config`. After all, paths under $HOME are just a convention, and there are legitimate reasons why users may wish to allow access to hidden files to confined applications, or use non-default paths for files. Locating config files in specific locations under my own home directory was simply not something that I've ever had to think about up until now. I don't like the way Snap deals with the issue, without even allowing the user to grant a confined application access to additional paths.

I replaced the Snap version with a DEB package. It's working flawlessly now.


> `$HOME/.gitconfig.local` is no more more arbitrary than `$HOME/.gitconfig` or `$HOME/.config/git/config`.

Except that `git` is programmed to read either `$HOME/.gitconfig` or `$XDG_CONFIG_HOME/git/config` (thus usually `$HOME/.config/git/config`), and these are documented in the git(1) man page.

`$HOME/.gitconfig.local`, on the other hand, is defined by the end user.  In that sense, something like `$HOME/.gitconfig.local` is no more arbitrary than, for example, `$HOME/.gitconfig.anthonyfok`.  Either of these user-defined paths would require adding a manual `include` path.

Not that there is anything wrong with that.  It is just that Snap's security model seemingly throws us a monkey wrench here.

> After all, paths under $HOME are just a convention, and there are legitimate reasons why users may wish to allow access to hidden files to confined applications, or use non-default paths for files.

This I completely agreed.  What I meant "arbitrary" above is more in the context of the Snap packaging where snapcraft.yaml currently does not allow us to use a wildcard in the `personal-files` interface permitted file list if I understand correctly.

> I replaced the Snap version with a DEB package. It's working flawlessly now.

I'm glad you came to this conclusion, as the .deb package is what I personally use and what [I personally maintain for Debian](https://tracker.debian.org/pkg/hugo) and derivatives.  I should have recommended that you switched to the .deb package in the first place.  :sweat_smile:
@anthonyfok Due to your extensive efforts to resolve https://github.com/gohugoio/hugo/issues/9078, the Hugo snap package works very well for the vast majority of users. Everything is included; it just works. And the fact that it includes Embedded Dart Sass makes it easy for site authors to transition from LibSass (deprecated) to Dart Sass.

I really like the snap package, and wanted to thank you again for your efforts.
@jmooring Thank you for your kind words and confidence boost!  I almost forgot #9078, and I am glad that it is working well for most use cases!
Let's see how to finally close this issue by making the `hugo:gitconfig` (`personal-files` interface) a bit more flexible.  Here are some reference documentation and discussions:

* [The personal-files interface - doc - snapcraft.io](https://forum.snapcraft.io/t/the-personal-files-interface/9357)
* universal-ctags/ctags-snap#4
* [SOLVED Help needed using 'personal-files' interface - snapcraft - snapcraft.io](https://forum.snapcraft.io/t/solved-help-needed-using-personal-files-interface/14031)

Here is a quick summary:

* `personal-files` does not support wildcard. This is by design.
* `personal-files` does support directory, so adding `$HOME/.config/git` gives read access to `$HOME/.config/git/config` and any arbitrary files inside the `$HOME/.config/git` directory.
* Top-level `$HOME` does not work.  Again, this is by design, for security reasons.  I tried `$HOME`, `$HOME/`, '$HOME/.`, and sure enough, none of these works.
* `$HOME/.gitconfig.local` seems nicely named, so I'll add this exception too.
* To test which files or directories are readable or not, simply run `snap run --shell hugo`.
* `git config -l --show-origin` is a great command for testing if git config `[include] path` is working properly or not. 

So, here's what I'm going to do:

* Allow `$HOME/.config/git/*` and `$HOME/.gitconfig.local` to work too
* Document the above (as well as other quirks of the Hugo Snap package) eventually; when to use Snap and when to use .deb.

Fix patch:
diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml
index c09eb98ae05..00ccc8abdfe 100644
--- a/snap/snapcraft.yaml
+++ b/snap/snapcraft.yaml
@@ -28,7 +28,8 @@ plugs:
     interface: personal-files
     read:
       - $HOME/.gitconfig
-      - $HOME/.config/git/config
+      - $HOME/.config/git       # Allows $HOME/.config/git/config and more
+      - $HOME/.gitconfig.local  # See #10337
 
 environment:
   HOME: $SNAP_REAL_HOME


Test patch: