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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ on:

jobs:
build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export { pruneCache } from "./prune";
export { removeSources } from "./remove";
export { resolveRepoInput } from "./resolve-repo";
export { printSyncPlan, runSync } from "./sync";
export { applyTargetDir } from "./targets";
export { verifyCache } from "./verify";
29 changes: 29 additions & 0 deletions src/materialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ export const materializeSource = async (params: MaterializeParams) => {
const tempDir = await mkdtemp(
path.join(params.cacheDir, `.tmp-${params.sourceId}-`),
);
let manifestStreamRef: ReturnType<typeof createWriteStream> | null = null;
const closeManifestStream = async () => {
const stream = manifestStreamRef;
if (!stream || stream.closed || stream.destroyed) {
return;
}
await new Promise<void>((resolve) => {
const cleanup = () => {
stream.off("close", onClose);
stream.off("error", onError);
resolve();
};
const onClose = () => cleanup();
const onError = () => cleanup();
stream.once("close", onClose);
stream.once("error", onError);
try {
stream.end();
} catch {
cleanup();
}
});
};

try {
const files = await fg(params.include, {
Expand Down Expand Up @@ -132,6 +155,7 @@ export const materializeSource = async (params: MaterializeParams) => {
const manifestStream = createWriteStream(manifestPath, {
encoding: "utf8",
});
manifestStreamRef = manifestStream;
const manifestHash = createHash("sha256");
const writeManifestLine = async (line: string) => {
return new Promise<void>((resolve, reject) => {
Expand Down Expand Up @@ -267,6 +291,11 @@ export const materializeSource = async (params: MaterializeParams) => {
manifestSha256,
};
} catch (error) {
try {
await closeManifestStream();
} catch {
// Ignore cleanup errors to preserve root cause.
}
await rm(tempDir, { recursive: true, force: true });
throw error;
}
Expand Down
2 changes: 2 additions & 0 deletions src/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export const runSync = async (options: SyncOptions, deps: SyncDeps = {}) => {
sourceDir: path.join(plan.cacheDir, source.id),
targetDir: resolvedTarget,
mode: source.targetMode ?? defaults.targetMode,
explicitTargetMode: source.targetMode !== undefined,
});
}),
);
Expand Down Expand Up @@ -352,6 +353,7 @@ export const runSync = async (options: SyncOptions, deps: SyncDeps = {}) => {
sourceDir: path.join(plan.cacheDir, source.id),
targetDir: resolvedTarget,
mode: source.targetMode ?? defaults.targetMode,
explicitTargetMode: source.targetMode !== undefined,
});
}
result.bytes = stats.bytes;
Expand Down
45 changes: 39 additions & 6 deletions src/targets.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
import { cp, mkdir, rm, symlink } from "node:fs/promises";
import path from "node:path";

type TargetDeps = {
cp: typeof cp;
mkdir: typeof mkdir;
rm: typeof rm;
symlink: typeof symlink;
stderr: NodeJS.WritableStream;
};

type TargetParams = {
sourceDir: string;
targetDir: string;
mode?: "symlink" | "copy";
explicitTargetMode?: boolean;
deps?: TargetDeps;
};

const removeTarget = async (targetDir: string) => {
await rm(targetDir, { recursive: true, force: true });
const removeTarget = async (targetDir: string, deps: TargetDeps) => {
await deps.rm(targetDir, { recursive: true, force: true });
};

export const applyTargetDir = async (params: TargetParams) => {
const deps = params.deps ?? {
cp,
mkdir,
rm,
symlink,
stderr: process.stderr,
};
const parentDir = path.dirname(params.targetDir);
await mkdir(parentDir, { recursive: true });
await removeTarget(params.targetDir);
await deps.mkdir(parentDir, { recursive: true });
await removeTarget(params.targetDir, deps);

const defaultMode = process.platform === "win32" ? "copy" : "symlink";
const mode = params.mode ?? defaultMode;
if (mode === "copy") {
await cp(params.sourceDir, params.targetDir, { recursive: true });
await deps.cp(params.sourceDir, params.targetDir, { recursive: true });
return;
}

const type = process.platform === "win32" ? "junction" : "dir";
await symlink(params.sourceDir, params.targetDir, type);
try {
await deps.symlink(params.sourceDir, params.targetDir, type);
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
const fallbackCodes = new Set(["EPERM", "EACCES", "ENOTSUP", "EINVAL"]);
if (code && fallbackCodes.has(code)) {
if (params.explicitTargetMode) {
const message = error instanceof Error ? error.message : String(error);
deps.stderr.write(
`Warning: Failed to create symlink at ${params.targetDir}. Falling back to copy. ${message}\n`,
);
}
await deps.cp(params.sourceDir, params.targetDir, { recursive: true });
return;
}
throw error;
}
};
8 changes: 8 additions & 0 deletions tests/edge-cases-validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ test("targetDir with Windows-style path is allowed", async () => {
],
});

if (process.platform === "win32") {
await assert.rejects(
() => loadConfig(configPath),
/targetDir.*escapes project directory/i,
);
return;
}

const { sources } = await loadConfig(configPath);
assert.equal(sources[0].targetDir, "C:\\Users\\test\\docs");
});
Expand Down
4 changes: 4 additions & 0 deletions tests/sync-targets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ test("sync applies targetDir with copy mode", async () => {
});

test("sync applies targetDir with symlink mode", async () => {
if (process.platform === "win32") {
return;
}

const tmpRoot = path.join(
tmpdir(),
`docs-cache-target-link-${Date.now().toString(36)}`,
Expand Down
47 changes: 47 additions & 0 deletions tests/targets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import assert from "node:assert/strict";
import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { test } from "node:test";

import { applyTargetDir } from "../dist/api.mjs";

test("applyTargetDir warns and falls back to copy when symlink fails", async () => {
const tmpRoot = path.join(
tmpdir(),
`docs-cache-target-fallback-${Date.now().toString(36)}`,
);
const sourceDir = path.join(tmpRoot, "source");
const targetDir = path.join(tmpRoot, "target");

await mkdir(sourceDir, { recursive: true });
await writeFile(path.join(sourceDir, "README.md"), "hello", "utf8");

let stderr = "";
await applyTargetDir({
sourceDir,
targetDir,
mode: "symlink",
explicitTargetMode: true,
deps: {
cp,
mkdir,
rm,
symlink: async () => {
const error = new Error("symlink blocked");
error.code = "EPERM";
throw error;
},
stderr: {
write: (chunk) => {
stderr += String(chunk);
return true;
},
},
},
});

const data = await readFile(path.join(targetDir, "README.md"), "utf8");
assert.equal(data, "hello");
assert.match(stderr, /Warning: Failed to create symlink/i);
});
Loading