Skip to content

Fix dockerfile git worktree - #2171

Closed
shellyco-code wants to merge 1 commit into
Project-HAMi:masterfrom
shellyco-code:fix-dockerfile-git-worktree
Closed

Fix dockerfile git worktree#2171
shellyco-code wants to merge 1 commit into
Project-HAMi:masterfrom
shellyco-code:fix-dockerfile-git-worktree

Conversation

@shellyco-code

@shellyco-code shellyco-code commented Jul 28, 2026

Copy link
Copy Markdown

What type of PR is this?
/kind bug

What this PR does / why we need it:
This PR resolves an issue where the Docker builds (docker/Dockerfile and docker/Dockerfile.hamicore) would fail early when building from a linked Git worktree. The build context assumed that .git/modules/libvgpu would always exist as a directory, which is false in worktree environments where .git is a file.

By removing the hardcoded COPY .git/modules/libvgpu steps, we make the Dockerfiles portable across all supported Git working tree layouts. The internal libvgpu/build.sh script already handles missing git metadata gracefully by defaulting CI_COMMIT_SHA to "unknown".

Which issue(s) this PR fixes:
Fixes #2041

Special notes for your reviewer:
This ensures compatibility with developers who use git worktree for maintaining multiple branches simultaneously without needing multiple full clones.

Does this PR introduce a user-facing change?:

NONE


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

* **Bug Fixes**
  * Docker build commands now work reliably when run from Git worktrees.
  * Repository metadata is restored automatically after successful or failed builds, preventing changes to the local checkout.
  * Improved handling of the `libvgpu` submodule during container builds.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

@hami-robot hami-robot Bot added kind/bug Something isn't working dco-signoff: yes labels Jul 28, 2026
@hami-robot
hami-robot Bot requested review from FouoF and archlitchi July 28, 2026 10:52
@hami-robot

hami-robot Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: shellyco-code
Once this PR has been reviewed and has the lgtm label, please assign shouren for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hami-robot hami-robot Bot added the size/M label Jul 28, 2026
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Docker build entry points now support Git worktrees by relocating .git, recreating libvgpu submodule metadata, and restoring .git after successful or failed builds.

Changes

Worktree-safe Docker builds

Layer / File(s) Summary
Git metadata build flow
Makefile, hack/build.sh
The Docker targets and docker_build temporarily relocate .git, recreate local libvgpu submodule metadata, and restore .git after successful or failed builds.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: archlitchi, mesutoezdil

Poem

A rabbit moved .git with care,
Then built a Docker image there.
Submodule paths were set just right,
Restored by day and guarded night.
“Worktrees build!” the rabbit cheered.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The changes support worktree builds but retain the Dockerfiles' hardcoded Git metadata dependency instead of removing it as required by issue #2041. Update the Dockerfiles to remove hardcoded .git/modules/libvgpu copies and pass required libvgpu metadata explicitly.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the Dockerfile Git worktree compatibility fix.
Out of Scope Changes check ✅ Passed The Makefile and hack/build.sh changes directly address Docker build behavior from linked Git worktrees.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai
coderabbitai Bot requested a review from mesutoezdil July 28, 2026 10:53

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/device-plugin/nvidiadevice/nvinternal/rm/health_checkhealth_test.go`:
- Around line 29-33: Update the nvmlResourceManager initialization in the
health-check test to set healthCheckXIDs using getHealthCheckXids("all", "")
instead of the nonexistent resourceManager.disableHealthChecks field, preserving
the early-return behavior before accessing the nil NVML interface.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 19d9c30d-9e00-484b-b23a-a67b1ab24d25

📥 Commits

Reviewing files that changed from the base of the PR and between 37730dd and aec9056.

📒 Files selected for processing (9)
  • docker/Dockerfile
  • docker/Dockerfile.hamicore
  • pkg/device-plugin/nvidiadevice/nvinternal/plugin/factory.go
  • pkg/device-plugin/nvidiadevice/nvinternal/rm/health.go
  • pkg/device-plugin/nvidiadevice/nvinternal/rm/health_checkhealth_test.go
  • pkg/device-plugin/nvidiadevice/nvinternal/rm/health_test.go
  • pkg/device-plugin/nvidiadevice/nvinternal/rm/nvml_manager.go
  • pkg/device-plugin/nvidiadevice/nvinternal/rm/rm.go
  • pkg/device-plugin/nvidiadevice/nvinternal/rm/tegra_manager.go
💤 Files with no reviewable changes (2)
  • docker/Dockerfile
  • docker/Dockerfile.hamicore

Comment on lines +29 to +33
r := &nvmlResourceManager{
resourceManager: resourceManager{
disableHealthChecks: "all",
},
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

Initialize the declared XID-selection field.

Line 31 references nonexistent resourceManager.disableHealthChecks, so this test does not compile. Set healthCheckXIDs: getHealthCheckXids("all", "") instead; that also preserves the intended early return before using the nil NVML interface.

Proposed fix
 resourceManager: resourceManager{
-    disableHealthChecks: "all",
+    healthCheckXIDs: getHealthCheckXids("all", ""),
 },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
r := &nvmlResourceManager{
resourceManager: resourceManager{
disableHealthChecks: "all",
},
}
r := &nvmlResourceManager{
resourceManager: resourceManager{
healthCheckXIDs: getHealthCheckXids("all", ""),
},
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/device-plugin/nvidiadevice/nvinternal/rm/health_checkhealth_test.go`
around lines 29 - 33, Update the nvmlResourceManager initialization in the
health-check test to set healthCheckXIDs using getHealthCheckXids("all", "")
instead of the nonexistent resourceManager.disableHealthChecks field, preserving
the early-return behavior before accessing the nil NVML interface.

