The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { hashDockerBuildInputs, selectDockerBuildContext } from "@alchemy.run/docker/BuildHash"; |
| 2 | import { expect, layer } from "@alchemy.run/test"; |
| 3 | import { BunServices } from "@effect/platform-bun"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | import * as Result from "effect/Result"; |
| 8 | |
| 9 | const sourceFor = (root: string) => ({ |
| 10 | context: root, |
| 11 | dockerfile: "Dockerfile", |
| 12 | platform: "linux/amd64", |
| 13 | buildArgs: { Z: "last", A: "first" }, |
| 14 | }); |
| 15 | |
| 16 | const makeContext = Effect.fn(function* (ignore = "") { |
| 17 | const fs = yield* FileSystem.FileSystem; |
| 18 | const path = yield* Path.Path; |
| 19 | const root = yield* fs.makeTempDirectoryScoped({ |
| 20 | prefix: "alchemy-build-hash-", |
| 21 | }); |
| 22 | yield* fs.makeDirectory(path.join(root, "src"), { recursive: true }); |
| 23 | yield* fs.writeFileString(path.join(root, "Dockerfile"), "FROM scratch\nCOPY src /src\n"); |
| 24 | yield* fs.writeFileString(path.join(root, ".dockerignore"), ignore); |
| 25 | return { root, fs, path }; |
| 26 | }); |
| 27 | |
| 28 | const guardedReadDirectories = ( |
| 29 | fs: FileSystem.FileSystem, |
| 30 | path: Path.Path, |
| 31 | root: string, |
| 32 | forbidden: ReadonlyArray<string>, |
| 33 | ) => { |
| 34 | const reads: string[] = []; |
| 35 | const guarded: FileSystem.FileSystem = { |
| 36 | ...fs, |
| 37 | readDirectory: (directory, options) => { |
| 38 | const relative = path.relative(root, directory) || "."; |
| 39 | reads.push(relative); |
| 40 | if (forbidden.some((entry) => relative === entry || relative.startsWith(`${entry}/`))) { |
| 41 | return Effect.die(new Error(`pruned directory was descended into: ${relative}`)); |
| 42 | } |
| 43 | return fs.readDirectory(directory, options); |
| 44 | }, |
| 45 | }; |
| 46 | return { guarded, reads }; |
| 47 | }; |
| 48 | |
| 49 | const hashWithGuard = ( |
| 50 | root: string, |
| 51 | fs: FileSystem.FileSystem, |
| 52 | path: Path.Path, |
| 53 | forbidden: ReadonlyArray<string>, |
| 54 | mode: "all" | "effective" = "effective", |
| 55 | source: ReturnType<typeof sourceFor> = sourceFor(root), |
| 56 | ) => { |
| 57 | const { guarded, reads } = guardedReadDirectories(fs, path, root, forbidden); |
| 58 | return { |
| 59 | reads, |
| 60 | hash: hashDockerBuildInputs(source, mode).pipe( |
| 61 | Effect.provideService(FileSystem.FileSystem, guarded), |
| 62 | ), |
| 63 | }; |
| 64 | }; |
| 65 | |
| 66 | layer(BunServices.layer)("Docker build hash traversal", (test) => { |
| 67 | // The user-visible failure: a folder Docker never sends (excluded by |
| 68 | // `.dockerignore`) that the deploying user cannot read, e.g. a root-owned |
| 69 | // database bind mount. Hashing used to walk into it and fail with EACCES. |
| 70 | test.effect( |
| 71 | "hashes a context whose ignored folder is unreadable", |
| 72 | () => |
| 73 | Effect.gen(function* () { |
| 74 | const { root, fs, path } = yield* makeContext("data/\n"); |
| 75 | const locked = path.join(root, "data", "postgres"); |
| 76 | yield* fs.makeDirectory(locked, { recursive: true }); |
| 77 | yield* fs.writeFileString(path.join(locked, "pg_hba.conf"), "local all all trust\n"); |
| 78 | yield* Effect.acquireRelease(fs.chmod(locked, 0o000), () => |
| 79 | fs.chmod(locked, 0o755).pipe(Effect.ignore), |
| 80 | ); |
| 81 | |
| 82 | const hash = yield* hashDockerBuildInputs(sourceFor(root), "effective"); |
| 83 | expect(hash).toMatch(/^[0-9a-f]+$/); |
| 84 | }), |
| 85 | { tags: ["unit", "provider:docker", "local"] }, |
| 86 | ); |
| 87 | |
| 88 | test.effect( |
| 89 | "does not descend into an ignored subtree", |
| 90 | () => |
| 91 | Effect.gen(function* () { |
| 92 | const { root, fs, path } = yield* makeContext("ignored/\n"); |
| 93 | yield* fs.makeDirectory(path.join(root, "ignored", "deep"), { |
| 94 | recursive: true, |
| 95 | }); |
| 96 | yield* fs.writeFileString(path.join(root, "ignored", "deep", "not-read"), "x"); |
| 97 | const { hash, reads } = hashWithGuard(root, fs, path, ["ignored"]); |
| 98 | yield* hash; |
| 99 | expect(reads).toContain("."); |
| 100 | expect(reads).not.toContain("ignored"); |
| 101 | }), |
| 102 | { tags: ["unit", "provider:docker", "local"] }, |
| 103 | ); |
| 104 | |
| 105 | test.effect( |
| 106 | "prunes Finn-style allowlists while traversing the required subtree", |
| 107 | () => |
| 108 | Effect.gen(function* () { |
| 109 | const { root, fs, path } = yield* makeContext("**\n!required/**\n**/node_modules\n"); |
| 110 | yield* fs.makeDirectory(path.join(root, "required", "source", "nested"), { |
| 111 | recursive: true, |
| 112 | }); |
| 113 | yield* fs.makeDirectory(path.join(root, "other", "nested"), { |
| 114 | recursive: true, |
| 115 | }); |
| 116 | yield* fs.makeDirectory(path.join(root, "required", "node_modules", "nested"), { |
| 117 | recursive: true, |
| 118 | }); |
| 119 | yield* fs.writeFileString(path.join(root, "required", "source", "nested", "kept"), "kept"); |
| 120 | yield* fs.writeFileString(path.join(root, "other", "nested", "not-read"), "ignored"); |
| 121 | yield* fs.writeFileString( |
| 122 | path.join(root, "required", "node_modules", "nested", "not-read"), |
| 123 | "ignored", |
| 124 | ); |
| 125 | const { hash, reads } = hashWithGuard(root, fs, path, ["other", "required/node_modules"]); |
| 126 | yield* hash; |
| 127 | expect(reads).toContain("required"); |
| 128 | expect(reads).toContain("required/source"); |
| 129 | expect(reads).toContain("required/source/nested"); |
| 130 | expect(reads).not.toContain("other"); |
| 131 | expect(reads).not.toContain("required/node_modules"); |
| 132 | expect( |
| 133 | reads.every((entry) => |
| 134 | [".", "required", "required/source", "required/source/nested"].includes(entry), |
| 135 | ), |
| 136 | ).toBe(true); |
| 137 | }), |
| 138 | { tags: ["unit", "provider:docker", "local"] }, |
| 139 | ); |
| 140 | |
| 141 | test.effect( |
| 142 | "walks a reincluded descendant, but not a negation shadowed by a later exclusion", |
| 143 | () => |
| 144 | Effect.gen(function* () { |
| 145 | const { root, fs, path } = yield* makeContext( |
| 146 | "ignored/\n!ignored/keep/**\n!before/keep\nbefore/\n", |
| 147 | ); |
| 148 | yield* fs.makeDirectory(path.join(root, "ignored", "keep", "deep"), { |
| 149 | recursive: true, |
| 150 | }); |
| 151 | yield* fs.makeDirectory(path.join(root, "ignored", "other"), { |
| 152 | recursive: true, |
| 153 | }); |
| 154 | yield* fs.makeDirectory(path.join(root, "before", "keep"), { |
| 155 | recursive: true, |
| 156 | }); |
| 157 | yield* fs.writeFileString(path.join(root, "ignored", "keep", "deep", "kept"), "kept"); |
| 158 | yield* fs.writeFileString(path.join(root, "ignored", "other", "not-read"), "ignored"); |
| 159 | yield* fs.writeFileString(path.join(root, "before", "keep", "not-read"), "shadowed"); |
| 160 | const { hash, reads } = hashWithGuard(root, fs, path, ["before", "ignored/other"]); |
| 161 | yield* hash; |
| 162 | expect(reads).toContain("ignored"); |
| 163 | expect(reads).toContain("ignored/keep"); |
| 164 | expect(reads).toContain("ignored/keep/deep"); |
| 165 | expect(reads).not.toContain("ignored/other"); |
| 166 | expect(reads).not.toContain("before"); |
| 167 | }), |
| 168 | { tags: ["unit", "provider:docker", "local"] }, |
| 169 | ); |
| 170 | |
| 171 | test.effect( |
| 172 | "handles nested and basename patterns without pruning a possible match", |
| 173 | () => |
| 174 | Effect.gen(function* () { |
| 175 | const { root, fs, path } = yield* makeContext("**/cache\n*.secret\n"); |
| 176 | yield* fs.makeDirectory(path.join(root, "a", "cache", "deep"), { |
| 177 | recursive: true, |
| 178 | }); |
| 179 | yield* fs.makeDirectory(path.join(root, "b", "keep"), { |
| 180 | recursive: true, |
| 181 | }); |
| 182 | yield* fs.writeFileString(path.join(root, "a", "cache", "deep", "not-read"), "ignored"); |
| 183 | yield* fs.writeFileString(path.join(root, "b", "keep", "visible.txt"), "visible"); |
| 184 | yield* fs.writeFileString(path.join(root, "b", "keep", "hidden.secret"), "ignored"); |
| 185 | const { hash, reads } = hashWithGuard(root, fs, path, ["a/cache"]); |
| 186 | yield* hash; |
| 187 | expect(reads).not.toContain("a/cache"); |
| 188 | expect(reads).toContain("b"); |
| 189 | expect(reads).toContain("b/keep"); |
| 190 | }), |
| 191 | { tags: ["unit", "provider:docker", "local"] }, |
| 192 | ); |
| 193 | |
| 194 | test.effect( |
| 195 | "forces a Dockerfile and its selected ignore file below a pruned directory", |
| 196 | () => |
| 197 | Effect.gen(function* () { |
| 198 | const { root, fs, path } = yield* makeContext("**\n"); |
| 199 | const nested = path.join(root, "pruned"); |
| 200 | yield* fs.makeDirectory(nested, { recursive: true }); |
| 201 | const dockerfile = path.join(nested, "Dockerfile"); |
| 202 | const selectedIgnore = path.join(nested, "Dockerfile.dockerignore"); |
| 203 | yield* fs.writeFileString(dockerfile, "FROM scratch\nCOPY . /app\n"); |
| 204 | yield* fs.writeFileString(selectedIgnore, "**\n"); |
| 205 | yield* fs.writeFileString(path.join(nested, "ignored.secret"), "ignored"); |
| 206 | const source = { ...sourceFor(root), dockerfile: "pruned/Dockerfile" }; |
| 207 | const selection = yield* selectDockerBuildContext(source); |
| 208 | expect(selection.includes("pruned/Dockerfile")).toBe(true); |
| 209 | expect(selection.includes("pruned/Dockerfile.dockerignore")).toBe(true); |
| 210 | const { hash, reads } = hashWithGuard(root, fs, path, ["other"], "effective", source); |
| 211 | const first = yield* hash; |
| 212 | expect(reads).toContain("pruned"); |
| 213 | yield* fs.writeFileString(dockerfile, "FROM scratch\nCOPY pruned/ignored.secret /app\n"); |
| 214 | const second = yield* hashDockerBuildInputs(source, "effective"); |
| 215 | expect(second).not.toBe(first); |
| 216 | yield* fs.writeFileString(selectedIgnore, "**\n# changed\n"); |
| 217 | const third = yield* hashDockerBuildInputs(source, "effective"); |
| 218 | expect(third).not.toBe(second); |
| 219 | }), |
| 220 | { tags: ["unit", "provider:docker", "local"] }, |
| 221 | ); |
| 222 | |
| 223 | test.effect( |
| 224 | "preserves symlink entries and empty directories", |
| 225 | () => |
| 226 | Effect.gen(function* () { |
| 227 | const { root, fs, path } = yield* makeContext(); |
| 228 | yield* fs.makeDirectory(path.join(root, "empty"), { recursive: true }); |
| 229 | yield* fs.writeFileString(path.join(root, "target-a"), "same"); |
| 230 | yield* fs.writeFileString(path.join(root, "target-b"), "same"); |
| 231 | yield* fs.symlink("target-a", path.join(root, "link")); |
| 232 | const source = sourceFor(root); |
| 233 | const before = yield* hashDockerBuildInputs(source, "effective"); |
| 234 | yield* fs.remove(path.join(root, "link")); |
| 235 | yield* fs.symlink("target-b", path.join(root, "link")); |
| 236 | const retargeted = yield* hashDockerBuildInputs(source, "effective"); |
| 237 | expect(retargeted).not.toBe(before); |
| 238 | const link = yield* Effect.result(fs.readLink(path.join(root, "link"))); |
| 239 | expect(Result.isSuccess(link)).toBe(true); |
| 240 | yield* fs.remove(path.join(root, "empty"), { recursive: true }); |
| 241 | const withoutEmptyDirectory = yield* hashDockerBuildInputs(source, "effective"); |
| 242 | expect(withoutEmptyDirectory).not.toBe(retargeted); |
| 243 | }), |
| 244 | { tags: ["unit", "provider:docker", "local"] }, |
| 245 | ); |
| 246 | |
| 247 | test.effect( |
| 248 | "does not duplicate a Dockerfile outside the context", |
| 249 | () => |
| 250 | Effect.gen(function* () { |
| 251 | const { root, fs, path } = yield* makeContext("Dockerfile\n**\n"); |
| 252 | const outside = yield* fs.makeTempDirectoryScoped({ |
| 253 | prefix: "alchemy-build-hash-dockerfile-", |
| 254 | }); |
| 255 | const externalDockerfile = path.join(outside, "Dockerfile"); |
| 256 | yield* fs.writeFileString(path.join(root, "Dockerfile"), "context copy\n"); |
| 257 | yield* fs.writeFileString(externalDockerfile, "FROM scratch\n"); |
| 258 | const source = { ...sourceFor(root), dockerfile: externalDockerfile }; |
| 259 | const first = yield* hashDockerBuildInputs(source, "effective"); |
| 260 | yield* fs.writeFileString(path.join(root, "Dockerfile"), "different context copy\n"); |
| 261 | const second = yield* hashDockerBuildInputs(source, "effective"); |
| 262 | expect(second).toBe(first); |
| 263 | yield* fs.writeFileString(externalDockerfile, "FROM alpine\n"); |
| 264 | const third = yield* hashDockerBuildInputs(source, "effective"); |
| 265 | expect(third).not.toBe(second); |
| 266 | }), |
| 267 | { tags: ["unit", "provider:docker", "local"] }, |
| 268 | ); |
| 269 | |
| 270 | test.effect( |
| 271 | "does not descend through a symlink to an external directory", |
| 272 | () => |
| 273 | Effect.gen(function* () { |
| 274 | const { root, fs, path } = yield* makeContext(); |
| 275 | const outside = yield* fs.makeTempDirectoryScoped({ |
| 276 | prefix: "alchemy-build-hash-external-", |
| 277 | }); |
| 278 | yield* fs.writeFileString(path.join(outside, "not-read"), "external"); |
| 279 | yield* fs.symlink(outside, path.join(root, "external-link")); |
| 280 | const { hash, reads } = hashWithGuard(root, fs, path, ["external-link"]); |
| 281 | yield* hash; |
| 282 | expect(reads).not.toContain("external-link"); |
| 283 | }), |
| 284 | { tags: ["unit", "provider:docker", "local"] }, |
| 285 | ); |
| 286 | |
| 287 | test.effect( |
| 288 | "keeps raw mode and the pre-traversal hash golden stable", |
| 289 | () => |
| 290 | Effect.gen(function* () { |
| 291 | const { root, fs, path } = yield* makeContext("**\n!src\n!src/**\nnode_modules\n"); |
| 292 | yield* fs.makeDirectory(path.join(root, "empty"), { recursive: true }); |
| 293 | yield* fs.makeDirectory(path.join(root, "node_modules", "ignored", "deep"), { |
| 294 | recursive: true, |
| 295 | }); |
| 296 | yield* fs.writeFileString(path.join(root, "src", "main.txt"), "main\n"); |
| 297 | yield* fs.writeFileString( |
| 298 | path.join(root, "node_modules", "ignored", "deep", "huge.txt"), |
| 299 | "ignored\n", |
| 300 | ); |
| 301 | const source = sourceFor(root); |
| 302 | expect(yield* hashDockerBuildInputs(source, "all")).toBe( |
| 303 | "d336bf3db2943311657f812d731ea71c", |
| 304 | ); |
| 305 | expect(yield* hashDockerBuildInputs(source, "effective")).toBe( |
| 306 | "91124478d665ef48e0af2670d271511b", |
| 307 | ); |
| 308 | }), |
| 309 | { tags: ["unit", "provider:docker", "local"] }, |
| 310 | ); |
| 311 | }); |