Skip to content

Commit 4beea52

Browse files
committed
gomod: key go-includes' and gohdr's builds by their files' contents
go-includes was built from this module's source as it is, so its build was keyed by the whole of dagger/go: every dagger/go version, even one that leaves go-includes as it is, built it again and ran every scan again. gohdr avoided that by writing its source's text into the recipe. Both now build from toolSource: a new directory of the tool's Go files that aren't tests', with its go.mod and go.sum, each keyed by its content with File.sync. A file's content key is its bytes and its path in the source, and a tool sits at the same path in every dagger/go, so a version that leaves a tool as it is finds its build. gohdr's source text no longer goes into the recipe. toolKeyCheck replaces gohdrKeyCheck: it builds both tools from two dagger/go sources that differ only in a file outside the tools, and checks that an exec mounting each one runs once. With the sources used as they are, each exec runs twice. Signed-off-by: Erik Sipsma <[email protected]>
1 parent 8e371f2 commit 4beea52

2 files changed

Lines changed: 53 additions & 22 deletions

File tree

‎gomod/.dagger/modules/e2e/main.dang‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,22 +346,34 @@ type E2e {
346346
}
347347

348348
"""
349-
gohdr's build is keyed by its own source alone: built from two directories
350-
that hold the same gohdr but were made differently, as the sources of two
351-
dagger/go versions that leave gohdr as it is are, an exec that mounts it
352-
runs once.
349+
The helper tools' builds are keyed by their own source alone: built from
350+
two dagger/go sources that differ only in a file outside the tools, as two
351+
dagger/go versions that leave the tools as they are do, an exec that mounts
352+
go-includes, or one that mounts gohdr, runs once.
353353
"""
354-
gohdrKeyCheck(ws: Workspace!): Void @check {
355-
let source = ws.directory("/gomod/helpers/gohdr")
356-
let remade = directory.withDirectory(".", source).withNewFile("unrelated", "").withoutFile("unrelated")
357-
let runs = [source, remade].map { dir =>
354+
toolKeyCheck(ws: Workspace!): Void @check {
355+
let sources = ["one", "two"].map { mark =>
356+
ws.directory("/", include: ["gomod/helpers/**"]).withNewFile("unrelated", mark)
357+
}
358+
let goIncludes = sources.map { source =>
359+
container
360+
.from("alpine:3.22")
361+
.withMountedFile(
362+
"/go-includes",
363+
gomod.includesHelperBinaryFrom(source.directory("gomod/helpers/go-includes"), gomod.hostPlatform),
364+
)
365+
.withExec(["cat", "/proc/sys/kernel/random/uuid"])
366+
.stdout
367+
}
368+
assert(goIncludes[0] == goIncludes[1], "an exec that mounts go-includes ran again for another source holding the same go-includes")
369+
let gohdr = sources.map { source =>
358370
container
359371
.from("alpine:3.22")
360-
.withMountedDirectory("/gohdr", gomod.gohdrFrom(dir))
372+
.withMountedDirectory("/gohdr", gomod.gohdrFrom(source.directory("gomod/helpers/gohdr")))
361373
.withExec(["cat", "/proc/sys/kernel/random/uuid"])
362374
.stdout
363375
}
364-
assert(runs[0] == runs[1], "an exec that mounts gohdr ran again for another source holding the same gohdr")
376+
assert(gohdr[0] == gohdr[1], "an exec that mounts gohdr ran again for another source holding the same gohdr")
365377
null
366378
}
367379

‎gomod/main.dang‎

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ type Gomod {
268268
engine image's, holds it.
269269
"""
270270
includesHelperBinary(platform: String!): File! {
271+
includesHelperBinaryFrom(currentModule.source.directory("helpers/go-includes"), platform)
272+
}
273+
274+
"""
275+
go-includes for a platform, as includesHelperBinary builds it, from source,
276+
the directory that holds it. Its build is keyed by the content of the files
277+
it builds from (toolSource), so a source that holds the same go-includes,
278+
such as another dagger/go's that leaves it as it is, gives the same build.
279+
"""
280+
includesHelperBinaryFrom(source: Directory!, platform: String!): File! {
271281
let target = platform.split("/")
272282
container.from(goImage(scannerVersion))
273283
# The same download retry as withGoCaches.
@@ -276,7 +286,7 @@ type Gomod {
276286
.withEnvVariable("GOARCH", target.takeFirst(2).takeLast(1).join(""))
277287
.withoutEntrypoint
278288
.withWorkdir("/helpers/go-includes")
279-
.withDirectory("/helpers/go-includes", currentModule.source.directory("helpers/go-includes"))
289+
.withDirectory("/helpers/go-includes", toolSource(source))
280290
# A fresh engine compiles the standard library for this build, and
281291
# skipping inlining there cuts the build by about a quarter. The scan
282292
# loses about 25 ms without inlining, and the GC setting it runs with
@@ -301,19 +311,28 @@ type Gomod {
301311
}
302312

303313
"""
304-
gohdr, as gohdr builds it, from source, a directory holding its go.mod
305-
and main.go. Its build is keyed by those two files' contents alone: it is
306-
written out from them, so a source that holds the same gohdr, such as
307-
another dagger/go's that leaves gohdr as it is, gives the same build.
308-
Read as the directory it is, a module's source would key it by the whole
309-
module, and every module download, plan and link that mounts it would run
310-
again for each dagger/go.
314+
gohdr, as gohdr builds it, from source, the directory that holds it. Its
315+
build is keyed by the content of the files it builds from (toolSource), so
316+
a source that holds the same gohdr, such as another dagger/go's that leaves
317+
gohdr as it is, gives the same build.
311318
"""
312319
gohdrFrom(source: Directory!): Directory! {
313-
let files = directory
314-
.withNewFile("go.mod", source.file("go.mod").contents)
315-
.withNewFile("main.go", source.file("main.go").contents)
316-
machineTools("gohdr", files, ".")
320+
machineTools("gohdr", toolSource(source), ".")
321+
}
322+
323+
# What a tool of this module is built from: the Go files of the main package
324+
# in source that aren't tests', and its go.mod and go.sum, each keyed by its
325+
# content. A module's source is keyed by the whole module, so a tool built
326+
# from it would be built again, and every exec that mounts it would run
327+
# again, for each dagger/go, even one that leaves the tool as it is. A file
328+
# keyed by its content is the same for any source that holds the same bytes
329+
# at the same path, as every dagger/go's source does for a tool it leaves as
330+
# it is.
331+
let toolSource(source: Directory!): Directory! {
332+
let names = source.glob("*").filter { name =>
333+
(name.hasSuffix(".go") and name.hasSuffix("_test.go") == false) or name == "go.mod" or name == "go.sum"
334+
}
335+
directory.withFiles(".", names.map { name => source.file(name).sync(keyByContent: true) })
317336
}
318337

319338
"""

0 commit comments

Comments
 (0)