@effected/engine 0.16.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,108 @@
1
+ import { Clock, Duration, Effect, FileSystem, Option, Path, Schema } from "effect";
2
+
3
+ // src/build/BuildLock.ts
4
+ /**
5
+ * Who holds a package's build lock, as its lock file records it.
6
+ *
7
+ * @public
8
+ */
9
+ var BuildLockHolder = class extends Schema.Class("BuildLockHolder")({
10
+ /** The holding process. */
11
+ pid: Schema.Number,
12
+ /** When it took the lock, as an ISO timestamp. */
13
+ startedAt: Schema.String,
14
+ /** The package directory it is building. */
15
+ cwd: Schema.String
16
+ }) {};
17
+ /**
18
+ * Another build of the same package held the lock for the whole wait.
19
+ *
20
+ * @public
21
+ */
22
+ var BuildLockTimeoutError = class extends Schema.TaggedError()("BuildLockTimeoutError", {
23
+ /** The lock file. */
24
+ lockPath: Schema.String,
25
+ /** The build holding it when the wait ran out, when its lock file could be read. */
26
+ holder: Schema.optionalKey(BuildLockHolder),
27
+ /** How long this build waited, in milliseconds. */
28
+ waitedMs: Schema.Number
29
+ }) {
30
+ get message() {
31
+ const holder = this.holder !== void 0 ? `process ${this.holder.pid}, building ${this.holder.cwd} since ${this.holder.startedAt}` : "a build whose lock file could not be read";
32
+ return `Another build of this package held ${this.lockPath} for ${Math.round(this.waitedMs / 1e3)}s: ${holder}. Wait for it to finish, or delete the lock file if no build is running.`;
33
+ }
34
+ };
35
+ const DEFAULT_TIMEOUT = "5 minutes";
36
+ const DEFAULT_POLL = "200 millis";
37
+ const TORN_GRACE_MS = 5e3;
38
+ /** A pid that answers signal 0 runs; `EPERM` means it runs under another user. */
39
+ const hostIsAlive = (pid) => {
40
+ try {
41
+ process.kill(pid, 0);
42
+ return true;
43
+ } catch (error) {
44
+ return error.code === "EPERM";
45
+ }
46
+ };
47
+ const decodeHolder = Schema.decodeUnknownOption(Schema.fromJsonString(BuildLockHolder));
48
+ const encodeHolder = Schema.encodeSync(Schema.fromJsonString(BuildLockHolder));
49
+ /** The lock file for a package, under the workspace root. A scoped name keeps its scope, with `/` as `+`. */
50
+ const buildLockPath = (path, root, packageName) => path.join(root, ".cache", "effected", "locks", `${packageName.replace(/[^A-Za-z0-9._@-]/g, "+")}.lock`);
51
+ /**
52
+ * Holds a package's build lock for the enclosing scope.
53
+ *
54
+ * @remarks
55
+ * The lock is a file created exclusively, holding the process id, the start time and the package directory. While
56
+ * another build holds it, this one waits, telling `onWaiting` once who it waits for, and fails
57
+ * {@link BuildLockTimeoutError} when the wait runs out. A lock whose process no longer runs is removed and taken, and
58
+ * so is one that never got its content. The lock is released when the scope closes, interruption included, and only
59
+ * when the file is still this build's.
60
+ *
61
+ * Two waiters can find the same dead holder at once. Each removes the file only when it still reads as the dead
62
+ * holder's, which narrows the window in which one removes a lock the other has just taken, but does not close it.
63
+ */
64
+ const acquireBuildLock = (lockPath, cwd, options = {}, onWaiting = () => Effect.void) => Effect.gen(function* () {
65
+ const fs = yield* FileSystem.FileSystem;
66
+ const path = yield* Path.Path;
67
+ const isAlive = options.isAlive ?? hostIsAlive;
68
+ const pollMs = Duration.toMillis(options.pollInterval ?? DEFAULT_POLL);
69
+ const startMs = yield* Clock.currentTimeMillis;
70
+ const deadline = startMs + Duration.toMillis(options.timeout ?? DEFAULT_TIMEOUT);
71
+ const ours = encodeHolder(BuildLockHolder.make({
72
+ pid: options.pid ?? process.pid,
73
+ startedAt: new Date(startMs).toISOString(),
74
+ cwd
75
+ }));
76
+ yield* fs.makeDirectory(path.dirname(lockPath), { recursive: true });
77
+ const readHolder = fs.readFileString(lockPath).pipe(Effect.map(Option.some), Effect.catchReason("PlatformError", "NotFound", () => Effect.succeedNone));
78
+ /** Removes the lock when it still reads `text`, so a lock taken since is left alone. */
79
+ const reclaim = (text) => readHolder.pipe(Effect.flatMap((now) => Option.isSome(now) && now.value === text ? fs.remove(lockPath) : Effect.void), Effect.catchReason("PlatformError", "NotFound", () => Effect.void));
80
+ const tryCreate = Effect.acquireRelease(fs.writeFileString(lockPath, ours, { flag: "wx" }).pipe(Effect.as(true), Effect.catchReason("PlatformError", "AlreadyExists", () => Effect.succeed(false))), (created) => created ? reclaim(ours).pipe(Effect.ignore) : Effect.void);
81
+ const isTorn = fs.stat(lockPath).pipe(Effect.flatMap((info) => Effect.map(Clock.currentTimeMillis, (now) => Option.match(info.mtime, {
82
+ onNone: () => true,
83
+ onSome: (mtime) => now - mtime.getTime() > TORN_GRACE_MS
84
+ }))), Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(false)));
85
+ let told = false;
86
+ while (!(yield* tryCreate)) {
87
+ const text = yield* readHolder;
88
+ if (Option.isNone(text)) continue;
89
+ const holder = decodeHolder(text.value);
90
+ if (Option.isSome(holder) ? !isAlive(holder.value.pid) : yield* isTorn) {
91
+ yield* reclaim(text.value);
92
+ continue;
93
+ }
94
+ if (!told) {
95
+ told = true;
96
+ yield* onWaiting(holder);
97
+ }
98
+ const now = yield* Clock.currentTimeMillis;
99
+ if (now >= deadline) return yield* new BuildLockTimeoutError({
100
+ lockPath,
101
+ ...Option.isSome(holder) ? { holder: holder.value } : {},
102
+ waitedMs: now - startMs
103
+ });
104
+ yield* Effect.sleep(pollMs);
105
+ }
106
+ });
107
+
108
+ export { BuildLockHolder, BuildLockTimeoutError, acquireBuildLock, buildLockPath };
package/build/BuildRun.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { acquireBuildLock, buildLockPath } from "./BuildLock.js";
1
2
  import { BuildRunResult } from "./BuildRunResult.js";
