twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
12.9k stars 478 forks source link

`MozillaInstallHash` hash value keeps changing on Gentoo with `www-client/firefox-bin` #2882

Closed xfzv closed 1 year ago

xfzv commented 1 year ago

Describe the bug

I use Gentoo. I recently switched from www-client/firefox to www-client/firefox-bin and have the following issue ever since.

I'm using mozillaInstallHash variable in my profiles.ini.tmpl to get the hash value for my profile (on Gentoo, www-client/firefox-bin installs to /opt/firefox so I made my profiles.ini a template as I'm also using Arch Linux on the same machine).

{{- $releaseId := "0" -}}

{{- if eq .distro "archlinux" -}}
{{- $releaseId = mozillaInstallHash "/usr/lib/firefox" -}}
{{- else if eq .distro "gentoo" -}}
{{- $releaseId = mozillaInstallHash "/opt/firefox" -}}
{{- end -}}

[Profile0]
Name={{ .machine }}-{{ .distro }}-main
IsRelative=1
Path=default-release
Default=1

[General]
StartWithLastProfile=1
Version=2

[Install{{- $releaseId -}}]
Default=default-release
Locked=1

To reproduce

  1. Remove everything in ~/.mozilla/firefox but the profile directory (~/.mozilla/firefox/default-release in my case)
  2. Run chezmoi apply
  3. At this point, here's ~/.mozilla/firefox/profiles.ini content:
[Profile0]
Name=desktop-gentoo-main
IsRelative=1
Path=default-release
Default=1

[General]
StartWithLastProfile=1
Version=2

[Install06AFDA46A1A8AD48]
Default=default-release
Locked=1
  1. Run Firefox: it starts with a new profile created in ~/.mozilla/firefox
  2. ~/.mozilla/firefox/profiles.ini content is then updated with the following:
[Install06AFDA46A1A8AD48]
Default=default-release
Locked=1

[Profile1]
Name=default-release
IsRelative=1
Path=nhwr0bvw.default-release

[Profile0]
Name=desktop-gentoo-main
IsRelative=1
Path=default-release
Default=1

[General]
StartWithLastProfile=1
Version=2

[Install6AFDA46A1A8AD48]
Default=nhwr0bvw.default-release
Locked=1

Note the missing "0" at the beginning of the freshly created profile hash (not sure why).

Alternatively, if I first run firefox-bin -P and select my profile in the list instead of directly launching Firefox, it starts my profile fine, but just once. After launching Firefox, ~/.mozilla/firefox/profiles.ini content is also updated with the following:

[Install06AFDA46A1A8AD48]
Default=default-release
Locked=1

[Profile0]
Name=desktop-gentoo-main
IsRelative=1
Path=default-release
Default=1

[General]
StartWithLastProfile=1
Version=2

[Install6AFDA46A1A8AD48]
Default=default-release
Locked=1

Expected behavior

profiles.ini isn't modified after closing Firefox and no new profile is created.

Output of command with the --verbose flag

$ chezmoi --verbose $COMMAND

Output of chezmoi doctor

```console $ chezmoi doctor RESULT CHECK MESSAGE warning version v2.32.0, built at 2023-03-23T16:12:43Z ok latest-version v2.32.0 ok os-arch linux/amd64 (Gentoo) ok uname Linux gentoo 6.2.8-gentoo-custom #1 SMP PREEMPT_DYNAMIC Thu Mar 23 10:10:10 CET 2023 x86_64 Intel(R) Core(TM) i7-5820K CPU @3.30GHz GenuineIntel GNU/Linux ok go-version go1.20.2 (gc) ok executable /usr/bin/chezmoi ok upgrade-method sudo-upgrade-package ok config-file ~/.config/chezmoi/chezmoi.yaml, last modified 2022-10-18T20:21:09+02:00 warning source-dir ~/dotfiles is a git working tree (dirty) warning suspicious-entries ~/dotfiles/.chezmoi.yml.tmpl and ~/dotfiles/.chezmoiexternal.yml warning working-tree ~/dotfiles is a git working tree (dirty) ok dest-dir ~ is a directory ok umask 022 ok cd-command found /bin/zsh ok cd-args /bin/zsh ok diff-command found /usr/bin/delta ok edit-command found /usr/bin/nvim ok edit-args /usr/bin/nvim ok git-command found /usr/bin/git, version 2.40.0 ok merge-command found /usr/bin/nvim ok shell-command found /bin/zsh ok shell-args /bin/zsh info age-command age not found in $PATH ok gpg-command found /usr/bin/gpg, version 2.4.0 info pinentry-command not set info 1password-command op not found in $PATH info bitwarden-command bw not found in $PATH info dashlane-command dcli not found in $PATH info gopass-command gopass not found in $PATH ok keepassxc-command found /usr/bin/keepassxc-cli, version 2.7.4 info keepassxc-db not set info keeper-command keeper not found in $PATH info lastpass-command lpass not found in $PATH info pass-command pass not found in $PATH info passhole-command ph not found in $PATH info rbw-command rbw not found in $PATH info vault-command vault not found in $PATH info secret-command not set ```

Additional context

$ chezmoi data | rg "distro"

"distro": "gentoo",

I didn't have the issue before with www-client/firefox.

Here are the two Gentoo ebuilds for reference:

Thanks in advance for any suggestion.

bradenhilton commented 1 year ago

Can you backup the .mozilla directory somewhere, then delete it and launch Firefox without writing profiles.ini with chezmoi? I'm curious to see what it looks like.

Can you run chezmoi execute-template '{{ mozillaInstallHash "/opt/firefox" }}' on gentoo and provide the result?

The profile ID for the path /opt/firefox should be 06AFDA46A1A8AD48. I have no idea why the zero is being removed, though I feel like that might be causing the issue. If it is, I'm not entirely sure that chezmoi or MozillaInstallHash are at fault here.

xfzv commented 1 year ago

Can you backup the .mozilla directory somewhere, then delete it and launch Firefox without writing profiles.ini with chezmoi? I'm curious to see what it looks like.

[Profile1]
Name=default
IsRelative=1
Path=961hs9qt.default
Default=1

[Profile0]
Name=default-release
IsRelative=1
Path=vqpporu0.default-release

[General]
StartWithLastProfile=1
Version=2

[Install6AFDA46A1A8AD48]
Default=vqpporu0.default-release
Locked=1

Can you run chezmoi execute-template '{{ mozillaInstallHash "/opt/firefox" }}' on gentoo and provide the result?

❯ chezmoi execute-template '{{ mozillaInstallHash "/opt/firefox" }}'
06AFDA46A1A8AD48%

The profile ID for the path /opt/firefox should be 06AFDA46A1A8AD48. I have no idea why the zero is being removed, though I feel like that might be causing the issue. If it is, I'm not entirely sure that chezmoi or MozillaInstallHash are at fault here.

I don't have any issue with my Arch Linux profile after more testing.

I'll move back to www-client/firefox for now and see what happens.

twpayne commented 1 year ago

Note that, as a work around, you can use a template function to remove leading zeros:

{{- $releaseId = mozillaInstallHash "/opt/firefox" | replaceAllRegex "^0+" "" -}}
xfzv commented 1 year ago

Note that, as a work around, you can use a template function to remove leading zeros:

{{- $releaseId = mozillaInstallHash "/opt/firefox" | replaceAllRegex "^0+" "" -}}

Thank you, I've just tried this and it does the trick!

I removed everything in ~/.mozilla/firefox except my profile directory, edited profiles.ini.tmpl with your suggested change and run chezmoi apply. I launched/closed Firefox and ran chezmoi apply multiple times, all good.

I don't understand why the leading zero is being removed either. Is this a Firefox issue then?

Feel free to close in this case.

twpayne commented 1 year ago

I don't understand why the leading zero is being removed either. Is this a Firefox issue then?

I suspect that Firefox has (maybe unintentionally) changed its behavior here, but @bradenhilton is the expert.

bradenhilton commented 1 year ago

@twpayne

❯ chezmoi execute-template '{{ mozillaInstallHash "C:\\Program Files\\aaa" }}'
0C5C3528386AC660

I just installed Firefox on my laptop to C:\Program Files\aaa, and the profiles.ini says the hash is C5C3528386AC660, so I think the package needs updating.

I can't recall if a path's hash can have a length above 16.

Is it possible to have Sprintf treat the length as a max length, rather than a fixed length? Or should we just keep it as is and manually trim the zeroes post hash generation?

twpayne commented 1 year ago

Is it possible to have Sprintf treat the length as a max length, rather than a fixed length?

Probably, but having a max length normally truncates the end of the string, whereas here we want remove bytes from the start of the string.

From your investigation, I agree that we should update the mozillainstallhash package.

bradenhilton commented 1 year ago

I think we get the max length of 16 for free from the format code itself, so I can revert the fixed length as a quick fix, but for a more proper fix I'll have to see if I can find what exactly Mozilla is doing with the hash. Might take a while though as the code base is huge.

I'll add tests for some known good paths and hashes also, instead of just hashing "Hello world".

Would you like me to add you as a collaborator as well, @twpayne? No expectations ever.

twpayne commented 1 year ago

This all sounds great!

Would you like me to add you as a collaborator as well, @twpayne? No expectations ever.

Yes, happy to help!