|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import path from "node:path" |
| 5 | +import test from "node:test" |
| 6 | +import { sourceExportCheckEnv, unloadableSourceExports, workspaceSpecifiersIn } from "../scripts/build.mjs" |
| 7 | + |
| 8 | +/** |
| 9 | + * A service with one workspace package served from TypeScript source, like @openwork/types: `broken` imports a |
| 10 | + * sibling without a file extension (what Node can't resolve at runtime), `fine` imports one with its real extension |
| 11 | + * only as a type, and `built` is served from dist. The package lives outside node_modules and is linked in, as pnpm |
| 12 | + * links workspace packages (Node refuses to strip types under a real node_modules path). |
| 13 | + */ |
| 14 | +function fixture() { |
| 15 | + const root = mkdtempSync(path.join(tmpdir(), "den-build-exports-")) |
| 16 | + const pkg = path.join(root, "packages", "fixture") |
| 17 | + mkdirSync(path.join(pkg, "src"), { recursive: true }) |
| 18 | + mkdirSync(path.join(root, "node_modules", "@openwork"), { recursive: true }) |
| 19 | + symlinkSync(pkg, path.join(root, "node_modules", "@openwork", "fixture"), "dir") |
| 20 | + mkdirSync(path.join(pkg, "dist"), { recursive: true }) |
| 21 | + writeFileSync( |
| 22 | + path.join(pkg, "package.json"), |
| 23 | + JSON.stringify({ |
| 24 | + name: "@openwork/fixture", |
| 25 | + type: "module", |
| 26 | + exports: { |
| 27 | + "./broken": { development: "./src/broken.ts", default: "./src/broken.ts" }, |
| 28 | + "./fine": { development: "./src/fine.ts", default: "./src/fine.ts" }, |
| 29 | + "./built": { development: "./src/broken.ts", default: "./dist/built.js" }, |
| 30 | + "./env-probe": { development: "./src/env-probe.ts", default: "./src/env-probe.ts" }, |
| 31 | + }, |
| 32 | + }), |
| 33 | + ) |
| 34 | + writeFileSync(path.join(pkg, "src", "sibling.ts"), "export const sibling: number = 1\n") |
| 35 | + writeFileSync(path.join(pkg, "src", "broken.ts"), 'import { sibling } from "./sibling"\nexport const broken: number = sibling\n') |
| 36 | + writeFileSync(path.join(pkg, "src", "fine.ts"), 'import type { Sibling } from "./types"\nexport const fine: Sibling = 1\n') |
| 37 | + writeFileSync(path.join(pkg, "src", "types.ts"), "export type Sibling = number\n") |
| 38 | + writeFileSync(path.join(pkg, "dist", "built.js"), "export const built = 1\n") |
| 39 | + // Top-level code that would leak a build secret: it fails to load if it can see one. |
| 40 | + writeFileSync( |
| 41 | + path.join(pkg, "src", "env-probe.ts"), |
| 42 | + 'if (process.env.SENTRY_AUTH_TOKEN !== undefined) throw new Error("SENTRY_AUTH_TOKEN visible")\nexport const probe = true\n', |
| 43 | + ) |
| 44 | + const dist = path.join(root, "dist") |
| 45 | + mkdirSync(path.join(dist, "routes"), { recursive: true }) |
| 46 | + writeFileSync(path.join(dist, "main.js"), 'import { broken } from "@openwork/fixture/broken"\nimport { fine } from "@openwork/fixture/fine"\n') |
| 47 | + writeFileSync(path.join(dist, "routes", "lazy.js"), 'const built = await import("@openwork/fixture/built")\nimport { probe } from "@openwork/fixture/env-probe"\n') |
| 48 | + return { root, dist } |
| 49 | +} |
| 50 | + |
| 51 | +test("the build finds every workspace import in the compiled service, including dynamic ones", () => { |
| 52 | + const { root, dist } = fixture() |
| 53 | + try { |
| 54 | + assert.deepEqual(workspaceSpecifiersIn(dist), ["@openwork/fixture/broken", "@openwork/fixture/built", "@openwork/fixture/env-probe", "@openwork/fixture/fine"]) |
| 55 | + } finally { |
| 56 | + rmSync(root, { recursive: true, force: true }) |
| 57 | + } |
| 58 | +}) |
| 59 | + |
| 60 | +test("a source-served export with an import Node can't resolve fails the build; working and dist-served ones pass", () => { |
| 61 | + const { root, dist } = fixture() |
| 62 | + try { |
| 63 | + const failures = unloadableSourceExports(workspaceSpecifiersIn(dist), root) |
| 64 | + assert.equal(failures.length, 1, failures.join("\n")) |
| 65 | + assert.match(failures[0] ?? "", /^@openwork\/fixture\/broken: Error \[ERR_MODULE_NOT_FOUND\]: Cannot find module .*sibling'/) |
| 66 | + } finally { |
| 67 | + rmSync(root, { recursive: true, force: true }) |
| 68 | + } |
| 69 | +}) |
| 70 | + |
| 71 | +test("the checked modules see none of the build's environment, so they can't read its secrets", () => { |
| 72 | + assert.deepEqual(sourceExportCheckEnv({ PATH: "/usr/bin", SENTRY_AUTH_TOKEN: "secret", NODE_OPTIONS: "--require x", HOME: "/root" }), process.platform === "win32" ? {} : { PATH: "/usr/bin" }) |
| 73 | + const { root, dist } = fixture() |
| 74 | + const previous = process.env.SENTRY_AUTH_TOKEN |
| 75 | + process.env.SENTRY_AUTH_TOKEN = "sntrys_fixture_not_a_real_token" |
| 76 | + try { |
| 77 | + const failures = unloadableSourceExports(workspaceSpecifiersIn(dist), root) |
| 78 | + assert.ok(!failures.some((entry) => entry.startsWith("@openwork/fixture/env-probe")), failures.join("\n")) |
| 79 | + } finally { |
| 80 | + if (previous === undefined) delete process.env.SENTRY_AUTH_TOKEN |
| 81 | + else process.env.SENTRY_AUTH_TOKEN = previous |
| 82 | + rmSync(root, { recursive: true, force: true }) |
| 83 | + } |
| 84 | +}) |
0 commit comments