@tsplc/compiler 0.5.13 → 0.5.15

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.
Files changed (263) hide show
  1. package/README.md +33 -8
  2. package/dist/cli.js +794 -621
  3. package/dist/cli.js.map +19 -16
  4. package/dist/clock.js +5 -2
  5. package/dist/clock.js.map +5 -4
  6. package/dist/codesys.js +4162 -4157
  7. package/dist/codesys.js.map +43 -29
  8. package/dist/index.js +7852 -6549
  9. package/dist/index.js.map +53 -50
  10. package/dist/ir.js +2482 -2277
  11. package/dist/ir.js.map +28 -25
  12. package/dist/retain.js +3 -1
  13. package/dist/retain.js.map +3 -3
  14. package/dist/signals.js +115 -0
  15. package/dist/signals.js.map +10 -0
  16. package/dist/telemetry.js +25 -20
  17. package/dist/telemetry.js.map +6 -6
  18. package/package.json +19 -39
  19. package/src/codesys/config.ts +3 -3
  20. package/src/codesys/debug.ts +92 -108
  21. package/src/codesys/emit.ts +100 -88
  22. package/src/codesys/endpoints.ts +111 -0
  23. package/src/codesys/engineering.ts +30 -54
  24. package/src/codesys/explain/expressions.ts +445 -0
  25. package/src/codesys/explain/flags.ts +134 -0
  26. package/src/codesys/explain/parameters.ts +53 -0
  27. package/src/codesys/explain/rows.ts +182 -0
  28. package/src/codesys/explain/tables.ts +79 -0
  29. package/src/codesys/explain/tasks.ts +68 -0
  30. package/src/codesys/explain/writes.ts +497 -0
  31. package/src/codesys/explain.ts +85 -1401
  32. package/src/codesys/expressions.ts +41 -33
  33. package/src/codesys/global-lists.ts +114 -94
  34. package/src/codesys/global-names.ts +12 -14
  35. package/src/codesys/http.ts +428 -388
  36. package/src/codesys/instrumentation.ts +142 -148
  37. package/src/codesys/io-paths.ts +31 -23
  38. package/src/codesys/loops.ts +101 -0
  39. package/src/codesys/manifest.ts +269 -269
  40. package/src/codesys/names.ts +10 -33
  41. package/src/codesys/numeric.ts +89 -42
  42. package/src/codesys/plan.ts +202 -190
  43. package/src/codesys/programs.ts +23 -16
  44. package/src/codesys/publication.ts +13 -86
  45. package/src/codesys/required.ts +8 -0
  46. package/src/codesys/routines.ts +103 -489
  47. package/src/codesys/scheduling.ts +96 -0
  48. package/src/codesys/service-channels.ts +23 -0
  49. package/src/codesys/sources.ts +34 -28
  50. package/src/codesys/st-files.ts +112 -105
  51. package/src/codesys/statements.ts +409 -0
  52. package/src/codesys/storage.ts +41 -23
  53. package/src/codesys/symbols.ts +130 -119
  54. package/src/codesys/task-access.ts +65 -221
  55. package/src/codesys/telemetry-settings.ts +19 -0
  56. package/src/codesys/telemetry.ts +185 -148
  57. package/src/codesys/types.ts +69 -109
  58. package/src/codesys/vendor.ts +13 -50
  59. package/src/frontend/analyzer.ts +66 -111
  60. package/src/frontend/async.ts +316 -128
  61. package/src/frontend/blocks.ts +178 -154
  62. package/src/frontend/configuration.ts +49 -153
  63. package/src/frontend/const-functions.ts +50 -40
  64. package/src/frontend/control-flow.ts +164 -129
  65. package/src/frontend/cooperative.ts +17 -9
  66. package/src/frontend/coroutines.ts +539 -441
  67. package/src/frontend/dialect-names.ts +104 -0
  68. package/src/frontend/elaborate/array-searches.ts +77 -69
  69. package/src/frontend/elaborate/assignments.ts +68 -77
  70. package/src/frontend/elaborate/calls.ts +878 -512
  71. package/src/frontend/elaborate/capture-protocol.ts +118 -21
  72. package/src/frontend/elaborate/capture.ts +196 -109
  73. package/src/frontend/elaborate/declarations.ts +124 -0
  74. package/src/frontend/elaborate/dialect.ts +119 -46
  75. package/src/frontend/elaborate/expressions.ts +763 -442
  76. package/src/frontend/elaborate/filtered-arrays.ts +127 -91
  77. package/src/frontend/elaborate/frame.ts +50 -2
  78. package/src/frontend/elaborate/generics.ts +20 -34
  79. package/src/frontend/elaborate/graph.ts +13 -87
  80. package/src/frontend/elaborate/imports.ts +0 -5
  81. package/src/frontend/elaborate/index.ts +416 -169
  82. package/src/frontend/elaborate/initializers.ts +64 -42
  83. package/src/frontend/elaborate/iterators.ts +228 -364
  84. package/src/frontend/elaborate/layouts.ts +477 -116
  85. package/src/frontend/elaborate/model.ts +113 -55
  86. package/src/frontend/elaborate/references.ts +116 -138
  87. package/src/frontend/elaborate/retention.ts +137 -59
  88. package/src/frontend/elaborate/specialize.ts +617 -308
  89. package/src/frontend/elaborate/statements.ts +436 -247
  90. package/src/frontend/elaborate/storage.ts +280 -213
  91. package/src/frontend/elaborate/time-constructors.ts +34 -0
  92. package/src/frontend/elaborate/values.ts +77 -7
  93. package/src/frontend/elaborate/writes.ts +98 -84
  94. package/src/frontend/expressions.ts +149 -151
  95. package/src/frontend/generated-names.ts +3 -3
  96. package/src/frontend/intrinsics.ts +31 -17
  97. package/src/frontend/lowering-key.ts +23 -0
  98. package/src/frontend/module-graph.ts +105 -43
  99. package/src/frontend/positions.ts +43 -35
  100. package/src/frontend/promise-chains.ts +226 -266
  101. package/src/frontend/promises.ts +88 -67
  102. package/src/frontend/routines.ts +122 -65
  103. package/src/frontend/sdk.ts +104 -5
  104. package/src/frontend/services.ts +149 -102
  105. package/src/frontend/statements.ts +60 -64
  106. package/src/frontend/tasks.ts +151 -96
  107. package/src/frontend/telemetry.ts +1 -1
  108. package/src/frontend/types.ts +366 -181
  109. package/src/frontend/typescript.ts +239 -112
  110. package/src/index.ts +2 -1
  111. package/src/ir/aggregates.ts +90 -75
  112. package/src/ir/allocation.ts +29 -18
  113. package/src/ir/conditions.ts +38 -78
  114. package/src/ir/core-model.ts +128 -11
  115. package/src/ir/declarations.ts +18 -32
  116. package/src/ir/diagnostics.ts +25 -18
  117. package/src/ir/domains.ts +26 -23
  118. package/src/ir/fold.ts +55 -50
  119. package/src/ir/identity.ts +10 -0
  120. package/src/ir/index.ts +31 -7
  121. package/src/ir/job-errors.ts +30 -8
  122. package/src/ir/layouts.ts +63 -34
  123. package/src/ir/link.ts +11 -46
  124. package/src/ir/model.ts +25 -6
  125. package/src/ir/normalize.ts +3 -28
  126. package/src/ir/numeric.ts +45 -18
  127. package/src/ir/operands.ts +102 -0
  128. package/src/ir/ranges.ts +242 -67
  129. package/src/ir/routine.ts +126 -183
  130. package/src/ir/scalars.ts +29 -25
  131. package/src/ir/share.ts +323 -343
  132. package/src/ir/short-circuit.ts +140 -0
  133. package/src/ir/tables.ts +111 -0
  134. package/src/ir/temporaries.ts +15 -113
  135. package/src/ir/types.ts +32 -8
  136. package/src/project/artifacts.ts +143 -71
  137. package/src/project/cli.ts +104 -87
  138. package/src/project/debugger.ts +48 -34
  139. package/src/project/deployments.ts +12 -68
  140. package/src/project/flags.ts +14 -0
  141. package/src/project/plant.ts +293 -225
  142. package/src/project/project-commands.ts +254 -192
  143. package/src/project/project.ts +184 -141
  144. package/src/project/release.ts +22 -28
  145. package/src/project/server.ts +22 -26
  146. package/src/project/terminal.ts +13 -2
  147. package/src/runtime/async.ts +17 -13
  148. package/src/runtime/launch.ts +5 -2
  149. package/src/runtime/main.ts +8 -3
  150. package/src/runtime/missing-file.ts +3 -0
  151. package/src/runtime/process.ts +24 -19
  152. package/src/runtime/program.ts +20 -53
  153. package/src/runtime/retain.ts +18 -10
  154. package/src/runtime/runtime.ts +47 -60
  155. package/src/runtime/settings.ts +27 -0
  156. package/src/runtime/storage.ts +49 -74
  157. package/src/runtime/symbols.ts +19 -75
  158. package/src/runtime/telemetry.ts +37 -14
  159. package/src/runtime/transport.ts +26 -30
  160. package/stdlib/clock.ts +4 -2
  161. package/stdlib/plc-time.ts +2 -0
  162. package/stdlib/retain.ts +2 -0
  163. package/stdlib/signals.ts +142 -0
  164. package/stdlib/telemetry.ts +5 -12
  165. package/types/dialect.d.ts +49 -0
  166. package/types/globals.d.ts +2 -42
  167. package/types/index.d.ts +3 -1
  168. package/types/retain.d.ts +8 -2
  169. package/types/telemetry.d.ts +3 -0
  170. package/dist/bun-runtime.js +0 -1472
  171. package/dist/bun-runtime.js.map +0 -26
  172. package/dist/elaborate-capture.js +0 -304
  173. package/dist/elaborate-capture.js.map +0 -11
  174. package/dist/math.js +0 -24
  175. package/dist/math.js.map +0 -10
  176. package/dist/project.js +0 -626
  177. package/dist/project.js.map +0 -12
  178. package/dist/server-check.js +0 -59
  179. package/dist/server-check.js.map +0 -11
  180. package/src/frontend/exposure.ts +0 -181
  181. package/src/project/index.ts +0 -6
  182. package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
  183. package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
  184. package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
  185. package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
  186. package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
  187. package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
  188. package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
  189. package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
  190. package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
  191. package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
  192. package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
  193. package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
  194. package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
  195. package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
  196. package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
  197. package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
  198. package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
  199. package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
  200. package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
  201. package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
  202. package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
  203. package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
  204. package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
  205. package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
  206. package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
  207. package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
  208. package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
  209. package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
  210. package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
  211. package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
  212. package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
  213. package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
  214. package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
  215. package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
  216. package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
  217. package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
  218. package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
  219. package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
  220. package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
  221. package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
  222. package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
  223. package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
  224. package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
  225. package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
  226. package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
  227. package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
  228. package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
  229. package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
  230. package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
  231. package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
  232. package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
  233. package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
  234. package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
  235. package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
  236. package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
  237. package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
  238. package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
  239. package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
  240. package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
  241. package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
  242. package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
  243. package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
  244. package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
  245. package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
  246. package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
  247. package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
  248. package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
  249. package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
  250. package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
  251. package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
  252. package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
  253. package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
  254. package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
  255. package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
  256. package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
  257. package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
  258. package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
  259. package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
  260. package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
  261. package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
  262. package/stdlib/math.ts +0 -23
  263. package/types/plc.d.ts +0 -4