@mesutoezdil mesutoezdil left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this branch carries #2151 too, c4847d0 and 3cd40b7 are both in here and only aec9056 is the docker fix. worth rebasing onto master so this pr is one commit.

related: 3cd40b7 is titled "address PR feedback for health check config", which is the commit u said u pushed to #2151 on the 28th. it landed here instead, #2151 is still on c4847d0 and still fails lint on the unused os import.

Comment thread docker/Dockerfile
RUN rm -rf /libvgpu/.git && echo "gitdir: /libvgpu-git" > /libvgpu/.git
WORKDIR /libvgpu
RUN rm -rf /libvgpu/build
RUN bash ./build.sh

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

dropping the copy means git describe fails in every build, not just worktrees, so CI_COMMIT_SHA falls back to unknown for released images too. #2042 tried to keep it by resolving the real gitdir, what does libvgpu report as its version after this?

}()

return rm.NewNVMLResourceManagers(o.infolib, o.nvmllib, o.devicelib, o.config.Config)
return rm.NewNVMLResourceManagers(o.infolib, o.nvmllib, o.devicelib, o.config.Config, disableHealthChecks, enableHealthChecks)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please remove changes that are unrelated to the pr title

Signed-off-by: shellyco-code <shellychahar57@gmail.com>
@shellyco-code
shellyco-code force-pushed the fix-dockerfile-git-worktree branch from aec9056 to 40e2192 Compare July 31, 2026 08:33
@coderabbitai
coderabbitai Bot requested a review from mesutoezdil July 31, 2026 08:34
@shellyco-code

Copy link
Copy Markdown
Author

@mesutoezdil ,@DSFans2014 PTAL

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
Makefile (1)

42-48: 📐 Maintainability & Code Quality | 🔵 Trivial | 🏗️ Heavy lift

Centralize the repeated worktree setup.

docker, dockerwithlib, and hack/build.sh:docker_build each mutate .git. Their quoting and failure handling already differ. Move relocation and restoration into one helper to prevent future divergence and reduce the dockerwithlib target length.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Makefile` around lines 42 - 48, Centralize the `.git` worktree relocation and
restoration currently duplicated across the `docker`, `dockerwithlib`, and
`docker_build` flows into a shared Makefile helper or callable function. Update
each caller to use that helper, preserving the existing worktree detection,
`.git/modules/libvgpu` setup, quoting, and failure-handling behavior while
shortening the `dockerwithlib` target.

Source: Linters/SAST tools

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Makefile`:
- Around line 24-30: Make the worktree metadata relocation failure-safe in
Makefile entries at Makefile lines 24-39 and 42-58, and in hack/build.sh lines
37-54: before renaming .git, register an idempotent cleanup handler that can
restore it, separately validate git rev-parse and the real_gitdir value, and
check mkdir and metadata-copy operations. Ensure every setup failure invokes
cleanup and restores metadata within the same shell.
- Line 29: Update the cp commands in the docker and dockerwithlib targets to
quote both expansions of REAL_GITDIR, including the source path to
modules/libvgpu, so worktree paths containing whitespace remain a single
argument.

---

Nitpick comments:
In `@Makefile`:
- Around line 42-48: Centralize the `.git` worktree relocation and restoration
currently duplicated across the `docker`, `dockerwithlib`, and `docker_build`
flows into a shared Makefile helper or callable function. Update each caller to
use that helper, preserving the existing worktree detection,
`.git/modules/libvgpu` setup, quoting, and failure-handling behavior while
shortening the `dockerwithlib` target.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: dab7ba3b-02d2-4171-a01d-f66a9adbc3c8

📥 Commits

Reviewing files that changed from the base of the PR and between aec9056 and 40e2192.

📒 Files selected for processing (2)
  • Makefile
  • hack/build.sh

Comment thread Makefile
Comment on lines +24 to +30
@if [ -f .git ]; then \
echo "Detected git worktree, preparing .git/modules/libvgpu"; \
REAL_GITDIR=$$(git rev-parse --git-common-dir); \
mv .git .git.bak; \
mkdir -p .git/modules; \
cp -r $$REAL_GITDIR/modules/libvgpu .git/modules/libvgpu; \
fi

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== files =="
git ls-files | rg '(^|/)Makefile$|hack/build\.sh$' || true

echo "== Makefile context =="
if [ -f Makefile ]; then
  nl -ba Makefile | sed -n '1,70p'
fi

echo "== build.sh context =="
if [ -f hack/build.sh ]; then
  nl -ba hack/build.sh | sed -n '1,90p'
fi

echo "== git status/diff =="
git status --short || true
git diff --stat || true

Repository: Project-HAMi/HAMi

Length of output: 269


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== files =="
git ls-files | grep -E '(^|/)Makefile$|hack/build\.sh$' || true

echo "== Makefile relevant context =="
awk 'NR>=1 && NR<=80 { printf "%4d\t%s\n", NR, $0 }' Makefile

echo "== charts Makefile relevant context =="
awk 'NR>=1 && NR<=80 { printf "%4d\t%s\n", NR, $0 }' charts/Makefile

echo "== hack/build.sh relevant context =="
awk 'NR>=1 && NR<=90 { printf "%4d\t%s\n", NR, $0 }' hack/build.sh

echo "== git status/diff =="
git status --short || true
git diff --stat || true

Repository: Project-HAMi/HAMi

Length of output: 7841


Make worktree metadata relocation failure-safe in every entry point.

Each implementation moves .git before cleanup is guaranteed. If git rev-parse, mkdir -p .git/modules, or the metadata copy fails, the checkout can be left without .git. Install an idempotent cleanup handler before the rename, check every setup command and variable, and restore metadata from the same shell.

  • Makefile#L24-L39 and Makefile#L42-L58: Register cleanup before mv .git .git.bak, then restore on setup failure.
  • hack/build.sh#L37-L54: Declare and check real_gitdir separately, install cleanup before mv, and restore metadata from the same shell.
📍 Affects 2 files
  • Makefile#L24-L30 (this comment)
  • Makefile#L42-L48
  • hack/build.sh#L37-L44
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Makefile` around lines 24 - 30, Make the worktree metadata relocation
failure-safe in Makefile entries at Makefile lines 24-39 and 42-58, and in
hack/build.sh lines 37-54: before renaming .git, register an idempotent cleanup
handler that can restore it, separately validate git rev-parse and the
real_gitdir value, and check mkdir and metadata-copy operations. Ensure every
setup failure invokes cleanup and restores metadata within the same shell.

Source: Linters/SAST tools

Comment thread Makefile
REAL_GITDIR=$$(git rev-parse --git-common-dir); \
mv .git .git.bak; \
mkdir -p .git/modules; \
cp -r $$REAL_GITDIR/modules/libvgpu .git/modules/libvgpu; \

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf 'Makefiles:\n'
git ls-files | grep -E '(^|/)Makefile$|(^|/)makefile$' || true

printf '\nRelevant Makefile excerpts:\n'
if [ -f Makefile ]; then
  nl -ba Makefile | sed -n '1,70p'
fi

printf '\nSearch REAL_GITDIR usages:\n'
rg -n '\bREAL_GITDIR\b|\bdockerwithlib\b|\blibvgpu\b' -S .

Repository: Project-HAMi/HAMi

Length of output: 260


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf 'Relevant Makefile excerpts:\n'
awk '{printf "%6d\t%s\n", NR, $0}' Makefile | sed -n '1,70p'

printf '\nSearch REAL_GITDIR usages:\n'
rg -n '\bREAL_GITDIR\b|\bdockerwithlib\b|\blibvgpu\b' -S .

Repository: Project-HAMi/HAMi

Length of output: 6405


🏁 Script executed:

#!/bin/bash
set -euo pipefail

tmp_dir="$(mktemp -d)"
worktree_parent="$tmp_dir/parent with spaces"
real_gitdir="$worktree_parent/gitdir with space"
mkdir -p "$worktree_parent/.git/modules"
mkdir -p "$real_gitdir/modules/libvgpu/subdir"
printf 'hello\n' > "$real_gitdir/modules/libvgpu/subdir/test.txt"

bash -c '
unset REAL_GITDIR; export REAL_GITDIR="'"$real_gitdir"'"; rm -f test_file; cp -r "$REAL_GITDIR/modules/libvgpu" .git/modules/libvgpu
' bashworkdir "$worktree_parent"

find "$worktree_parent" -type f -print | sort | while read -r f; do
  rel="${f#$worktree_parent/}"
  printf '%s\t' "$rel"
  if [ -f "$f" ]; then cat -- "$f"; else printf '[dir]\n'; fi
done

rm -rf "$tmp_dir"

Repository: Project-HAMi/HAMi

Length of output: 233


🏁 Script executed:

#!/bin/bash
set -euo pipefail

tmp_dir="$(mktemp -d)"
worktree_parent="$tmp_dir/parent with spaces"
real_gitdir="$worktree_parent/gitdir with space"
mkdir -p "$worktree_parent/.git/modules"
mkdir -p "$real_gitdir/modules/libvgpu/subdir"
printf 'hello\n' > "$real_gitdir/modules/libvgpu/subdir/test.txt"

bash -c '
unset REAL_GITDIR; export REAL_GITDIR="'"$real_gitdir"'"; rm -f test_file; mkdir -p .git/modules; cp -r "$REAL_GITDIR/modules/libvgpu" .git/modules/libvgpu
' bashworkdir "$worktree_parent"

find "$worktree_parent" -mindepth 1 -print | sort | while read -r f; do
  rel="${f#$worktree_parent/}"
  printf '%s\t' "$rel"
  if [ -f "$f" ]; then cat -- "$f"; else printf '[dir]\n'; fi
done

rm -rf "$tmp_dir"

Repository: Project-HAMi/HAMi

Length of output: 384


Quote both REAL_GITDIR expansions.

A git worktree under a path with whitespace splits the unquoted cp source into multiple arguments. Quote the source in both docker and dockerwithlib.

Proposed fix
-		cp -r $$REAL_GITDIR/modules/libvgpu .git/modules/libvgpu; \
+		cp -r "$$REAL_GITDIR/modules/libvgpu" .git/modules/libvgpu; \
...
-		cp -r $$REAL_GITDIR/modules/libvgpu .git/modules/libvgpu; \
+		cp -r "$$REAL_GITDIR/modules/libvgpu" .git/modules/libvgpu; \
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Makefile` at line 29, Update the cp commands in the docker and dockerwithlib
targets to quote both expansions of REAL_GITDIR, including the source path to
modules/libvgpu, so worktree paths containing whitespace remain a single
argument.

@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 62.93% <ø> (+0.92%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.
see 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@FouoF

FouoF commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for working on Git worktree builds. The current approach temporarily renames the checkout's .git entry to .git.bak. That can leave the repository metadata displaced when the copy fails or the process is interrupted, and it is unsafe when builds run concurrently. The existing major review concern is therefore a correctness issue, not a minor cleanup request. We are closing this implementation. A replacement must leave the working tree metadata untouched and include a real worktree-build regression test.

@FouoF FouoF closed this Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] docker build fails from linked git worktree because Dockerfiles assume .git/modules/libvgpu exists

4 participants