2
3
  import { DateTime, Effect, FileSystem, Layer, Option, Path, PlatformError, Predicate, Schema } from "effect";
3
4
  import { BuildIssuesSchema, BuildJournal, Bundler, EffectedConfigError, EffectedConfigFile, IssueJournal, flattenIssues, loadPlugins } from "@effected/bundler";
@@ -87,7 +88,12 @@ const readIssues = Effect.fnUntraced(function* (outcome, packageName, startedAt)
87
88
  * A build that crashes or records an error is a `failed` verdict rather than a failure, so a front end can always
88
89
  * show the report and the journals always get an envelope. The error channel holds a run that could not start or
89
90
  * record: no `package.json`, a config or plugin that will not load, a config the bundler rejects before any build
90
- * work, and a journal that cannot be written.
91
+ * work, a journal that cannot be written, and another build of the same package that held its lock for the whole
92
+ * wait ({@link BuildLockTimeoutError}).
93
+ *
94
+ * From before the bundler cleans `dist` until the journals have the answer, the build holds the package's lock,
95
+ * `<root>/.cache/effected/locks/<name>.lock`, so two builds of one package in one tree run one after the other rather
96
+ * than writing the same `dist/prod`.
91
97
  *
92
98
  * @public
93
99
  */
@@ -110,45 +116,57 @@ var BuildRun = class {
110
116
  modules: build.plugins,
111
117
  ...deps.importModule !== void 0 ? { importModule: deps.importModule } : {}
112
118
  });
