Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packaging/linux/deb/debroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ func fixupGenerators(spec *dalec.Spec, cfg *SourcePkgConfig) []byte {
buf := bytes.NewBuffer(nil)
writeScriptHeader(buf, cfg)
if spec.HasGomods() {
fmt.Fprintf(buf, "tar --skip-old-files -xzf %s\n", revisionSourcesArchive)
// Older go versions did not have support for the `GOMODCACHE` var
// This is a hack to try and make the build work by linking the go modules
// we've already fetched into to module dir under $GOPATH
Expand Down
47 changes: 38 additions & 9 deletions packaging/linux/deb/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"path/filepath"
"regexp"
"strings"

"github.com/moby/buildkit/client/llb"
"github.com/pkg/errors"
Expand All @@ -12,8 +14,8 @@ import (

const (
// Unique name that would not normally be in the spec
// This will get used to create the source tar for go module deps
gomodsName = "xxxdalecGomodsInternal"
gomodsName = "xxxdalecGomodsInternal"
revisionSourcesArchive = "debian/dalec/revision-sources.tar.gz"
// Unique name that would not normally be in the spec
// This will get used to create the source tar for cargo deps
cargohomeName = "xxxdalecCargoHomeInternal"
Expand Down Expand Up @@ -106,9 +108,7 @@ func SourcePackage(ctx context.Context, sOpt dalec.SourceOpts, worker llb.State,

gomodSt := spec.GomodDeps(sOpt, worker, opts...)
if gomodSt != nil {
st := gomodSt.With(nestState(gomodsName, opts...))
gomodSt = &st

dr = addGomodRevisionSources(spec, sources, worker, dr, *gomodSt, opts...)
}

cargohomeSt := spec.CargohomeDeps(sOpt, worker, opts...)
Expand All @@ -130,10 +130,6 @@ func SourcePackage(ctx context.Context, sOpt dalec.SourceOpts, worker llb.State,
sources[key] = srcsWithNodeMods[key]
}

if gomodSt != nil {
sources[gomodsName] = *gomodSt
}

if cargohomeSt != nil {
sources[cargohomeName] = *cargohomeSt
}
Expand All @@ -159,6 +155,39 @@ func SourcePackage(ctx context.Context, sOpt dalec.SourceOpts, worker llb.State,
return work.AddMount("/tmp/out", llb.Scratch())
}

func addGomodRevisionSources(spec *dalec.Spec, sources map[string]llb.State, worker, dr, gomods llb.State, opts ...llb.ConstraintsOpt) llb.State {
// Go dependencies are generated from patched sources and can change with a
// Debian revision. Keep them and patch inputs out of the shared orig tarball.
revisionSources := map[string]llb.State{gomodsName: gomods.With(nestState(gomodsName, opts...))}
for _, patches := range spec.Patches {
for _, patch := range patches {
// A patch source can also be an upstream source that has its own
// patches or generators. It must still be available in the orig tarball.
if len(spec.Patches[patch.Source]) > 0 || len(spec.Sources[patch.Source].Generate) > 0 {
continue
}
if src, ok := sources[patch.Source]; ok {
revisionSources[patch.Source] = src
delete(sources, patch.Source)
}
}
}

var states []llb.State
var names []string
for name, st := range dalec.SortedMapIter(revisionSources) {
states = append(states, st)
names = append(names, strings.ReplaceAll(regexp.QuoteMeta(name), "\n", `\n`))
}
archive := dalec.Tar(worker, dalec.MergeAtPath(llb.Scratch(), states, "/", opts...), revisionSourcesArchive, opts...)
// A binary build restores these inputs at their usual paths. Ignore them
// if dpkg-source is run again in that tree.
ignore := `extend-diff-ignore = "^(?:` + strings.Join(names, "|") + `)(?:/|$)"` + "\n"
dr = dr.File(llb.Mkfile("debian/source/options", 0o640, []byte(ignore)), opts...).
File(llb.Mkfile("debian/source/include-binaries", 0o640, []byte("dalec\n"+revisionSourcesArchive+"\n")), opts...)
return dr.File(llb.Copy(archive, revisionSourcesArchive, revisionSourcesArchive), opts...)
}

func BuildDebBinaryOnly(worker llb.State, spec *dalec.Spec, debroot llb.State, distroVersionID string, opts ...llb.ConstraintsOpt) llb.State {
dirName := filepath.Join("/work", spec.Name+"_"+spec.Version+"-"+spec.Revision)
st := worker.
Expand Down
71 changes: 60 additions & 11 deletions test/source_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,16 @@ func testPatchesFromSameSource(ctx context.Context, t *testing.T, targetCfg targ
})
}

// testSourceOutputAppliesGomodEdits verifies that spec preprocessing (gomod
// replace directives) is applied when producing the deb source package output.
// The handler previously skipped Preprocess, silently emitting a source package
// missing the generated gomod patch.
// testSourceOutputAppliesGomodEdits checks that the Debian source target
// includes the generated Go patch and that a dependency change between Debian
// revisions does not prevent them from sharing one upstream archive.
func testSourceOutputAppliesGomodEdits(ctx context.Context, t *testing.T, targetCfg targetConfig) {
if !strings.HasSuffix(targetCfg.Package, "/deb") {
t.Skip("source-package preprocessing check only implemented for deb /dsc")
}
sourceTarget := strings.TrimSuffix(targetCfg.Package, "/deb") + "/dsc"

strip := 1
spec := &dalec.Spec{
Name: "test-dalec-dsc-preprocess",
Version: "0.0.1",
Expand Down Expand Up @@ -249,24 +249,68 @@ func testSourceOutputAppliesGomodEdits(ctx context.Context, t *testing.T, target
},
},
},
"revision.patch": {Inline: &dalec.SourceInline{File: &dalec.SourceInlineFile{Contents: `--- /dev/null
+++ b/revision
@@ -0,0 +1 @@
+2
`}}},
},
Patches: map[string][]dalec.PatchSpec{
"src": {{Source: "revision.patch", Strip: &strip}},
},
Dependencies: &dalec.PackageDependencies{
Build: map[string]dalec.PackageConstraints{
targetCfg.GetPackage("golang"): {},
},
},
Build: dalec.ArtifactBuild{
Steps: []dalec.BuildStep{{Command: "cd src && go build -o ../testgomodsource"}},
},
Artifacts: dalec.Artifacts{
Binaries: map[string]dalec.ArtifactConfig{"testgomodsource": {}},
},
}

testEnv.RunTest(ctx, t, func(ctx context.Context, gwc gwclient.Client) {
original := *spec
src := spec.Sources["src"]
src.Generate = []*dalec.SourceGenerator{{Gomod: &dalec.GeneratorGomod{}}}
original.Sources = map[string]dalec.Source{"src": src}
original.Patches = nil
spec.Revision = "2"
first := solveT(ctx, t, gwc, newSolveRequest(withSpec(ctx, t, &original), withBuildTarget(sourceTarget)))
res := solveT(ctx, t, gwc, newSolveRequest(withSpec(ctx, t, spec), withBuildTarget(sourceTarget)))
origName := spec.Name + "_" + spec.Version + ".orig.tar.gz"
assert.Assert(t, bytes.Equal(readFile(ctx, t, origName, first), readFile(ctx, t, origName, res)),
"the same upstream version must use the same orig archive after a dependency update")

// Pull dalec-changes.patch out of the source package and confirm the
// gomod replace directive made it in (i.e. Preprocess ran).
st := llb.Image("alpine:latest").
Run(dalec.ShArgs("apk add --no-cache tar xz")).Root().
worker := solveT(ctx, t, gwc, newSolveRequest(withBuildTarget(targetCfg.Worker), withSpec(ctx, t, nil)))
st := resultToState(t, worker).
Run(dalec.ShArgs("apt-get update && apt-get install -y --no-install-recommends "+targetCfg.GetPackage("golang"))).Root().
Network(llb.NetModeNone).
Run(
dalec.ShArgs(`set -e; mkdir -p /out /x; for f in /src/*.debian.tar.xz; do tar -xJf "$f" -C /x; done; cp /x/debian/patches/dalec-changes.patch /out/patch`),
llb.AddMount("/src", resultToState(t, res), llb.Readonly),
// Rebuild both revisions from a single orig tarball with no network.
dalec.ShArgs(`set -eu
for revision in 1 2; do
mkdir -p "/work/$revision"
cd "/work/$revision"
cp /first/*.orig.tar.gz .
if [ "$revision" = 1 ]; then
cp /first/*.dsc /first/*.debian.tar.xz .
else
cp /second/*.dsc /second/*.debian.tar.xz .
fi
dpkg-source -x ./*.dsc source
cd source
GOPROXY=off dpkg-buildpackage -b -uc -us
go version -m testgomodsource > "/out/deps-$revision"
done
test "$(cat src/revision)" = 2
test -f revision.patch
cp debian/patches/dalec-changes.patch /out/patch
`),
llb.AddMount("/first", resultToState(t, first), llb.Readonly),
llb.AddMount("/second", resultToState(t, res), llb.Readonly),
).AddMount("/out", llb.Scratch())

def, err := st.Marshal(ctx)
Expand All @@ -277,6 +321,9 @@ func testSourceOutputAppliesGomodEdits(ctx context.Context, t *testing.T, target
patch := string(readFile(ctx, t, "patch", out))
assert.Check(t, strings.Contains(patch, "replace github.com/cpuguy83/tar2go"),
"source package patch must contain the gomod replace directive (Preprocess must run for source packages), got:\n%s", patch)
const module = "github.com/cpuguy83/tar2go"
assert.Check(t, strings.Contains(string(readFile(ctx, t, "deps-1", out)), "dep\t"+module+"\tv0.3.1"))
assert.Check(t, strings.Contains(string(readFile(ctx, t, "deps-2", out)), "=>\t"+module+"\tv0.3.0"))
})
}

Expand Down Expand Up @@ -425,7 +472,9 @@ tar -xzf src.tar.gz -C /out
_, err := fs.Stat(pkgFS, name)
assert.NilError(t, err, "expected source-package artifact %q", name)
}
extract = fmt.Sprintf("set -eu; dpkg-source -x %q /out/source", path.Join("/pkg", descriptors[0]))
extract = fmt.Sprintf(`set -eu
dpkg-source -x %q /out/source
tar -xzf /out/source/debian/dalec/revision-sources.tar.gz -C /out/source`, path.Join("/pkg", descriptors[0]))
gomodsRoot, sourceRoot = "source/xxxdalecGomodsInternal", "source/src"
}

Expand Down