Skip to content

Commit 11df252

Browse files
committed
πŸ› fix: create the temporary directory atomically
`randomUUID()` then `ensureDir()` names a directory and creates it as two steps, and `ensureDir` succeeds on one that already exists β€” so neither half rules out handing back a directory this call did not create. `mkdtemp` does both at once and fails rather than adopting. The test fixtures move to `@effectionx/fs` for the operations it provides β€” `writeTextFile`, `readTextFile`, `exists`, `rm` β€” and to a resource whose `ensure` removes the fixture, instead of synchronous try/finally. Node stays only where there is no equivalent: `mkdtemp`, `realpath`, and asking whether a pid is alive. The specification no longer says import consults built-ins before the filesystem. That sentence decided the precedence question #202 owns, in the same paragraph that deferred it. What is contracted is that a built-in resolves with no path and no search directory; a collision with a repository-local name is unspecified, and where the lookup happens to sit is incidental.
1 parent 2309be3 commit 11df252

4 files changed

Lines changed: 179 additions & 205 deletions

File tree

β€Žpackages/core/src/components/TempDir.tsβ€Ž

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
* the path it renders, and removed with that scope.
99
*/
1010

11-
import { ensure, resource } from "effection";
11+
import { ensure, resource, until } from "effection";
1212
import type { Operation } from "effection";
13-
import { until } from "effection";
14-
import { ensureDir, rm } from "@effectionx/fs";
13+
import { rm } from "@effectionx/fs";
1514
import { API } from "@executablemd/runtime";
16-
import { realpath } from "node:fs/promises";
15+
import { mkdtemp, realpath } from "node:fs/promises";
1716
import { tmpdir } from "node:os";
1817
import { join } from "node:path";
19-
import { randomUUID } from "node:crypto";
2018
import { retain } from "../component-api.ts";
2119
import { hasContent, useContent } from "../content-context.ts";
2220

@@ -27,17 +25,22 @@ export const props = {
2725
};
2826

2927
/**
30-
* The directory's canonical path, not the one `tmpdir()` reports.
28+
* A directory this call created, named by its canonical path.
3129
*
32-
* On macOS `tmpdir()` is a symlink (`/var/folders/…`) while a child process
33-
* resolves it (`/private/var/…`). Canonicalizing here is what makes the
34-
* rendered path, `Env.cwd`, and a subprocess's own `cwd` the same string, so a
35-
* document can compare them.
30+
* `mkdtemp` creates and names in one step, so the directory cannot already
31+
* exist and cannot be claimed between the two β€” which generating a name and
32+
* then creating it does not rule out.
33+
*
34+
* The path is then canonicalized: on macOS `tmpdir()` is a symlink
35+
* (`/var/folders/…`) while a child process resolves it (`/private/var/…`).
36+
* Canonicalizing is what makes the rendered path, `Env.cwd`, and a
37+
* subprocess's own `cwd` the same string, so a document can compare them.
38+
*
39+
* `@effectionx/fs` has neither operation, so both come from Node.
3640
*/
3741
function useTemporaryDirectory(): Operation<string> {
3842
return resource(function* (provide) {
39-
const created = join(tmpdir(), `xmd-tempdir-${randomUUID()}`);
40-
yield* ensureDir(created);
43+
const created = yield* until(mkdtemp(join(tmpdir(), "xmd-tempdir-")));
4144
yield* ensure(() => rm(created, { recursive: true, force: true }));
4245
yield* provide(yield* until(realpath(created)));
4346
});

β€Žpackages/core/src/components/registry.tsβ€Ž

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
* path or a bundling step. Import consults this map and returns the definition
77
* directly.
88
*
9-
* Deliberately module-private β€” not exported from `mod.ts`. What a host may
10-
* add, whether a repository file may shadow one of these names, and how
11-
* inspection reports the choice are #202's to settle.
9+
* Deliberately module-private β€” not exported from `mod.ts`. What is contracted
10+
* is only that a built-in resolves with no path and no search directory; where
11+
* this lookup sits relative to filesystem resolution is incidental and may
12+
* change. What a host may add, what happens when a repository file shares one
13+
* of these names, and how inspection reports the choice are #202's to settle.
1214
*/
1315

1416
import type { FunctionComponentDefinition } from "../types.ts";

0 commit comments

Comments
Β (0)