@@ -7,12 +7,12 @@ import {
7
7
  writeFile,
8
8
  } from "node:fs/promises";
9
9
  import { basename, dirname, join, resolve } from "node:path";
10
- import { installLibraries, type ProgressOptions } from "@tsplc/build";
11
10
  import {
12
- analyze,
13
- type CompileEnvironment,
14
- checkHostConfig,
15
- } from "@tsplc/compiler";
11
+ buildFiles,
12
+ installLibraries,
13
+ type ProgressOptions,
14
+ } from "@tsplc/build";
15
+ import { analyze, type BoundPath } from "@tsplc/compiler";
16
16
  import {
17
17
  buildCodesys,
18
18
  type CodesysPlan,
@@ -36,12 +36,17 @@ import {
36
36
  type Controller,
37
37
  compilerConfig,
38
38
  type LoadedConfig,
39
- loadTsplcConfig,
40
39
  type NormalizedConfig,
41
40
  type ResolvedTsplcConfig,
41
+ serverBaseUrl,
42
42
  simulationConfig,
43
+ taskPlan,
43
44
  } from "@tsplc/config";
44
- import { emittedFiles, emittedStDirectory } from "@tsplc/config/project-files";
45
+ import {
46
+ configFileName,
47
+ emittedFiles,
48
+ emittedStDirectory,
49
+ } from "@tsplc/config/project-files";
45
50
  import { explainFileName } from "@tsplc/debug/explain-file";
46
51
  import { debugManifestFile } from "@tsplc/debug/manifest";
47
52
  import {
@@ -52,13 +57,14 @@ import {
52
57
  } from "@tsplc/typegen";
53
58
  import { serviceRoutes } from "../runtime/launch";
54
59
  import {
60
+ claimed,
55
61
  cleanArtifacts,
56
62
  engineeringDirectory,
57
63
  owned,
58
64
  publishArtifacts,
65
+ withdrawReceipt,
59
66
  } from "./artifacts";
60
67
 
61
- /** The emitted ST's directory inside the compiler's artifacts directory. */
62
68
  const emittedSt = basename(emittedStDirectory);
63
69
 
64
70
  const failure = (
@@ -73,7 +79,7 @@ const failure = (
73
79
  });
74
80
 
75
81
  /**
76
- * `buildProject`'s `loaded` and `configured` options. With `simulate` the build omits fieldbus
82
+ * `buildTypeScriptProject`'s `loaded` and `configured` options. With `simulate` the build omits fieldbus
77
83
  * I/O so a plant simulator can write the inputs; generated types and the debug manifest keep
78
84
  * describing the hardware project.
79
85
  */
@@ -89,52 +95,52 @@ export const buildConfigs = (
89
95
  : { loaded };
90
96
 
91
97
  interface ProjectOptions {
92
- loaded?: LoadedConfig;
98
+ /** The config, whose `env` the program's `env()` calls, endpoints and telemetry read. */
99
+ loaded: LoadedConfig;
93
100
  buildTarget?: BuildTarget;
94
101
  /** The configuration `loaded` simulates, whose hardware the debug manifest describes. */
95
102
  configured?: ResolvedTsplcConfig;
96
- /**
97
- * The host config check's diagnostics, when the caller ran it before loading the config. The
98
- * check type-checks the config and takes seconds.
99
- */
100
- hostConfigDiagnostics?: readonly Diagnostic[];
101
- /** What the program's `env()` calls, endpoints and telemetry read; this process's environment by default. */
102
- environment?: CompileEnvironment;
103
+ /** The host config check's diagnostics, which type-check the config before it runs. */
104
+ hostConfigDiagnostics: readonly Diagnostic[];
103
105
  }
104
106
 
105
- /** Loads the config in `environment`, this process's by default, with its only target's values. */
106
- const loadEnvironment = (environment: CompileEnvironment | undefined) => ({
107
- env: environment ?? process.env,
108
- target: undefined,
109
- forAgent: false,
110
- });
111
-
107
+ /** The project `file` configures, or the diagnostics of why it can't be compiled. */
112
108
  async function loadProject(
113
109
  file: string,
114
- {
115
- loaded,
116
- buildTarget,
117
- configured,
118
- hostConfigDiagnostics,
119
- environment,
120
- }: ProjectOptions,
121
- ): Promise<{
122
- config?: NormalizedConfig;
123
- configFile: string;
124
- shared?: ResolvedTsplcConfig;
125
- project?: CodesysProject;
126
- diagnostics: readonly Diagnostic[];
127
- }> {
128
- let configFile = resolve(file);
110
+ { loaded, buildTarget, configured, hostConfigDiagnostics }: ProjectOptions,
111
+ ): Promise<
112
+ | {
113
+ config: NormalizedConfig;
114
+ configFile: string;
115
+ shared: ResolvedTsplcConfig;
116
+ project: CodesysProject;
117
+ /** The state paths the hardware's `io` binds, which a simulator writes too. */
118
+ bound: BoundPath[];
119
+ diagnostics: readonly Diagnostic[];
120
+ }
121
+ | { configFile: string; diagnostics: readonly Diagnostic[] }
122
+ > {
123
+ const unresolved = resolve(file);
124
+ const configFile = await realpath(unresolved).catch((cause: unknown) =>
125
+ unloaded(unresolved, cause),
126
+ );
127
+ if (typeof configFile !== "string") {
128
+ return configFile;
129
+ }
129
130
  try {
130
- configFile = await realpath(configFile);
131
- const diagnostics = hostConfigDiagnostics ?? checkHostConfig(configFile);
132
- if (hasErrors(diagnostics)) {
133
- return { configFile, diagnostics };
131
+ if (hasErrors(hostConfigDiagnostics)) {
132
+ return { configFile, diagnostics: hostConfigDiagnostics };
133
+ }
134
+ const { config: shared, env } = loaded;
135
+ if (
136
+ shared.debug?.trace &&
137
+ shared.server?.transport !== "opcua" &&
138
+ !serverBaseUrl(shared, env)
139
+ ) {
140
+ throw new Error(
141
+ 'debug.trace sends its records to the app server: configure server with baseUrlEnv and set that variable, or use server.transport "opcua".',
142
+ );
134
143
  }
135
- const { config: shared, env } =
136
- loaded ??
137
- (await loadTsplcConfig(configFile, loadEnvironment(environment)));
138
144
  const root = dirname(configFile);
139
145
  const catalog = await loadHardware(shared, root);
140
146
 
@@ -143,6 +149,9 @@ async function loadProject(
143
149
  resolveIo(shared, catalog),
144
150
  buildTarget,
145
151
  );
152
+ const hardware = configured
153
+ ? { config: configured, catalog: await loadHardware(configured, root) }
154
+ : { config: shared, catalog };
146
155
  return {
147
156
  configFile,
148
157
  shared,
@@ -156,18 +165,23 @@ async function loadProject(
156
165
  ...(shared.hmi && { hmi: shared.hmi }),
157
166
  // A simulation build drops fieldbus IO, but the debugger still shows the hardware it stands in for.
158
167
  hardware: {
159
- catalog: configured ? await loadHardware(configured, root) : catalog,
160
- config: configured ?? shared,
168
+ ...hardware,
161
169
  tree: shared.codesys?.deviceTree !== undefined,
162
170
  },
163
171
  },
164
- diagnostics,
172
+ bound: boundPaths(resolveIo(hardware.config, hardware.catalog)),
173
+ diagnostics: hostConfigDiagnostics,
165
174
  };
166
175
  } catch (cause) {
167
- return { configFile, diagnostics: [failure("config", "PLC5003", cause)] };
176
+ return unloaded(configFile, cause);
168
177
  }
169
178
  }
170
179
 
180
+ const unloaded = (configFile: string, cause: unknown) => ({
181
+ configFile,
182
+ diagnostics: [failure("config", "PLC5003", cause)],
183
+ });
184
+
171
185
  /** The analyzer's entry points, resolved against the config file's directory. */
172
186
  function sourceInput(
173
187
  compiler: {
@@ -193,13 +207,10 @@ function sourceInput(
193
207
  * it is undefined only while the source itself fails analysis.
194
208
  */
195
209
  export async function projectTargets(
196
- file = "tsplc.config.ts",
197
- options: Pick<ProjectOptions, "loaded" | "environment"> = {},
210
+ file: string,
211
+ { config, env }: LoadedConfig,
198
212
  ): Promise<ProjectTargets | undefined> {
199
213
  const configFile = await realpath(resolve(file));
200
- const { config, env } =
201
- options.loaded ??
202
- (await loadTsplcConfig(configFile, loadEnvironment(options.environment)));
203
214
  if (!config.compiler) {
204
215
  return undefined;
205
216
  }
@@ -207,19 +218,31 @@ export async function projectTargets(
207
218
  ...sourceInput(config.compiler, dirname(configFile)),
208
219
  services: config.server,
209
220
  environment: env,
221
+ // A device channel's direction needs the hardware, which check and build resolve.
222
+ io: boundPaths(config.io),
210
223
  });
211
224
  const core = source.module && normalizeModule(source.module).core;
212
225
  return core ? coreTargets(core) : undefined;
213
226
  }
214
227
 
228
+ /** The state paths `io` binds, each marked with the `%I` address the hardware writes it from. */
229
+ const boundPaths = (io: ResolvedTsplcConfig["io"]): BoundPath[] =>
230
+ Object.entries(io).map(([path, binding]) => ({
231
+ path,
232
+ input:
233
+ "address" in binding && binding.address.startsWith("%I")
234
+ ? binding.address
235
+ : null,
236
+ }));
237
+
215
238
  const coreTargets = (core: VerifiedCoreModule): ProjectTargets => ({
216
239
  io: ioTargets(core),
217
240
  publication: publicationSelectors(core),
218
241
  });
219
242
 
220
243
  export async function checkProject(
221
- file = "tsplc.config.ts",
222
- options: ProjectOptions = {},
244
+ file: string,
245
+ options: ProjectOptions,
223
246
  ): Promise<{
224
247
  configFile: string;
225
248
  config?: NormalizedConfig;
@@ -228,29 +251,17 @@ export async function checkProject(
228
251
  diagnostics: readonly Diagnostic[];
229
252
  }> {
230
253
  const loaded = await loadProject(file, options);
231
- if (!loaded.config || !loaded.project) {
254
+ if (!("project" in loaded)) {
232
255
  return loaded;
233
256
  }
234
257
 
235
- const config = loaded.config;
236
- if (config.target.kind !== "codesys") {
237
- return {
238
- ...loaded,
239
- diagnostics: [
240
- failure(
241
- "backend",
242
- "PLC7001",
243
- `Unsupported target: ${config.target.kind}`,
244
- ),
245
- ],
246
- };
247
- }
248
-
258
+ const { config } = loaded;
249
259
  const source = analyze({
250
260
  tsconfig: config.tsconfig,
251
261
  programs: config.programs,
252
- services: loaded.shared?.server,
262
+ services: loaded.shared.server,
253
263
  environment: loaded.project.environment,
264
+ io: loaded.bound,
254
265
  });
255
266
  if (!source.module) {
256
267
  return { ...loaded, diagnostics: source.diagnostics };
@@ -260,12 +271,7 @@ export async function checkProject(
260
271
  return { ...loaded, diagnostics: normalized.diagnostics };
261
272
  }
262
273
 
263
- const tasks = config.tasks.map((task) => ({
264
- name: task.name,
265
- intervalNanoseconds: task.schedule.interval.nanoseconds,
266
- priority: task.priority,
267
- programs: task.programs,
268
- }));
274
+ const tasks = taskPlan(config.tasks);
269
275
  const planned = planCodesys(
270
276
  normalized.core,
271
277
  { tasks },
@@ -306,15 +312,18 @@ async function engineeringInputs(
306
312
  };
307
313
  }
308
314
 
309
- export async function buildProject(
310
- file = "tsplc.config.ts",
315
+ const missingBuildSection =
316
+ 'Compiling with CODESYS needs a build section in the config, for example build: { outDir: "build/codesys" }. Add one, or emit the Structured Text only with build --emit-only.';
317
+
318
+ export async function buildTypeScriptProject(
319
+ file: string,
311
320
  options: ProjectOptions & {
312
321
  emitOnly?: boolean;
313
322
  frozenLockfile?: boolean;
314
323
  controller?: Controller;
315
324
  /** Resolve libraries and the device tree into `controllerConfig` even when only emitting. */
316
325
  forController?: boolean;
317
- } & ProgressOptions = {},
326
+ } & ProgressOptions,
318
327
  ): Promise<{
319
328
  directory?: string;
320
329
  status?: "emitted" | "vendor-compiled";
@@ -330,7 +339,9 @@ export async function buildProject(
330
339
  const configFile = await realpath(resolve(file));
331
340
  // Withdraw the previous success receipt before checking the new source.
332
341
  // Old artifacts remain owned and intact until publication succeeds.
333
- const previous = await owned(configFile, true);
342
+ const claim = await claimed(configFile);
343
+ await withdrawReceipt(claim);
344
+ const previous = await owned(claim);
334
345
  if (previous.unknown.length) {
335
346
  throw new Error(
336
347
  `Output contains unrelated files: ${previous.unknown.join(", ")}`,
@@ -342,26 +353,36 @@ export async function buildProject(
342
353
  return { diagnostics: checked.diagnostics };
343
354
  }
344
355
  const bundle = emitCodesys(checked.plan);
345
- if (hasErrors(bundle.diagnostics)) {
346
- return { diagnostics: bundle.diagnostics };
347
- }
348
356
  const shared = checked.shared;
357
+ const outDir =
358
+ shared?.build && resolve(dirname(configFile), shared.build.outDir);
359
+ if (!options.emitOnly && !outDir) {
360
+ return {
361
+ diagnostics: [
362
+ ...checked.diagnostics,
363
+ failure("config", "PLC5003", missingBuildSection),
364
+ ],
365
+ };
366
+ }
349
367
  const engineering =
350
368
  shared && (!options.emitOnly || options.forController)
351
369
  ? await engineeringInputs(shared, configFile, bundle, {
352
370
  frozen: options.frozenLockfile,
353
371
  })
354
372
  : undefined;
355
- const outDir =
356
- shared?.build && resolve(dirname(configFile), shared.build.outDir);
373
+ // The emitted ST already binds the program's I/O; bindings here would have the controller
374
+ // build bind it a second time.
357
375
  const controllerConfig = outDir
358
- ? codesysBuildConfig(checked.plan, bundle, {
359
- configFile,
360
- sourceDir: join(previous.output, emittedSt),
361
- outDir,
362
- controller: options.controller,
363
- ...engineering,
364
- })
376
+ ? {
377
+ ...codesysBuildConfig(checked.plan, bundle, {
378
+ configFile,
379
+ sourceDir: join(previous.output, emittedSt),
380
+ outDir,
381
+ controller: options.controller,
382
+ ...engineering,
383
+ }),
384
+ io: {},
385
+ }
365
386
  : undefined;
366
387
 
367
388
  await mkdir(previous.root, { recursive: true });
@@ -384,18 +405,12 @@ export async function buildProject(
384
405
  path: "config.json",
385
406
  content: `${JSON.stringify(checked.config, null, 2)}\n`,
386
407
  },
387
- ...(bundle.debug
388
- ? [
389
- {
390
- path: debugManifestFile,
391
- content: `${JSON.stringify(bundle.debug)}\n`,
392
- },
393
- ]
394
- : []),
408
+ {
409
+ path: debugManifestFile,
410
+ content: `${JSON.stringify(bundle.debug)}\n`,
411
+ },
395
412
  // The manifest's digest covers these exact bytes.
396
- ...(bundle.explain === undefined
397
- ? []
398
- : [{ path: explainFileName, content: bundle.explain }]),
413
+ { path: explainFileName, content: bundle.explain },
399
414
  ];
400
415
  const stagedSt = join(staging, emittedSt);
401
416
  // Thousands of artifacts share a few hundred directories; each is made once.
@@ -409,41 +424,20 @@ export async function buildProject(
409
424
  await writeFile(join(stagedSt, artifact.path), artifact.content);
410
425
  }
411
426
 
412
- let vendor: Awaited<ReturnType<typeof buildCodesys>> | undefined;
413
- if (!options.emitOnly) {
414
- // The engineering cache has its own ownership marker and survives clean.
415
- const engineeringDir = outDir ?? join(previous.root, "engineering");
416
- await engineeringDirectory(engineeringDir, configFile);
417
- try {
418
- vendor = await buildCodesys(
419
- checked.plan,
420
- bundle,
421
- {
427
+ const vendor =
428
+ options.emitOnly || !outDir
429
+ ? undefined
430
+ : await compileVendor(checked.plan, bundle, {
422
431
  configFile,
423
- sourceDir: join(staging, emittedSt),
424
- outDir: engineeringDir,
432
+ staging,
433
+ // The engineering cache has its own ownership marker and survives clean.
434
+ engineeringDir: outDir,
425
435
  controller: options.controller,
426
- ...engineering,
427
- },
428
- { progress: options.progress },
429
- );
430
- } catch (cause) {
431
- return { diagnostics: diagnoseCodesys(cause, bundle) };
432
- }
433
-
434
- const vendorArtifacts = [
435
- ["Application.project", vendor.project],
436
- ["Application.app", vendor.output],
437
- ["Application.crc", vendor.checksum],
438
- ] as const;
439
- for (const [name, path] of vendorArtifacts) {
440
- if (!path) {
441
- continue;
442
- }
443
- const destination = join(staging, "vendor", name);
444
- await mkdir(dirname(destination), { recursive: true });
445
- await copyFile(path, destination);
446
- }
436
+ engineering,
437
+ progress: options.progress,
438
+ });
439
+ if (vendor && "diagnostics" in vendor) {
440
+ return vendor;
447
441
  }
448
442
 
449
443
  const status = vendor ? "vendor-compiled" : "emitted";
@@ -462,7 +456,7 @@ export async function buildProject(
462
456
  ...(controllerConfig && { controllerConfig }),
463
457
  targets: coreTargets(checked.plan.core),
464
458
  services: serviceRoutes(checked.plan.core),
465
- diagnostics: [...checked.diagnostics, ...bundle.diagnostics],
459
+ diagnostics: checked.diagnostics,
466
460
  };
467
461
  } catch (cause) {
468
462
  return { diagnostics: [failure("config", "PLC5004", cause)] };
@@ -473,6 +467,55 @@ export async function buildProject(
473
467
  }
474
468
  }
475
469
 
470
+ /**
471
+ * Compiles the staged ST with CODESYS and copies the vendor's project, application and checksum
472
+ * into the staging tree's `vendor/`; the compiler's diagnostics when CODESYS refuses it.
473
+ */
474
+ async function compileVendor(
475
+ plan: CodesysPlan,
476
+ bundle: ReturnType<typeof emitCodesys>,
477
+ options: {
478
+ configFile: string;
479
+ staging: string;
480
+ engineeringDir: string;
481
+ controller: Controller | undefined;
482
+ engineering: Awaited<ReturnType<typeof engineeringInputs>> | undefined;
483
+ } & ProgressOptions,
484
+ ) {
485
+ await engineeringDirectory(options.engineeringDir, options.configFile);
486
+ const vendor = await buildCodesys(
487
+ plan,
488
+ bundle,
489
+ {
490
+ configFile: options.configFile,
491
+ sourceDir: join(options.staging, emittedSt),
492
+ outDir: options.engineeringDir,
493
+ controller: options.controller,
494
+ ...options.engineering,
495
+ },
496
+ { progress: options.progress },
497
+ ).catch((cause: unknown) => ({
498
+ diagnostics: diagnoseCodesys(cause, bundle),
499
+ }));
500
+ if ("diagnostics" in vendor) {
501
+ return vendor;
502
+ }
503
+ for (const artifact of ["project", "output", "checksum"] as const) {
504
+ const path = vendor[artifact];
505
+ if (!path) {
506
+ continue;
507
+ }
508
+ const destination = join(
509
+ options.staging,
510
+ "vendor",
511
+ basename(buildFiles[artifact]),
512
+ );
513
+ await mkdir(dirname(destination), { recursive: true });
514
+ await copyFile(path, destination);
515
+ }
516
+ return vendor;
517
+ }
518
+
476
519
  /**
477
520
  * Source mappings with each ST path and source file written once: `mappings` rows are
478
521
  * `[object, line, file, start, end, sourceLine, sourceColumn]`, indexing `objects` and `files`.
@@ -504,7 +547,7 @@ function compactMappings(mappings: readonly SourceMapping[]) {
504
547
  };
505
548
  }
506
549
 
507
- export async function cleanProject(file = "tsplc.config.ts"): Promise<{
550
+ export async function cleanProject(file = configFileName): Promise<{
508
551
  diagnostics: readonly Diagnostic[];
509
552
  }> {
510
553
  try {
@@ -1,13 +1,13 @@
1
- import { existsSync } from "node:fs";
2
1
  import { basename, relative, resolve, sep } from "node:path";
3
2
  import { pathToFileURL } from "node:url";
4
3
  import type { ReleaseManifest } from "@tsplc/agent";
5
4
  import type { ReleaseFile } from "@tsplc/agent/client";
6
- import type { CompileEnvironment } from "@tsplc/compiler";
5
+ import { checkHostConfig } from "@tsplc/compiler";
7
6
  import { hasErrors } from "@tsplc/compiler/ir";
8
7
  import {
9
8
  buildTargetFor,
10
9
  controllerFor,
10
+ type Environment,
11
11
  type LoadedConfig,
12
12
  serverTokenEnv,
13
13
  } from "@tsplc/config";
@@ -15,23 +15,17 @@ import type { ReleaseConfig } from "@tsplc/config/build";
15
15
  import { explainFileName } from "@tsplc/debug/explain-file";
16
16
  import { debugManifestFile } from "@tsplc/debug/manifest";
17
17
  import { contractVariables, plcContractSchema } from "@tsplc/opcua/contract";
18
- import { generatedFiles, generateProject } from "@tsplc/typegen";
19
- import { buildConfigs, buildProject } from "./project";
20
- import { bundleServer, checkServer } from "./server";
18
+ import { generateProject } from "@tsplc/typegen";
19
+ import { buildConfigs, buildTypeScriptProject } from "./project";
20
+ import { bundleServer, startServerCheck } from "./server";
21
21
 
22
- /** Every published leaf the generated contract describes, as CODESYS expressions name it. */
23
- const releaseVariables = async (
24
- config: LoadedConfig["config"],
25
- root: string,
26
- ) => {
27
- if (!config.typegen) {
22
+ /** Every published leaf the generated contract at `file` describes, as CODESYS expressions name it. */
23
+ const releaseVariables = async (file: string | null) => {
24
+ if (file === null) {
28
25
  return [];
29
26
  }
30
27
  const contract = plcContractSchema.parse(
31
- await import(
32
- pathToFileURL(resolve(root, config.typegen.dir, generatedFiles.contract))
33
- .href
34
- ),
28
+ await import(pathToFileURL(file).href),
35
29
  );
36
30
  return contractVariables(contract).map(({ path, type }) => ({ path, type }));
37
31
  };
@@ -42,12 +36,11 @@ const releaseVariables = async (
42
36
  * virtual PLC whose inputs a plant simulation writes, as `tsplc run --simulate` builds it.
43
37
  */
44
38
  export const prepareRelease = async (
45
- file: string,
46
39
  loaded: LoadedConfig,
47
40
  flags: { simulate?: boolean; "frozen-lockfile"?: boolean },
48
41
  built: {
49
42
  /** What the build reads, with the plant PC's values and placeholders for its secrets. */
50
- env: CompileEnvironment;
43
+ env: Environment;
51
44
  /** Every plain value the release carries to its server. */
52
45
  values: ReleaseManifest["values"];
53
46
  /** The secrets the agent fills in. */
@@ -62,15 +55,14 @@ export const prepareRelease = async (
62
55
  }
63
56
  const command = flags.simulate ? "run" : "deploy";
64
57
  const serverCheck = config.server?.runtime
65
- ? checkServer(config, root)
58
+ ? startServerCheck(config, root)
66
59
  : undefined;
67
- // A failed PLC build reports its own diagnostics; the check's outcome is read only after a success.
68
- serverCheck?.catch(() => undefined);
69
- const emitted = await buildProject(file, {
60
+ const emitted = await buildTypeScriptProject(project.configFile, {
70
61
  emitOnly: true,
71
62
  forController: true,
72
63
  frozenLockfile: flags["frozen-lockfile"],
73
64
  ...buildConfigs(project, flags),
65
+ hostConfigDiagnostics: checkHostConfig(project.configFile),
74
66
  buildTarget: buildTargetFor(command),
75
67
  controller: controllerFor(config, command),
76
68
  });
@@ -81,7 +73,9 @@ export const prepareRelease = async (
81
73
  if (!settings) {
82
74
  throw new Error("Configure build settings for deployment.");
83
75
  }
84
- await generateProject(file, { loaded: project, targets: emitted.targets });
76
+ const { contract } = await generateProject(project, {
77
+ targets: emitted.targets,
78
+ });
85
79
 
86
80
  const portable = (path: string) => path.split(sep).join("/");
87
81
  const libraries = (settings.libraries ?? []).map((library) => ({
@@ -107,10 +101,10 @@ export const prepareRelease = async (
107
101
  }
108
102
  // The agent's debugger reads the program through its debug manifest, and explains it with `explain.json`.
109
103
  for (const name of [debugManifestFile, explainFileName]) {
110
- const path = resolve(settings.sourceDir, name);
111
- if (existsSync(path)) {
112
- files.push({ path: name, content: Bun.file(path) });
113
- }
104
+ files.push({
105
+ path: name,
106
+ content: Bun.file(resolve(settings.sourceDir, name)),
107
+ });
114
108
  }
115
109
  const serverScript =
116
110
  serverCheck && (await bundleServer(await serverCheck, config, root));
@@ -128,7 +122,7 @@ export const prepareRelease = async (
128
122
  const releaseConfig: {
129
123
  [Key in keyof Required<ReleaseConfig>]: ReleaseConfig[Key];
130
124
  } = {
131
- io: {},
125
+ io: settings.io,
132
126
  deviceTree: settings.deviceTree,
133
127
  controller: settings.controller,
134
128
  libraryReferences: settings.libraryReferences,
@@ -147,7 +141,7 @@ export const prepareRelease = async (
147
141
  };
148
142
  const manifest: ReleaseManifest = {
149
143
  config: releaseConfig,
150
- variables: await releaseVariables(config, root),
144
+ variables: await releaseVariables(contract),
151
145
  server: server?.path,
152
146
  debug: config.debug,
153
147
  serverTokenEnv: serverTokenEnv(config),