| 1 | # The Go dependency boundary (AGENTS.md, "Go dependencies"): every Go |
| 2 | # module imports only the standard library and this repository's modules, |
| 3 | # and only flow/ imports third-party modules. |
| 4 | { |
| 5 | pkgs, |
| 6 | lib, |
| 7 | packages, |
| 8 | ... |
| 9 | }: |
| 10 | let |
| 11 | root = ../..; |
| 12 | |
| 13 | # Its tests run in its unit build, against fixture trees. |
| 14 | checker = packages.goModule { |
| 15 | pname = "valley-go-dependencies"; |
| 16 | dir = ./go-dependencies-checker; |
| 17 | tests.postCheck = '' |
| 18 | go vet ./... |
| 19 | ''; |
| 20 | }; |
| 21 | |
| 22 | # What the checker reads, and what it must see to refuse: every Go file, |
| 23 | # go.mod and go.work, every file under a vendor directory, and every |
| 24 | # symlink, whatever it names. |
| 25 | goSource = lib.fileset.fromSource ( |
| 26 | lib.cleanSourceWith { |
| 27 | src = root; |
| 28 | filter = |
| 29 | path: type: |
| 30 | type == "directory" |
| 31 | || type == "symlink" |
| 32 | || lib.hasSuffix ".go" path |
| 33 | || builtins.elem (baseNameOf path) [ |
| 34 | "go.mod" |
| 35 | "go.work" |
| 36 | ] |
| 37 | || lib.hasInfix "/vendor/" path; |
| 38 | } |
| 39 | ); |
| 40 | in |
| 41 | { |
| 42 | # Scans the tree, without building or testing any module in it: each Go |
| 43 | # file's imports, whatever its build constraints, and each go.mod's |
| 44 | # module path, requires, replacements and tools, with the standard |
| 45 | # library as the pinned toolchain lists it. A workspace, a vendor |
| 46 | # directory, a go.mod or Go file that does not parse, and a symlink |
| 47 | # leaving the tree or giving a Go file's name to another file are |
| 48 | # refused. Every refusal names its file and its import or directive. |
| 49 | # |
| 50 | # It holds the committed tree: Go source or a go.mod that a build phase |
| 51 | # generates or rewrites after the scan is outside it. It runs on changes to |
| 52 | # Go source, module and workspace manifests, and anything named vendor |
| 53 | # (policy/project/policy.cue); a change that adds or changes only another |
| 54 | # symlink or non-Go path, retargeting a symlink included, is not judged |
| 55 | # until a later change touches one of those. Crossing the boundary takes an |
| 56 | # import, a module directive or a vendor entry, and all three are in paths |
| 57 | # whose changes run it. |
| 58 | go-dependencies = pkgs.runCommand "valley-go-dependencies" { |
| 59 | nativeBuildInputs = [ |
| 60 | pkgs.go |
| 61 | checker.unit |
| 62 | ]; |
| 63 | tree = lib.fileset.toSource { |
| 64 | inherit root; |
| 65 | fileset = goSource; |
| 66 | }; |
| 67 | } (builtins.readFile ./go-dependencies.sh); |
| 68 | } |