Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/core/src/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, test, expect, beforeEach, afterEach } from "vitest";
import {
loadChantConfig,
loadChantConfigUpward,
DEFAULT_CHANT_CONFIG,
resolveAutoReleaseDisabled,
resolveFoldEnabled,
Expand Down Expand Up @@ -137,6 +138,58 @@ describe("loadChantConfig", () => {
});
});

// #1502 — the upward walk skips lint-scoping fragments. A `src/chant.config.json`
// holding only `extends`/`rules` (the examples/ convention) must not shadow the
// project config above it, or `chant build src` silently loses `ownership`/
// `buildParams` — the exact fallback #1117's walk exists to prevent.
describe("loadChantConfigUpward (#1502 — lint fragments do not end the walk)", () => {
const SRC = join(TEST_DIR, "src");

test("walks past a lint-only src/chant.config.json to the project config", async () => {
mkdirSync(SRC, { recursive: true });
writeFileSync(
join(SRC, "chant.config.json"),
JSON.stringify({ extends: ["@intentius/chant/lint/presets/strict"], rules: { COR001: "off" } }),
);
writeFileSync(
join(TEST_DIR, "chant.config.json"),
JSON.stringify({ ownership: { stack: "billing", env: "prod" } }),
);

const result = await loadChantConfigUpward(SRC);
expect(result.config.ownership).toEqual({ stack: "billing", env: "prod" });
expect(result.configPath).toBe(join(TEST_DIR, "chant.config.json"));
});

test("a src/chant.config.json declaring any project-level key still wins in place", async () => {
mkdirSync(SRC, { recursive: true });
writeFileSync(
join(SRC, "chant.config.json"),
JSON.stringify({ ownership: { stack: "nested" }, rules: { COR001: "off" } }),
);
writeFileSync(
join(TEST_DIR, "chant.config.json"),
JSON.stringify({ ownership: { stack: "root" } }),
);

const result = await loadChantConfigUpward(SRC);
expect(result.config.ownership?.stack).toBe("nested");
});

test("a fragment-only project resolves to the default config at the boundary", async () => {
mkdirSync(SRC, { recursive: true });
writeFileSync(join(TEST_DIR, "package.json"), JSON.stringify({ name: "boundary" }));
writeFileSync(
join(SRC, "chant.config.json"),
JSON.stringify({ extends: ["@intentius/chant/lint/presets/strict"] }),
);

const result = await loadChantConfigUpward(SRC);
expect(result.config).toEqual(DEFAULT_CHANT_CONFIG);
expect(result.configPath).toBeUndefined();
});
});

// #1166 — `environments` accepts either a bare name (unchanged) or
// `{ name, endpoint }`, so a declared environment can be self-sufficient for
// `--live` reads without an ambient AWS_ENDPOINT_URL export.
Expand Down
44 changes: 41 additions & 3 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync } from "fs";
import { join } from "path";
import { existsSync, readFileSync } from "fs";
import { dirname, join } from "path";
import { z } from "zod";
import type { LintConfig } from "./lint/config";
import type { OwnershipMarker } from "./ownership";
Expand Down Expand Up @@ -384,12 +384,50 @@ export async function loadChantConfig(dir: string): Promise<ResolvedConfig> {
* same walk `chant lint`/`chant graph` already used ({@link findProjectConfig},
* shared with `./lint/config.ts`'s `findProjectRoot`) — one config-discovery
* contract for the whole CLI.
*
* chant #1502 — a lint-scoping fragment does not end the walk. The convention
* of a `src/chant.config.json` holding only `extends`/`rules` (cc-aws-canonical
* and most of examples/) sits BETWEEN the build directory and the real
* `chant.config.ts`, and stopping there re-introduced the exact silent
* fallback this walk exists to prevent: `chant build src` resolved the
* fragment, found no `ownership`, and built unstamped manifests that every
* owned-scoped live read then withheld. A fragment is skipped, not merged —
* lint resolution keeps its own nearest-wins walk untouched, and a JSON
* config declaring any project-level key still wins where it stands.
*/
export async function loadChantConfigUpward(startDir: string): Promise<ResolvedConfig> {
const { dir } = findProjectConfig(startDir);
let { dir, configPath } = findProjectConfig(startDir);
while (configPath && isLintOnlyFragment(configPath)) {
const parent = dirname(dir);
if (parent === dir) break;
({ dir, configPath } = findProjectConfig(parent));
}
return loadChantConfig(dir);
}

/**
* The top-level keys of `./lint/config.ts`'s `LintConfigSchema` (plus the
* `$schema` editor convention). A `chant.config.json` whose keys all come from
* this set is a lint-scoping fragment, not a project config — see
* {@link loadChantConfigUpward}. `chant.config.ts` is never a fragment: it is
* project-authored code, and inspecting it would mean evaluating it.
*/
const LINT_FRAGMENT_KEYS = new Set(["$schema", "extends", "rules", "overrides", "plugins", "policies"]);

function isLintOnlyFragment(configPath: string): boolean {
if (!configPath.endsWith("chant.config.json")) return false;
try {
const parsed = JSON.parse(readFileSync(configPath, "utf-8")) as unknown;
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return false;
const keys = Object.keys(parsed);
return keys.length > 0 && keys.every((k) => LINT_FRAGMENT_KEYS.has(k));
} catch {
// Unreadable/unparseable JSON: let loadChantConfig surface the real error
// in place rather than silently walking past it.
return false;
}
}

/**
* Resolve the ownership marker to stamp from project config, or undefined when
* ownership marking is off (no `stack`, or `enabled: false`).
Expand Down