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

Prevent fetchGit from using incorrect cached rev for different refs #7174

Merged
merged 2 commits into from Oct 26, 2022

Conversation

agbrooks
Copy link

Background

This is a fix for #7146.

When using fetchGit on a non-local repository and using a ref (but no rev), fetchGit will decide that any cached rev for that repository is usable (if not too stale).

This is potentially really disruptive. If a user uses fetchGit to fetch both the nixos-22.05 and nixos-21.11 branches of nixpkgs back-to-back, and they don't specify a rev, they'll wind up with nixos-22.05 sources in both cases (!!):

nix-repl> builtins.fetchGit { url = "git@github.com:nixos/nixpkgs"; ref = "refs/heads/nixos-22.05"; }
{ lastModified = 1665613119; lastModifiedDate = "20221012221839"; narHash = "sha256-VTutbv5YKeBGWou6ladtgfx11h6et+Wlkdyh4jPJ3p0="; outPath = "/nix/store/xc6s7iccz5nkm8vl9nybz698nq2ngxlc-source"; rev = "e06bd4b64bbfda91d74f13cb5eca89485d47528f"; revCount = 383579; shortRev = "e06bd4b"; submodules = false; }

nix-repl> builtins.fetchGit { url = "git@github.com:nixos/nixpkgs"; ref = "refs/heads/nixos-21.11"; } 
{ lastModified = 1665613119; lastModifiedDate = "20221012221839"; narHash = "sha256-VTutbv5YKeBGWou6ladtgfx11h6et+Wlkdyh4jPJ3p0="; outPath = "/nix/store/xc6s7iccz5nkm8vl9nybz698nq2ngxlc-source"; rev = "e06bd4b64bbfda91d74f13cb5eca89485d47528f"; revCount = 383579; shortRev = "e06bd4b"; submodules = false; }

The fix

The fix I'm suggesting is pretty straightforward: when no rev is provided for a remote repo fetched with fetchGit, incorporate the ref into the input attrs to differentiate cached revs between different refs.

To illustrate, here's the fetcher cache after running the example above in Nix 2.9.1:

sqlite3 ~/.cache/nix/fetcher-cache-v1.sqlite 'SELECT * FROM Cache;'
input                                                                            info                                                                                            path                                                immutable  timestamp 
-------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------  --------------------------------------------------  ---------  ----------
{"name":"source","type":"git","url":"ssh://git@github.com/nixos/nixpkgs"}        {"lastModified":1665613119,"rev":"e06bd4b64bbfda91d74f13cb5eca89485d47528f","revCount":383579}  /nix/store/xc6s7iccz5nkm8vl9nybz698nq2ngxlc-source  0          1665789700
{"name":"source","rev":"e06bd4b64bbfda91d74f13cb5eca89485d47528f","type":"git"}  {"lastModified":1665613119,"rev":"e06bd4b64bbfda91d74f13cb5eca89485d47528f","revCount":383579}  /nix/store/xc6s7iccz5nkm8vl9nybz698nq2ngxlc-source  1          1665789700

and this is how it looks on my bugfix branch:

sqlite3 ~/.cache/nix/fetcher-cache-v1.sqlite 'SELECT * FROM Cache;'
input                                                                                                     info                                                                                            path                                                immutable  timestamp 
--------------------------------------------------------------------------------------------------------  ----------------------------------------------------------------------------------------------  --------------------------------------------------  ---------  ----------
{"name":"source","ref":"refs/heads/nixos-22.05","type":"git","url":"ssh://git@github.com/nixos/nixpkgs"}  {"lastModified":1665613119,"rev":"e06bd4b64bbfda91d74f13cb5eca89485d47528f","revCount":383579}  /nix/store/xc6s7iccz5nkm8vl9nybz698nq2ngxlc-source  0          1665789958
{"name":"source","rev":"e06bd4b64bbfda91d74f13cb5eca89485d47528f","type":"git"}                           {"lastModified":1665613119,"rev":"e06bd4b64bbfda91d74f13cb5eca89485d47528f","revCount":383579}  /nix/store/xc6s7iccz5nkm8vl9nybz698nq2ngxlc-source  1          1665789958
{"name":"source","ref":"refs/heads/nixos-21.11","type":"git","url":"ssh://git@github.com/nixos/nixpkgs"}  {"lastModified":1659446231,"rev":"eabc38219184cc3e04a974fe31857d8e0eac098d","revCount":337975}  /nix/store/9ks8076lanzwp09dmwy5r3racihsc1ip-source  0          1665790070
{"name":"source","rev":"eabc38219184cc3e04a974fe31857d8e0eac098d","type":"git"}                           {"lastModified":1659446231,"rev":"eabc38219184cc3e04a974fe31857d8e0eac098d","revCount":337975}  /nix/store/9ks8076lanzwp09dmwy5r3racihsc1ip-source  1          1665790070

Andrew Brooks added 2 commits October 14, 2022 17:27
When fetching a non-local git repo by ref (and no rev), don't consider unrelated
cached revs for the same repository.
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/prs-in-distress/3604/55

Copy link
Member

@thufschmitt thufschmitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix. It's indeed much more correct to include the ref in the cache key 👍

@agbrooks
Copy link
Author

@thufschmitt: Is there anything else to address before this can be merged?

I have a few co-workers who have been affected by #7146 , so I'd be happy to make any changes needed.

@edolstra edolstra merged commit 9323d13 into NixOS:master Oct 26, 2022
@thufschmitt
Copy link
Member

Is there anything else to address before this can be merged?

Nope sorry, I guess I just forgot to press the merge button 😬

Thanks @edolstra for the merge :)

@edolstra
Copy link
Member

I merged this into the lazy-trees PR by deleting the changes to git.cc, and the test kept working, so I guess this bug was already fixed on that branch...

@agbrooks
Copy link
Author

Just manually tested the example in the description with your lazy-trees branch to rule out an issue with the test -- your branch did indeed fix it. :)

@Yuhanun
Copy link

Yuhanun commented Dec 9, 2022

Heyo we're seeing some weird behavior happening within our production landscape and we feel like it relates to this PR.

getFlake on a remote flake using a ref= in the URL causes it to ignore the ref= and pull master instead.

We do not specify a rev=, we just specify git+https://.../?ref=...

It works again if we rm -rf ~/.cache/nix

This issue started occuring when we upgraded our engineers from 2.4 to 2.11.1

@Radvendii
Copy link
Contributor

This seems like a major issue. Should it be back-ported to earlier versions of nix? it seems to have started in nix 2.9

Minion3665 pushed a commit to Minion3665/nix that referenced this pull request Feb 23, 2023
Prevent fetchGit from using incorrect cached rev for different refs
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.

None yet

7 participants