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

fetchers/git: make relative path absolute for local repo (backport #12107) #12265

Open
wants to merge 4 commits into
base: 2.24-maintenance
Choose a base branch
from

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Jan 15, 2025

Motivation

Closes #9708 (again).

This patch:

  • (naively) Resolves relative paths to local git repos, returning absolute paths, thus allowing flake URIs such as git+file:./${submodule} to continue working.
  • Adds tests to prevent future breakages.

Context

Flake URIs such as git+file:./${submodule} used to work before 2.18, but broke in 2.19, was fixed later, and broke again recently since 3e0129c, resulting in a core-dumped assertion error in #9708 (comment).

This PR suppresses the error and allows git+file:./${submodule}to continue working. However, this fix is not ideal and should potentially be superseded by a better submodule implementation in the future. See:

A better fix is outlined by @roberth in https://discourse.nixos.org/t/57783, namely:

This bug seems to have allowed relative git flake inputs to be resolved against the current working directory (as in POSIX), and this tends to work out ok in the context of flakes, but is the wrong behavior, as it should resolve against the flake.nix base directory instead.
Supporting relative paths in fetchTree would add significant value.
This behavior was used as a workaround for lacking relative flake input or “subflake” support.


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

Friendly pings:


This is an automatic backport of pull request #12107 done by [Mergify](https://mergify.com).

bryango and others added 4 commits January 15, 2025 19:55
(cherry picked from commit 96bd9ba)

# Conflicts:
#	src/libfetchers/git.cc
Relative, local git repo used to work (for submodules), but it
fails after 3e0129c.

This commit adds a test to prevent such failure in the future.

(cherry picked from commit 9d088fa)
(cherry picked from commit 37ac18d)

# Conflicts:
#	tests/functional/flakes/flake-in-submodule.sh
Copy link
Contributor Author

mergify bot commented Jan 15, 2025

Cherry-pick of 96bd9ba has failed:

On branch mergify/bp/2.24-maintenance/pr-12107
Your branch is up to date with 'origin/2.24-maintenance'.

You are currently cherry-picking commit 96bd9bad2.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/libfetchers/git.cc

no changes added to commit (use "git add" and/or "git commit -a")

Cherry-pick of 37ac18d has failed:

On branch mergify/bp/2.24-maintenance/pr-12107
Your branch is ahead of 'origin/2.24-maintenance' by 2 commits.
  (use "git push" to publish your local commits)

You are currently cherry-picking commit 37ac18d1d.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   tests/functional/flakes/flake-in-submodule.sh

no changes added to commit (use "git add" and/or "git commit -a")

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@mergify mergify bot requested a review from edolstra as a code owner January 15, 2025 19:55
@github-actions github-actions bot added with-tests Issues related to testing. PRs with tests have some priority fetching Networking with the outside (non-Nix) world, input locking labels Jan 15, 2025
Copy link
Member

@bryango bryango left a comment

Choose a reason for hiding this comment

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

@roberth proposed resolutions of backport conflicts!

Comment on lines +428 to +441
<<<<<<< HEAD
repoInfo.url = repoInfo.isLocal ? url.path : url.base;
=======
//
// FIXME: here we turn a possibly relative path into an absolute path.
// This allows relative git flake inputs to be resolved against the
// **current working directory** (as in POSIX), which tends to work out
// ok in the context of flakes, but is the wrong behavior,
// as it should resolve against the flake.nix base directory instead.
//
// See: https://discourse.nixos.org/t/57783 and #9708
//
repoInfo.url = repoInfo.isLocal ? std::filesystem::absolute(url.path).string() : url.to_string();
>>>>>>> 96bd9bad2 (fetchers/git: make path absolute for local repo)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<<<<<<< HEAD
repoInfo.url = repoInfo.isLocal ? url.path : url.base;
=======
//
// FIXME: here we turn a possibly relative path into an absolute path.
// This allows relative git flake inputs to be resolved against the
// **current working directory** (as in POSIX), which tends to work out
// ok in the context of flakes, but is the wrong behavior,
// as it should resolve against the flake.nix base directory instead.
//
// See: https://discourse.nixos.org/t/57783 and #9708
//
repoInfo.url = repoInfo.isLocal ? std::filesystem::absolute(url.path).string() : url.to_string();
>>>>>>> 96bd9bad2 (fetchers/git: make path absolute for local repo)
//
// FIXME: here we turn a possibly relative path into an absolute path.
// This allows relative git flake inputs to be resolved against the
// **current working directory** (as in POSIX), which tends to work out
// ok in the context of flakes, but is the wrong behavior,
// as it should resolve against the flake.nix base directory instead.
//
// See: https://discourse.nixos.org/t/57783 and #9708
//
repoInfo.url = repoInfo.isLocal ? std::filesystem::absolute(url.path).string() : url.to_string();

Comment on lines +66 to +99
<<<<<<< HEAD
=======

# Test that `nix flake metadata` parses `submodule` correctly.
cat > "$rootRepo"/flake.nix <<EOF
{
outputs = { self }: {
};
}
EOF
git -C "$rootRepo" add flake.nix
git -C "$rootRepo" commit -m "Add flake.nix"

storePath=$(nix flake metadata --json "$rootRepo?submodules=1" | jq -r .path)
[[ -e "$storePath/submodule" ]]

# The root repo may use the submodule repo as an input
# through the relative path. This may change in the future;
# see: https://discourse.nixos.org/t/57783 and #9708.
cat > "$rootRepo"/flake.nix <<EOF
{
inputs.subRepo.url = "git+file:./submodule";
outputs = { ... }: { };
}
EOF
git -C "$rootRepo" add flake.nix
git -C "$rootRepo" commit -m "Add subRepo input"
(
cd "$rootRepo"
# The submodule must be locked to the relative path,
# _not_ the absolute path:
[[ $(nix flake metadata --json | jq -r .locks.nodes.subRepo.locked.url) = "file:./submodule" ]]
)
>>>>>>> 37ac18d1d (tests/flake-in-submodule: git+file:./* input)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<<<<<<< HEAD
=======
# Test that `nix flake metadata` parses `submodule` correctly.
cat > "$rootRepo"/flake.nix <<EOF
{
outputs = { self }: {
};
}
EOF
git -C "$rootRepo" add flake.nix
git -C "$rootRepo" commit -m "Add flake.nix"
storePath=$(nix flake metadata --json "$rootRepo?submodules=1" | jq -r .path)
[[ -e "$storePath/submodule" ]]
# The root repo may use the submodule repo as an input
# through the relative path. This may change in the future;
# see: https://discourse.nixos.org/t/57783 and #9708.
cat > "$rootRepo"/flake.nix <<EOF
{
inputs.subRepo.url = "git+file:./submodule";
outputs = { ... }: { };
}
EOF
git -C "$rootRepo" add flake.nix
git -C "$rootRepo" commit -m "Add subRepo input"
(
cd "$rootRepo"
# The submodule must be locked to the relative path,
# _not_ the absolute path:
[[ $(nix flake metadata --json | jq -r .locks.nodes.subRepo.locked.url) = "file:./submodule" ]]
)
>>>>>>> 37ac18d1d (tests/flake-in-submodule: git+file:./* input)
# Test that `nix flake metadata` parses `submodule` correctly.
cat > "$rootRepo"/flake.nix <<EOF
{
outputs = { self }: {
};
}
EOF
git -C "$rootRepo" add flake.nix
git -C "$rootRepo" commit -m "Add flake.nix"
storePath=$(nix flake metadata --json "$rootRepo?submodules=1" | jq -r .path)
[[ -e "$storePath/submodule" ]]
# The root repo may use the submodule repo as an input
# through the relative path. This may change in the future;
# see: https://discourse.nixos.org/t/57783 and #9708.
cat > "$rootRepo"/flake.nix <<EOF
{
inputs.subRepo.url = "git+file:./submodule";
outputs = { ... }: { };
}
EOF
git -C "$rootRepo" add flake.nix
git -C "$rootRepo" commit -m "Add subRepo input"
(
cd "$rootRepo"
# The submodule must be locked to the relative path,
# _not_ the absolute path:
[[ $(nix flake metadata --json | jq -r .locks.nodes.subRepo.locked.url) = "file:./submodule" ]]
)

@edolstra
Copy link
Member

To be honest, I'd prefer to hold off on backporting this PR, since the behaviour of making the fetcher depend on the cwd seems extremely questionable to me. (See #12107 (comment).)

@bryango
Copy link
Member

bryango commented Jan 17, 2025

Agree, let's see how:

... are finalized and then decide what to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conflicts fetching Networking with the outside (non-Nix) world, input locking merge-queue with-tests Issues related to testing. PRs with tests have some priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants