---
id: w1/m18/t002
title: BuildKit subdir context threading
worker: worker1
status: done
estimate: 45m
depends_on: [w1/m18/t001]
---

## Objective

Extend the operator's build engine so a non-empty `RootDir` makes BuildKit build from that subdirectory of the cloned repo, using BuildKit's native git-context subdir syntax.

## Context

- `lego/operator/internal/build/build.go` — `Options` struct (~lines 65-77) currently has `Repo`, `Ref`, no context/subdirectory field.
- `gitContext(repo, ref string) string` (lines 214-230) builds the `context=` opt passed to `buildctl` at line 148/158: today it produces `<repo>.git#<ref>`.
- BuildKit's dockerfile frontend supports a subdir suffix on the git context: `<repo>#<ref>:<subdir>` — appending `:<subdir>` after the ref makes BuildKit build (and resolve the Dockerfile) relative to that subdirectory.
- Existing unit tests for `gitContext` live in `lego/operator/internal/build/build_test.go` (e.g. asserts `context=https://github.com/bex-co/hello.git#main`).

## Steps

1. Add `RootDir string` to `build.Options`.
2. Change `gitContext` (or add a variant) to accept `rootDir` and append `:<rootDir>` (with any leading/trailing slashes trimmed) to the context string when non-empty, leaving today's `<repo>.git#<ref>` output unchanged when `rootDir` is empty.
3. Update the call site in `BuildJob` to pass `o.RootDir` through.
4. Extend `build_test.go` to cover the new subdir suffix (both the case with and without a ref set).

## Files

- `lego/operator/internal/build/build.go`
- `lego/operator/internal/build/build_test.go`

## Acceptance criteria

- [x] `gitContext` (or equivalent) appends `:<rootDir>` only when `RootDir` is set; empty `RootDir` produces byte-identical output to today
- [x] New/updated unit tests cover both branches and pass
- [x] `go test ./...` in `lego/operator/` is green

## Out of scope

- Wiring `RootDir` from the `App` CR into `build.Options` (t003) — this task only extends the build-engine mechanism, testable in isolation.
- Buildpack/CNB root-dir support (buildpack builder is not yet in-cluster; out of scope until kpack lands).
