-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgo.dang
More file actions
842 lines (767 loc) · 24.3 KB
/
Copy pathgo.dang
File metadata and controls
842 lines (767 loc) · 24.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
"""
A Dagger module for Go - a programming language for building simple, secure, scalable systems.
https://dagger.io
https://golang.org
"""
type Go {
"""
Go version used for the default Go base image. Null when a custom base is set.
"""
pub version: String
"""
Base image used for Go test, generate, and helper containers.
"""
pub base: Container!
new(
"""
Go version used for the default Go base image.
"""
version: String = null,
"""
Base image used for Go test, generate, and helper containers.
Mutually exclusive with version.
"""
base: Container = null,
"""
Extra workspace-root include patterns mounted for each module's Go commands.
Use this for fixtures, generator inputs, or other files not covered by the
built-in Go patterns.
"""
includeExtraFiles: [String!]! = [],
"""
Module roots to lint. A bare pattern selects, a "!"-prefixed pattern
excludes, and an exclude always wins (order does not matter). With no
selecting pattern, every module is linted. Supported patterns: "**" or "*"
(all modules), and "X" or "X/**" (module X and any module below it); other
glob forms (mid-path "**", single-segment "*") are not interpreted. For
example ["**", "!docs"] lints all but docs; ["e2e/**"] lints only e2e;
["!**"] lints nothing.
"""
lint: [String!]! = ["**"],
"""
Module roots to test. Same selection rules as `lint`.
"""
test: [String!]! = ["**"],
"""
Module roots to run go generate in. Same selection rules as `lint`.
"""
generate: [String!]! = ["**"],
) {
if (version != null and base != null) {
raise "version and base are mutually exclusive"
} else {
self.version = if (base == null) { version ?? "1.26" } else { null }
self.base = base ?? container.from("golang:" + (version ?? "1.26") + "-alpine")
self.includeExtraFiles = includeExtraFiles
self.lint = lint
self.test = test
self.generate = generate
self
}
}
"""
Extra workspace-root include patterns mounted for each module's Go commands.
Use this for fixtures, generator inputs, or other files not covered by the
built-in Go patterns.
"""
pub includeExtraFiles: [String!]! = []
"""
Module roots to lint ("!" excludes; see the `lint` constructor argument).
"""
pub lint: [String!]! = ["**"]
"""
Module roots to test ("!" excludes; see the `lint` constructor argument).
"""
pub test: [String!]! = ["**"]
"""
Module roots to run go generate in ("!" excludes; see `lint`).
"""
pub generate: [String!]! = ["**"]
"""
Return every Go module discovered from workspace go.mod files.
Optional include/exclude patterns filter discovered module root directories.
"""
pub modules(
ws: Workspace!,
include: [String!]! = [],
exclude: [String!]! = [],
includeSkipLint: Boolean! = true,
includeSkipTest: Boolean! = true,
includeSkipGenerate: Boolean! = true,
): [GoModule!]! {
ws
.directory("/", include: ["**/go.mod"])
.glob("**/go.mod")
.map { modPath =>
if (modPath == "go.mod") { "." } else { modPath.trimSuffix("/go.mod") }
}
.filter { modPath =>
modulePathIncluded(ws, modPath, include, exclude)
}
.map { modPath =>
GoModule(
path: modPath,
version: version,
baseImage: base,
includeExtraFiles: includeExtraFiles,
lintPatterns: lint,
testPatterns: test,
generatePatterns: generate,
)
}
.filter { mod =>
(includeSkipLint or mod.skipLint(ws) == false) and
(includeSkipTest or mod.skipTest(ws) == false) and
(includeSkipGenerate or mod.skipGenerate(ws) == false)
}
}
let modulePathIncluded(
ws: Workspace!,
modPath: String!,
include: [String!]!,
exclude: [String!]!,
): Boolean! {
if (include.length == 0 and exclude.length == 0) {
true
} else {
let includePatterns = if (include.length == 0) { ["**"] } else { include }
let glob = if (modPath == ".") { "**" } else { modPath.trimSuffix("/") + "/**" }
ws
.directory("/", include: includePatterns, exclude: exclude)
.glob(glob)
.length > 0
}
}
"""
Return the Go module containing a workspace path.
By default, path may be a module root or a directory inside a module. Set
findUp to false when path is already a module root and no lookup is needed.
"""
pub module(ws: Workspace!, path: String!, findUp: Boolean! = true): GoModule! {
GoModule(
path: if (findUp) {
let foundGoModPath = ws.findUp("go.mod", path)
if (foundGoModPath == null) {
raise "no Go module found containing path: " + path
} else {
let goModPath = foundGoModPath.trimPrefix("/")
if (goModPath == "go.mod") { "." } else {
goModPath.trimSuffix("/go.mod")
}
}
} else {
path.trimPrefix("/")
},
version: version,
baseImage: base,
includeExtraFiles: includeExtraFiles,
lintPatterns: lint,
testPatterns: test,
generatePatterns: generate,
)
}
"""
Run golangci-lint in every discovered Go module.
Modules configured to skip lint are skipped.
"""
pub lintAll(ws: Workspace!): Void @check {
let layers = modules(ws, includeSkipLint: false).reduce(directory) { layers, mod =>
layers.withDirectory(
mod.path,
directory.withFile("go.mod", mod.lintExec(ws).file("go.mod")),
)
}
# FIXME: replace this when Dagger can directly run multiple containers in
# parallel. For now, merge one file from each module's lint container into
# a directory and sync it to force every module lint command to run.
layers.sync
null
}
"""
Run tests in every discovered Go module.
Modules configured to skip tests are skipped.
"""
pub testAll(ws: Workspace!): Void @check {
let mods = modules(ws, includeSkipTest: false)
let layers = mods.reduce(directory) { layers, mod =>
layers.withDirectory(
mod.path,
directory.withFile("go.mod", mod.testExec(ws).file("go.mod")),
)
}
# FIXME: replace this when Dagger can directly run multiple containers in
# parallel. For now, merge one file from each module's test container into
# a directory and sync it to force every module test command to run.
layers.sync
null
}
"""
Run go generate in every discovered Go module.
Modules configured to skip generate, or without go:generate directives,
are skipped.
"""
pub generateAll(ws: Workspace!): Changeset! @generate {
let mods = modules(ws).filter { mod =>
mod.skipGenerate(ws) == false and mod.hasGenerateDirectives(ws)
}
# Snapshot each selected module before generation, using the same source
# boundary that its generate container will receive.
let before = mods.reduce(directory) { before, mod =>
before.withDirectory(mod.path, mod.source(ws, generate: true).directory(mod.path))
}
# Merge each generated module tree back into that snapshot, then diff once
# so callers get one changeset for the whole workspace.
let after = mods.reduce(before) { after, mod =>
after.withDirectory(mod.path, mod.generateExec(ws).directory("."))
}
after.changes(before)
}
}
# Keep all Dang types in one source file until Dagger v0.21.x can
# convert Workspace arguments for installed multi-file Dang toolchains.
# See dagger/dagger#13476.
"""
A Go module rooted at a workspace-relative path.
"""
type GoModule {
"""
Workspace-relative path of this module root.
"""
pub path: String!
"""
Go version configured on the root Go module. Null when a custom base is set.
"""
pub version: String
"""
Base image used by this module's Go containers.
"""
let baseImage: Container!
"""
Extra workspace-root include patterns mounted for this module's Go commands.
"""
pub includeExtraFiles: [String!]!
"""
Gitignore-style globs selecting which module roots to lint.
"""
let lintPatterns: [String!]!
"""
Gitignore-style globs selecting which module roots to test.
"""
let testPatterns: [String!]!
"""
Gitignore-style globs selecting which module roots to run go generate in.
"""
let generatePatterns: [String!]!
"""
Return a workspace-root path for a module-relative subpath.
"""
let subpath(relativePath: String!): String! {
if (path == ".") {
relativePath
} else {
path.trimSuffix("/") + "/" + relativePath
}
}
"""
Absolute workspace path for this module root.
"""
let workspacePath: String! {
if (path == ".") { "/" } else { "/" + path.trimSuffix("/") }
}
"""
Base container for the Go include helper.
"""
let goIncludesHelper(ws: Workspace!): Container! {
baseImage
.withoutEntrypoint
.withWorkdir("/helpers/go-includes")
.withMountedCache("/go/pkg/mod", cacheVolume("go-mod"))
.withMountedCache("/root/.cache/go-build", cacheVolume("go-build"))
.withDirectory("/helpers/go-includes", currentModule.source.directory("helpers/go-includes"))
.withExec(["go", "build", "-o", "/usr/local/bin/go-includes", "."])
.withDirectory("/ws", directory)
.withWorkdir("/ws")
.withEnvVariable("DAGGER_GO_WORKSPACE_ID", toJSON(ws.id))
}
"""
Whether this module falls outside the configured lint selection.
"""
pub skipLint(ws: Workspace!): Boolean! {
outOfScope(lintPatterns)
}
"""
Whether this module falls outside the configured test selection.
"""
pub skipTest(ws: Workspace!): Boolean! {
outOfScope(testPatterns)
}
"""
Whether this module falls outside the configured go generate selection.
"""
pub skipGenerate(ws: Workspace!): Boolean! {
outOfScope(generatePatterns)
}
"""
Whether this module's Go files contain a go:generate directive.
"""
pub hasGenerateDirectives(ws: Workspace!): Boolean! {
let source = ws.directory("/", include: [subpath("**/*.go")])
if (source.glob("**/*.go").length == 0) {
false
} else {
source
.search(
pattern: "//go:generate",
literal: true,
filesOnly: true,
limit: 1,
).{id}
.length > 0
}
}
"""
Whether go generate should run for this module.
"""
let shouldGenerate(ws: Workspace!): Boolean! {
skipGenerate(ws) == false and hasGenerateDirectives(ws)
}
"""
Whether this module root falls outside the selection `patterns`.
Matching is on the module root path only — no workspace files are read. A
bare pattern includes; a "!"-prefixed pattern excludes. With no include
pattern, every module is included. A module is in scope when it matches an
include and no exclude; an exclude always wins regardless of order.
"""
let outOfScope(patterns: [String!]!): Boolean! {
let included = patterns.filter { p => p.hasPrefix("!") == false }
let excluded = patterns.filter { p => p.hasPrefix("!") }.map { p => p.trimPrefix("!") }
let isIncluded = included.length == 0 or included.any { p => matchesModulePath(p) }
let isExcluded = excluded.any { p => matchesModulePath(p) }
isIncluded == false or isExcluded
}
"""
Whether this module root matches a single glob: "**"/"*" match everything,
"X/**" or bare "X" match X and any module below it.
"""
let matchesModulePath(pattern: String!): Boolean! {
let p = pattern.trimPrefix("/").trimSuffix("/")
if (p == "**" or p == "*") {
true
} else {
let prefix = p.trimSuffix("/**")
path == prefix or path.hasPrefix(prefix + "/")
}
}
"""
Helper container that computes includes for every module in one local pass
over a content-addressed Go-source snapshot of the workspace. It is identical
across all modules, so the engine runs it once and reuses the result; the
helper reads the snapshot from the local filesystem, so the exec caches on
source content and makes no workspace gateway round-trips.
"""
let allIncludesHelper(ws: Workspace!): Container! {
baseImage
.withoutEntrypoint
.withWorkdir("/helpers/go-includes")
.withMountedCache("/go/pkg/mod", cacheVolume("go-mod"))
.withMountedCache("/root/.cache/go-build", cacheVolume("go-build"))
.withDirectory("/helpers/go-includes", currentModule.source.directory("helpers/go-includes"))
.withExec(["go", "build", "-o", "/usr/local/bin/go-includes", "."])
.withDirectory("/ws", ws.directory("/", include: ["**/*.go", "**/go.mod"]))
.withWorkdir("/ws")
}
"""
Discovery output directory: one file of include patterns per module, named
"<moduleRoot>.inc" (the root module is "_root_.inc"). For a given mode every
module produces the same container and exec, so discovery runs once per
workspace rather than once per module, and each module reads only its own file.
"""
let allIncludesDir(
ws: Workspace!,
lint: Boolean!,
test: Boolean!,
generate: Boolean!,
): Directory! {
allIncludesHelper(ws)
.withExec(["go-includes", "--all", "--output-dir", "/output"] + includeFlags(lint, test, generate))
.directory("/output")
}
"""
Additional workspace include patterns discovered by scanning Go directives
and local go.mod replaces reachable from this module's include graph.
With no include flags, defaults to test inputs.
"""
pub includeDiscovered(
ws: Workspace!,
lint: Boolean! = false,
test: Boolean! = false,
generate: Boolean! = false,
): [String!]! {
let includeFile = (if (path == ".") { "_root_" } else { path }) + ".inc"
allIncludesDir(ws, lint: lint, test: test, generate: generate)
.file(includeFile)
.contents
.split("\n")
.filter { pattern => pattern != "" }
}
"""
Directories in this module containing Go test files.
"""
pub testDirectories(ws: Workspace!): [GoDirectory!]! {
goIncludesHelper(ws)
.withExec(
["go-includes", "--output", "/output", "--test-dirs", workspacePath],
experimentalPrivilegedNesting: true,
)
.file("/output")
.contents
.split("\n")
.filter { testPath => testPath != "" }
.map { testPath =>
GoDirectory(
path: testPath,
ws: ws,
modulePath: path,
baseImage: baseImage,
includeExtraFiles: includeExtraFiles,
testPatterns: testPatterns,
)
}
}
"""
CLI flags for selecting include sources.
"""
let includeFlags(lint: Boolean!, test: Boolean!, generate: Boolean!): [String!]! {
let lintFlags = if (lint) { ["--lint"] } else { [] }
let testFlags = if (test) { ["--test"] } else { [] }
let generateFlags = if (generate) { ["--generate"] } else { [] }
lintFlags + testFlags + generateFlags
}
"""
Built-in workspace include patterns for this module's own Go source files.
"""
pub includeBase: [String!]! {
[
subpath("**/*.go"),
subpath("**/*.c"),
subpath("**/*.h"),
subpath("**/*.s"),
subpath("**/*.S"),
subpath("**/*.syso"),
subpath("go.mod"),
subpath("go.sum"),
subpath("go.work"),
subpath("go.work.sum"),
]
}
"""
Final workspace include patterns used to build this module's source directory.
With no include flags, defaults to test inputs.
"""
pub include(
ws: Workspace!,
lint: Boolean! = false,
test: Boolean! = false,
generate: Boolean! = false,
): [String!]! {
let commonIncludes = includeExtraFiles + includeDiscovered(
ws,
lint: lint,
test: test,
generate: generate,
)
if (lint) {
commonIncludes + ["**/*.golangci.yml", "**/*.golangci.yaml", "**/.golangci.toml", "**/.golangci.json"]
} else {
commonIncludes
}
}
"""
Workspace source mounted for this module's Go commands.
With no include flags, defaults to test inputs.
"""
pub source(
ws: Workspace!,
lint: Boolean! = false,
test: Boolean! = false,
generate: Boolean! = false,
): Directory! {
let src = ws.directory(
"/",
include: include(ws, lint: lint, test: test, generate: generate),
)
}
"""
Workspace testdata directories mounted while testing this module.
"""
pub testData(ws: Workspace!): Directory! {
ws.directory(
"/",
include: [
subpath("testdata/**"),
subpath("**/testdata/**"),
],
)
}
"""
Base Go container before source files and commands are added.
"""
pub base: Container! {
baseImage
.withoutEntrypoint
.withWorkdir("/ws")
.withMountedCache("/go/pkg/mod", cacheVolume("go-mod"))
.withMountedCache("/root/.cache/go-build", cacheVolume("go-build"))
}
"""
Run golangci-lint in this module.
If this module is configured to skip lint, no lint check is run.
"""
pub lint(ws: Workspace!): Void @check {
lintExec(ws).sync
null
}
"""
Run tests in this module.
If this module is configured to skip tests, no tests are run.
"""
pub test(ws: Workspace!): Void @check {
testExec(ws).sync
null
}
"""
Changes made by running go generate in this module.
If this module is configured to skip generate or no go:generate directives are
found, the changeset is empty.
"""
pub generate(ws: Workspace!): Changeset! {
if (shouldGenerate(ws) == false) {
directory.changes(directory)
} else {
let before = directory.withDirectory(
path,
source(ws, generate: true).directory(path),
)
let after = before.withDirectory(path, generateExec(ws).directory("."))
after.changes(before)
}
}
"""
Container state after running go generate in this module.
"""
let generateExec(ws: Workspace!): Container! {
base
.withDirectory(".", source(ws, generate: true))
.withWorkdir(path)
.withExec(["go", "generate", "./..."], experimentalPrivilegedNesting: true)
}
"""
Container synced by this module's lint check.
"""
let lintExec(ws: Workspace!): Container! {
if (skipLint(ws)) {
base
.withDirectory(".", ws.directory("/", include: [subpath("go.mod")]))
.withWorkdir(path)
} else {
let lintImage = "docker.io/golangci/golangci-lint:v2.11.4-alpine@sha256:" + "72bcd68512b4e27540dd3a778a1b7afd45759d8145cfb3c089f1d7af53e718e9"
container
.from(lintImage)
.withMountedCache("/go/pkg/mod", cacheVolume("go-mod"))
.withMountedCache("/root/.cache/go-build", cacheVolume("go-build"))
.withMountedCache("/root/.cache/golangci-lint", cacheVolume("golangci-lint"))
.withWorkdir("/src")
.withDirectory(".", source(ws, lint: true))
.withWorkdir(path)
.withExec([
"golangci-lint", "run",
"--path-prefix", path + "/",
"--output.tab.path=stderr",
"--output.tab.print-linter-name=true",
"--output.tab.colors=false",
"--show-stats=false",
"--max-issues-per-linter=0",
"--max-same-issues=0",
])
}
}
"""
Container synced by this module's test check.
"""
let testExec(ws: Workspace!): Container! {
if (skipTest(ws)) {
base
.withDirectory(".", ws.directory("/", include: [subpath("go.mod")]))
.withWorkdir(path)
} else {
base
.withExec(["go", "install", "github.com/dagger/otel-go/cmd/otelgotest@main"])
.withDirectory(".", source(ws, test: true))
.withDirectory(".", testData(ws))
# Nested Dagger clients find the workspace boundary by walking up to .git.
.withNewFile(".git/HEAD", "ref: refs/heads/main\n")
.withWorkdir(path)
.withExec(["otelgotest", "./..."], experimentalPrivilegedNesting: true)
}
}
}
"""
A workspace directory containing Go test files.
"""
type GoDirectory {
"""
Workspace-relative path of this directory.
"""
pub path: String!
"""
Workspace containing this directory.
"""
let ws: Workspace!
"""
Workspace-relative path of this directory's Go module root.
"""
let modulePath: String!
"""
Extra workspace-root include patterns mounted for this directory's Go tests.
"""
let includeExtraFiles: [String!]!
"""
Base image used by this directory's Go containers.
"""
let baseImage: Container!
"""
Gitignore-style globs selecting which module roots to test.
"""
let testPatterns: [String!]!
"""
Return a workspace-root path for a module-relative subpath.
"""
let subpath(relativePath: String!): String! {
if (modulePath == ".") {
relativePath
} else {
modulePath.trimSuffix("/") + "/" + relativePath
}
}
"""
Absolute workspace path for this directory's Go module root.
"""
let workspacePath: String! {
if (modulePath == ".") { "/" } else { "/" + modulePath.trimSuffix("/") }
}
"""
Base container for the Go include helper.
"""
let goIncludesHelper: Container! {
baseImage
.withoutEntrypoint
.withWorkdir("/helpers/go-includes")
.withMountedCache("/go/pkg/mod", cacheVolume("go-mod"))
.withMountedCache("/root/.cache/go-build", cacheVolume("go-build"))
.withDirectory("/helpers/go-includes", currentModule.source.directory("helpers/go-includes"))
.withExec(["go", "build", "-o", "/usr/local/bin/go-includes", "."])
.withDirectory("/ws", directory)
.withWorkdir("/ws")
.withEnvVariable("DAGGER_GO_WORKSPACE_ID", toJSON(ws.id))
}
"""
Whether this directory's module falls outside the configured test selection.
Matching is on the module root path only — no workspace files are read. A
bare pattern includes; a "!"-prefixed pattern excludes; an exclude always
wins. With no include pattern, every module is included.
"""
let skipTest: Boolean! {
let included = testPatterns.filter { p => p.hasPrefix("!") == false }
let excluded = testPatterns.filter { p => p.hasPrefix("!") }.map { p => p.trimPrefix("!") }
let isIncluded = included.length == 0 or included.any { p => matchesModulePath(p) }
let isExcluded = excluded.any { p => matchesModulePath(p) }
isIncluded == false or isExcluded
}
"""
Whether this directory's module root matches a single glob: "**"/"*" match
everything, "X/**" or bare "X" match X and any module below it.
"""
let matchesModulePath(pattern: String!): Boolean! {
let p = pattern.trimPrefix("/").trimSuffix("/")
if (p == "**" or p == "*") {
true
} else {
let prefix = p.trimSuffix("/**")
modulePath == prefix or modulePath.hasPrefix(prefix + "/")
}
}
"""
Additional workspace include patterns discovered for this directory's Go tests.
"""
let includeDiscovered: [String!]! {
goIncludesHelper
.withExec(
["go-includes", "--output", "/output", "--test", workspacePath],
experimentalPrivilegedNesting: true,
)
.file("/output")
.contents
.split("\n")
.filter { pattern => pattern != "" }
}
"""
Final workspace include patterns used to build this directory's source.
"""
let include: [String!]! {
includeExtraFiles + includeDiscovered
}
"""
Workspace source mounted for this directory's Go tests.
"""
let source: Directory! {
ws.directory("/", include: include)
}
"""
Workspace testdata directories mounted while testing this directory.
"""
let testData: Directory! {
ws.directory(
"/",
include: [
subpath("testdata/**"),
subpath("**/testdata/**"),
],
)
}
"""
Base Go container before source files and commands are added.
"""
let base: Container! {
baseImage
.withoutEntrypoint
.withWorkdir("/ws")
.withMountedCache("/go/pkg/mod", cacheVolume("go-mod"))
.withMountedCache("/root/.cache/go-build", cacheVolume("go-build"))
}
"""
Container synced by this directory's test check.
"""
let testExec: Container! {
if (skipTest) {
base
.withDirectory(".", ws.directory("/", include: [subpath("go.mod")]))
.withWorkdir(modulePath)
} else {
base
.withExec(["go", "install", "github.com/dagger/otel-go/cmd/otelgotest@main"])
.withDirectory(".", source)
.withDirectory(".", testData)
# Nested Dagger clients find the workspace boundary by walking up to .git.
.withNewFile(".git/HEAD", "ref: refs/heads/main\n")
.withWorkdir(path)
.withExec(["otelgotest"], experimentalPrivilegedNesting: true)
}
}
"""
Run Go tests in this directory.
"""
pub execute(): Void @check {
testExec.sync
null
}
}