Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion src/cache/targets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ export const applyTargetDir = async (params: TargetParams) => {

const type = process.platform === "win32" ? "junction" : "dir";
try {
await deps.symlink(sourceDir, params.targetDir, type);
const linkTarget =
process.platform === "win32"
? sourceDir
: path.relative(parentDir, sourceDir);
await deps.symlink(linkTarget, params.targetDir, type);
} catch (error) {
const code = getErrnoCode(error);
const fallbackCodes = new Set(["EPERM", "EACCES", "ENOTSUP", "EINVAL"]);
Expand Down
27 changes: 26 additions & 1 deletion tests/targets.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "node:assert/strict";
import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { cp, mkdir, readFile, readlink, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { test } from "node:test";
Expand Down Expand Up @@ -45,3 +45,28 @@ test("applyTargetDir warns and falls back to copy when symlink fails", async ()
assert.equal(data, "hello");
assert.match(stderr, /Warning: Failed to create symlink/i);
});

test("applyTargetDir uses relative symlink targets on non-Windows", async (t) => {
if (process.platform === "win32") {
t.skip("Relative symlink targets are not used on Windows.");
Comment thread
fbosch marked this conversation as resolved.
Outdated
}
const tmpRoot = path.join(
tmpdir(),
`docs-cache-target-relative-${Date.now().toString(36)}`,
);
const sourceDir = path.join(tmpRoot, "source");
const targetDir = path.join(tmpRoot, "target");
const parentDir = path.dirname(targetDir);

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

await applyTargetDir({
sourceDir,
targetDir,
mode: "symlink",
});

const linkTarget = await readlink(targetDir);
assert.equal(linkTarget, path.relative(parentDir, sourceDir));
});
Loading