113
- yield* onProgress?.({
114
- _tag: "Started",
115
- package: packageName,
116
- config: resolved
117
- }) ?? Effect.void;
118
- const startedAt = DateTime.toEpochMillis(yield* DateTime.now);
119
- const outcome = yield* Bundler.build({
120
- cwd,
121
- config: build,
122
- bundle,
123
- noExe: input.noExe,
124
- verbose: input.verbose,
125
- plugins,
126
- ci: deps.ci,
127
- ...deps.seams !== void 0 ? { seams: deps.seams } : {}
128
- }).pipe(Effect.map((result) => ({
129
- report: result.report,
130
- issuesPath: result.issuesPath,
131
- crash: Option.none()
132
- })), Effect.catchTag("BundlerError", (error) => Effect.succeed({
133
- report: error.report,
134
- issuesPath: pathService.join(cwd, "dist", "prod", "issues.json"),
135
- crash: Option.some({
136
- name: error.name,
137
- message: error.message
138
- })
139
- })));
140
- if (onProgress !== void 0) for (const group of outcome.report.targetGroups) for (const pass of group.passes) yield* onProgress({
141
- _tag: "PassDone",
142
- pass: pass.id,
143
- group: group.id
144
- });
145
- const issues = yield* readIssues(outcome, packageName, startedAt);
146
- const journals = Layer.mergeAll(BuildJournal.layerAt(root), IssueJournal.layerAt(root)).pipe(Layer.provide(deps.journalWatcher ?? NodeJournalWatcher.layer));
147
- yield* BuildJournal.record(issues, cwd).pipe(Effect.provide(journals));
148
- yield* onProgress?.({
149
- _tag: "Done",
150
- issues
151
- }) ?? Effect.void;
119
+ const lockPath = buildLockPath(pathService, root, packageName);
120
+ const { outcome, issues } = yield* Effect.scoped(Effect.gen(function* () {
121
+ yield* acquireBuildLock(lockPath, cwd, deps.lock, (holder) => onProgress !== void 0 ? onProgress({
122
+ _tag: "Waiting",
123
+ lockPath,
124
+ holder
125
+ }) : Effect.void);
126
+ yield* onProgress?.({
127
+ _tag: "Started",
128
+ package: packageName,
129
+ config: resolved
130
+ }) ?? Effect.void;
131
+ const startedAt = DateTime.toEpochMillis(yield* DateTime.now);
132
+ const outcome = yield* Bundler.build({
133
+ cwd,
134
+ config: build,
135
+ bundle,
136
+ noExe: input.noExe,
137
+ verbose: input.verbose,
138
+ plugins,
139
+ ci: deps.ci,
140
+ ...deps.seams !== void 0 ? { seams: deps.seams } : {}
141
+ }).pipe(Effect.map((result) => ({
142
+ report: result.report,
143
+ issuesPath: result.issuesPath,
144
+ crash: Option.none()
145
+ })), Effect.catchTag("BundlerError", (error) => Effect.succeed({
146
+ report: error.report,
147
+ issuesPath: pathService.join(cwd, "dist", "prod", "issues.json"),
148
+ crash: Option.some({
149
+ name: error.name,
150
+ message: error.message
151
+ })
152
+ })));
153
+ if (onProgress !== void 0) for (const group of outcome.report.targetGroups) for (const pass of group.passes) yield* onProgress({
154
+ _tag: "PassDone",
155
+ pass: pass.id,
156
+ group: group.id
157
+ });
158
+ const issues = yield* readIssues(outcome, packageName, startedAt);
159
+ const journals = Layer.mergeAll(BuildJournal.layerAt(root), IssueJournal.layerAt(root)).pipe(Layer.provide(deps.journalWatcher ?? NodeJournalWatcher.layer));
160
+ yield* BuildJournal.record(issues, cwd).pipe(Effect.provide(journals));
161
+ yield* onProgress?.({
162
+ _tag: "Done",
163
+ issues
164
+ }) ?? Effect.void;
165
+ return {
166
+ outcome,
167
+ issues
168
+ };
169
+ }));
152
170
  return BuildRunResult.make({
153
171
  package: packageName,
154
172
  verdict: !issues.buildOk || issues.errors.length > 0 ? "failed" : "ok",
package/build.d.ts CHANGED
@@ -1,7 +1,56 @@
1
+ /**
2
+ * The `effected package build`, `effected action build` and `effected action init` machinery the tool's front ends share.
3
+ *
4
+ * @packageDocumentation
5
+ */
1
6
  import { ActionBuildResult, ActionBuildResult as ActionBuildResult$1, ActionValidation, ActionYmlPathError, BuildIssues, BuildIssues as BuildIssues$1, BuildReport, BuildReport as BuildReport$1, BuildResult, BundleFailed, BundleResult, BundlerSeams, CleanError, ConfigValidationError, EffectedConfigError, PassReport, PersistLocalError, PluginLoadError, ResolvedEffectedConfig, ValidationIssue, ValidationResult, WriteError, renderCiAnnotations } from "@effected/bundler";
2
- import { Effect, FileSystem, Layer, Path, PlatformError, Schema } from "effect";
7
+ import { Duration, Effect, FileSystem, Layer, Option, Path, PlatformError, Schema } from "effect";
3
8
  import { AppendError, JournalWatcher } from "@effected/jsonl";
4
- import "@effected/workspaces";
9
+ declare const BuildLockHolder_base: Schema.Class<BuildLockHolder, Schema.Struct<{
10
+ /** The holding process. */
11
+ readonly pid: Schema.Number;
12
+ /** When it took the lock, as an ISO timestamp. */
13
+ readonly startedAt: Schema.String;
14
+ /** The package directory it is building. */
15
+ readonly cwd: Schema.String;
16
+ }>, {}>;
17
+ /**
18
+ * Who holds a package's build lock, as its lock file records it.
19
+ *
20
+ * @public
21
+ */
22
+ export declare class BuildLockHolder extends BuildLockHolder_base {}
23
+ declare const BuildLockTimeoutError_base: Schema.Class<BuildLockTimeoutError, Schema.TaggedStruct<"BuildLockTimeoutError", {
24
+ /** The lock file. */
25
+ readonly lockPath: Schema.String;
26
+ /** The build holding it when the wait ran out, when its lock file could be read. */
27
+ readonly holder: Schema.optionalKey<typeof BuildLockHolder>;
28
+ /** How long this build waited, in milliseconds. */
29
+ readonly waitedMs: Schema.Number;
30
+ }>, import("effect/Cause").YieldableError>;
31
+ /**
32
+ * Another build of the same package held the lock for the whole wait.
33
+ *
34
+ * @public
35
+ */
36
+ export declare class BuildLockTimeoutError extends BuildLockTimeoutError_base {
37
+ get message(): string;
38
+ }
39
+ /**
40
+ * How a build waits for its package's lock, and the test seams.
41
+ *
42
+ * @public
43
+ */
44
+ interface BuildLockOptions {
45
+ /** How long to wait for another build of the package; omitted, five minutes. */
46
+ readonly timeout?: Duration.Input;
47
+ /** How often to look again while waiting; omitted, 200 milliseconds. */
48
+ readonly pollInterval?: Duration.Input;
49
+ /** This process's id, written into the lock; omitted, `process.pid`. */
50
+ readonly pid?: number;
51
+ /** Whether a holder's process still runs; omitted, a signal-0 probe of the pid. */
52
+ readonly isAlive?: (pid: number) => boolean;
53
+ }
5
54
  declare const BuildRunResult_base: Schema.Class<BuildRunResult, Schema.Struct<{
6
55
  /** The package's name. */
7
56
  readonly package: Schema.String;
@@ -96,7 +145,9 @@ interface BuildRunInput {
96
145
  *
97
146
  * @remarks
98
147
  * `PassDone` is read from the report once the bundler returns, so a front end gets them together rather than as
99
- * each pass ends. A crashed build reports the passes it recorded before the crash.
148
+ * each pass ends. A crashed build reports the passes it recorded before the crash. `Waiting` comes at most once, before
149
+ * the build starts, when another build of the same package holds its lock; `holder` is absent when the lock file
150
+ * could not be read.
100
151
  *
101
152
  * @public
102
153
  */
@@ -104,6 +155,10 @@ type BuildProgress = {
104
155
  readonly _tag: "Started";
105
156
  readonly package: string;
106
157
  readonly config: ResolvedEffectedConfig;
158
+ } | {
159
+ readonly _tag: "Waiting";
160
+ readonly lockPath: string;
161
+ readonly holder: Option.Option<BuildLockHolder>;
107
162
  } | {
108
163
  readonly _tag: "PassDone";
109
164
  readonly pass: PassReport["id"];
@@ -128,8 +183,10 @@ interface BuildRunDeps {
128
183
  readonly seams?: Partial<BundlerSeams>;
129
184
  /** The journals' watcher; omitted, `NodeJournalWatcher.layer`. */
130
185
  readonly journalWatcher?: Layer.Layer<JournalWatcher>;
131
- /** Told when the build starts, of each pass and when it is done. */
186
+ /** Told when the build starts, of each pass and when it is done, and when it waits for another build. */
132
187
  readonly onProgress?: (event: BuildProgress) => Effect.Effect<void>;
188
+ /** How the build waits for another build of the same package; omitted, five minutes and the host's process. */
189
+ readonly lock?: BuildLockOptions;
133
190
  }
134
191
  /**
135
192
  * Runs one package build.
@@ -138,13 +195,18 @@ interface BuildRunDeps {
138
195
  * A build that crashes or records an error is a `failed` verdict rather than a failure, so a front end can always
139
196
  * show the report and the journals always get an envelope. The error channel holds a run that could not start or
140
197
  * record: no `package.json`, a config or plugin that will not load, a config the bundler rejects before any build
141
- * work, and a journal that cannot be written.
198
+ * work, a journal that cannot be written, and another build of the same package that held its lock for the whole
199
+ * wait ({@link BuildLockTimeoutError}).
200
+ *
201
+ * From before the bundler cleans `dist` until the journals have the answer, the build holds the package's lock,
202
+ * `<root>/.cache/effected/locks/<name>.lock`, so two builds of one package in one tree run one after the other rather
203
+ * than writing the same `dist/prod`.
142
204
  *
143
205
  * @public
144
206
  */
145
207
  export declare class BuildRun {
146
208
  private constructor();
147
- static readonly run: (input: BuildRunInput, deps: BuildRunDeps) => Effect.Effect<BuildRunResult, EffectedConfigError | PluginLoadError | NotAPackageError | ConfigValidationError | AppendError | PlatformError.PlatformError, FileSystem.FileSystem | Path.Path>;
209
+ static readonly run: (input: BuildRunInput, deps: BuildRunDeps) => Effect.Effect<BuildRunResult, EffectedConfigError | PluginLoadError | NotAPackageError | ConfigValidationError | AppendError | BuildLockTimeoutError | PlatformError.PlatformError, FileSystem.FileSystem | Path.Path>;
148
210
  }
149
211
  declare const ActionScaffoldFile_base: Schema.Class<ActionScaffoldFile, Schema.Struct<{
150
212
  /** The path, relative to the action's directory, with `/` separators. */
@@ -442,5 +504,5 @@ export declare class ActionRun {
442
504
  private constructor();
443
505
  static readonly run: (input: ActionRunInput, deps: ActionRunDeps) => Effect.Effect<ActionRunResult, EffectedConfigError | NotAPackageError | NotAnActionError | BundleFailed | CleanError | WriteError | PersistLocalError | ActionYmlPathError | PlatformError.PlatformError, FileSystem.FileSystem | Path.Path>;
444
506
  }
445
- export { ActionBuildResult, type ActionInitDeps, type ActionInitInput, type ActionRunDeps, type ActionRunInput, type ActionScaffoldInput, ActionValidation, type BuildIssues, type BuildProgress, type BuildReport, BuildResult, type BuildRunDeps, type BuildRunInput, type BundleResult, type ValidationIssue, ValidationResult, renderCiAnnotations };
507
+ export { ActionBuildResult, type ActionInitDeps, type ActionInitInput, type ActionRunDeps, type ActionRunInput, type ActionScaffoldInput, ActionValidation, type BuildIssues, type BuildLockOptions, type BuildProgress, type BuildReport, BuildResult, type BuildRunDeps, type BuildRunInput, type BundleResult, type ValidationIssue, ValidationResult, renderCiAnnotations };
446
508
  //# sourceMappingURL=build.d.ts.map
package/build.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { ActionInit, ActionInitResult, ActionMetadataInvalidError, ActionMetadataMissingError, ActionScaffold, ActionScaffoldFile, ScaffoldConflict, ScaffoldConflictError, ScaffoldExistsError, ScaffoldWriteError } from "./build/ActionInit.js";
2
2
  import { ActionRun, NotAnActionError } from "./build/ActionRun.js";
3
3
  import { ActionRunResult } from "./build/ActionRunResult.js";
4
+ import { BuildLockHolder, BuildLockTimeoutError } from "./build/BuildLock.js";
4
5
  import { BuildRun, NotAPackageError } from "./build/BuildRun.js";
5
6
  import { BuildRunResult } from "./build/BuildRunResult.js";
6
7
  import { ActionBuildResult, ActionValidation, BuildResult, ValidationResult, renderCiAnnotations } from "@effected/bundler";
7
8
 
8
- export { ActionBuildResult, ActionInit, ActionInitResult, ActionMetadataInvalidError, ActionMetadataMissingError, ActionRun, ActionRunResult, ActionScaffold, ActionScaffoldFile, ActionValidation, BuildResult, BuildRun, BuildRunResult, NotAPackageError, NotAnActionError, ScaffoldConflict, ScaffoldConflictError, ScaffoldExistsError, ScaffoldWriteError, ValidationResult, renderCiAnnotations };
9
+ export { ActionBuildResult, ActionInit, ActionInitResult, ActionMetadataInvalidError, ActionMetadataMissingError, ActionRun, ActionRunResult, ActionScaffold, ActionScaffoldFile, ActionValidation, BuildLockHolder, BuildLockTimeoutError, BuildResult, BuildRun, BuildRunResult, NotAPackageError, NotAnActionError, ScaffoldConflict, ScaffoldConflictError, ScaffoldExistsError, ScaffoldWriteError, ValidationResult, renderCiAnnotations };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effected/engine",
3
- "version": "0.16.0",
3
+ "version": "0.17.1",
4
4
  "private": false,
5
5
  "description": "The effected tool's shared logic, consumed by its CLI and MCP front ends",
6
6
  "keywords": [
@@ -46,14 +46,14 @@
46
46
  }
47
47
  },
48
48
  "dependencies": {
49
- "@effected/bundler": "0.3.0",
50
- "@effected/files": "^0.3.0",
51
- "@effected/jsonc": "^0.25.0",
52
- "@effected/jsonl": "^0.19.0",
53
- "@effected/kit": "^0.14.0",
49
+ "@effected/bundler": "0.4.1",
50
+ "@effected/files": "0.3.0",
51
+ "@effected/jsonc": "0.25.0",
52
+ "@effected/jsonl": "0.19.0",
53
+ "@effected/kit": "0.14.0",
54
54
  "@effected/schemastore": "0.32.0",
55
- "@effected/source": "^0.5.0",
56
- "@effected/workspaces": "^0.45.0",
55
+ "@effected/source": "0.5.0",
56
+ "@effected/workspaces": "0.47.0",
57
57
  "ajv": "8.20.0",
58
58
  "ajv-formats": "3.0.1",
59
59
  "effect": "4.1.3",
package/schema.d.ts CHANGED
@@ -1,3 +1,8 @@
1
+ /**
2
+ * The `effected schema` machinery the tool's front ends share.
3
+ *
4
+ * @packageDocumentation
5
+ */
1
6
  import { CanonicalJsonError, DriftTolerance, DriftVerdict, InstanceValidator, InstanceValidatorError, OnDrift, PipelineFinding, SchemaConversionError, SchemaFile, SchemaFileReadError, SchemaFileWriteError, SchemaValidator, SchemaValidatorError, SchemastoreConfig, UndeclaredAnnotationKeyError, WriteChange } from "@effected/schemastore";
2
7
  import { Effect, FileSystem, Layer, Option, Path, PlatformError, Schema } from "effect";
3
8
  /**