@typesol/cli 0.45.0 → 0.46.0
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.
- package/CHANGELOG.md +227 -0
- package/README.md +64 -17
- package/SECURITY.md +9 -7
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +9 -0
- package/dist/cli.js +33 -15
- package/dist/compiler/analyze.js +57 -6
- package/dist/compiler/diagnostics.d.ts +4 -3
- package/dist/compiler/diagnostics.js +10 -6
- package/dist/compiler/emit/base.d.ts +1 -1
- package/dist/compiler/emit/calls.js +18 -5
- package/dist/compiler/emit/cpi-core.d.ts +3 -1
- package/dist/compiler/emit/cpi-core.js +8 -4
- package/dist/compiler/emit/expressions.d.ts +14 -0
- package/dist/compiler/emit/expressions.js +52 -4
- package/dist/compiler/emit/shared.d.ts +8 -1
- package/dist/compiler/emit/shared.js +8 -1
- package/dist/compiler/emit/statements.d.ts +8 -3
- package/dist/compiler/emit/statements.js +68 -3
- package/dist/compiler/emit/storage.js +5 -1
- package/dist/compiler/hints.d.ts +7 -0
- package/dist/compiler/hints.js +58 -9
- package/dist/compiler/index.d.ts +5 -0
- package/dist/compiler/index.js +10 -1
- package/dist/compiler/model.js +3 -2
- package/dist/compiler/toolchain.d.ts +50 -2
- package/dist/compiler/toolchain.js +96 -11
- package/dist/node-version.d.ts +4 -0
- package/dist/node-version.js +12 -0
- package/dist/tools/doctor.d.ts +41 -0
- package/dist/tools/doctor.js +96 -14
- package/dist/workspace.d.ts +9 -2
- package/dist/workspace.js +13 -3
- package/docker/deps/0.46.0.lock.json +1206 -0
- package/docs/ai.md +2 -1
- package/docs/architecture.md +7 -3
- package/docs/audit.md +6 -6
- package/docs/case-study-phoenix.md +1 -1
- package/docs/case-study-raydium-amm.md +1 -1
- package/docs/case-study-spl-stake-pool.md +1 -1
- package/docs/cli.md +42 -4
- package/docs/errors.md +22 -8
- package/docs/glossary.md +210 -0
- package/docs/language-accounts.md +499 -0
- package/docs/language-basics.md +389 -0
- package/docs/language-builtins.md +411 -0
- package/docs/language-chain.md +198 -0
- package/docs/language-cpi.md +470 -0
- package/docs/language-errors-events.md +184 -0
- package/docs/language-native.md +493 -0
- package/docs/language-pdas.md +198 -0
- package/docs/language-statements.md +620 -0
- package/docs/language-types.md +601 -0
- package/docs/language.md +190 -467
- package/docs/mainnet.md +18 -3
- package/docs/migrate-from-anchor.md +7 -6
- package/docs/security-model.md +3 -3
- package/docs/stability.md +3 -1
- package/docs/test-your-program.md +52 -15
- package/docs/troubleshooting.md +222 -100
- package/lib/typesol.d.ts +67 -22
- package/llms-full.txt +4915 -634
- package/llms.txt +12 -1
- package/package.json +2 -2
- package/releases/0.45.0.json +732 -0
- package/ts-plugin/index.js +16 -5
|
@@ -7,6 +7,8 @@ import { cTokens, declaredNames, isGlobalName, parameterDecls, usedNames } from
|
|
|
7
7
|
/**
|
|
8
8
|
* Statements: variables, assignment, control flow and return.
|
|
9
9
|
*/
|
|
10
|
+
/** A proof or signature check whose result decides nothing: `how` says why (discarded, never read, an empty if). */
|
|
11
|
+
const VERIFIER_UNUSED = (checker, how) => `the result of ${checker} ${how}, so nothing is checked: it returns whether the ${checker === "merkleVerify" ? "proof" : "signature"} holds; write require(${checker}(…), error)`;
|
|
10
12
|
export class StatementEmitter extends CpiEmitter {
|
|
11
13
|
statement(stmt, topLevel = false) {
|
|
12
14
|
const outer = this.where;
|
|
@@ -29,6 +31,11 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
29
31
|
if (ts.isExpressionStatement(stmt))
|
|
30
32
|
return this.expressionStatement(stmt.expression, topLevel);
|
|
31
33
|
if (ts.isIfStatement(stmt)) {
|
|
34
|
+
// `if (merkleVerify(…)) {}` (or `{} else {}`): the result decides nothing.
|
|
35
|
+
const checker = this.verifierCall(stmt.expression, true);
|
|
36
|
+
const empty = (s) => ts.isEmptyStatement(s) || (ts.isBlock(s) && s.statements.length === 0);
|
|
37
|
+
if (checker && (!stmt.elseStatement || empty(stmt.elseStatement)) && empty(stmt.thenStatement))
|
|
38
|
+
this.fail(stmt, `${VERIFIER_UNUSED(checker, "is not acted on: the if around it has an empty body")}`);
|
|
32
39
|
const cond = this.expect(this.expr(stmt.expression, BOOL), BOOL, stmt.expression);
|
|
33
40
|
this.line(`if (${cond.c}) {`);
|
|
34
41
|
this.block(() => this.body(stmt.thenStatement));
|
|
@@ -181,6 +188,7 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
181
188
|
}
|
|
182
189
|
const subject = this.expr(stmt.expression);
|
|
183
190
|
const typed = subject.literal !== undefined ? this.literal(subject.literal, U64, stmt.expression) : subject;
|
|
191
|
+
this.refuseF64(stmt.expression, "switch", typed);
|
|
184
192
|
if (typed.type.kind !== "int" && typed.type.kind !== "enum")
|
|
185
193
|
this.fail(stmt.expression, "switch works on integers and enums");
|
|
186
194
|
// C cannot switch on 128-bit values or u256 limbs: pick the case's index with comparisons, then switch on that.
|
|
@@ -1468,6 +1476,8 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
1468
1476
|
if (!(list.flags & (ts.NodeFlags.Const | ts.NodeFlags.Let)))
|
|
1469
1477
|
this.fail(list, "use const or let, not var");
|
|
1470
1478
|
for (const d of list.declarations) {
|
|
1479
|
+
if (ts.isIdentifier(d.name) && d.initializer)
|
|
1480
|
+
this.refuseUnreadVerifier(d.name, d.initializer);
|
|
1471
1481
|
if (ts.isObjectBindingPattern(d.name) && d.initializer) {
|
|
1472
1482
|
this.destructure(list, d, d.name, d.initializer, topLevel);
|
|
1473
1483
|
continue;
|
|
@@ -1892,11 +1902,56 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
1892
1902
|
this.variable({ ...list, declarations: [decl] }, topLevel);
|
|
1893
1903
|
}
|
|
1894
1904
|
}
|
|
1905
|
+
/** merkleVerify, verifyEd25519 or verifySecp256k1 when `e` (in parentheses, or under `!` if `negated`) calls one. */
|
|
1906
|
+
verifierCall(e, negated = false) {
|
|
1907
|
+
while (ts.isParenthesizedExpression(e) || (negated && ts.isPrefixUnaryExpression(e) && e.operator === ts.SyntaxKind.ExclamationToken))
|
|
1908
|
+
e = ts.isParenthesizedExpression(e) ? e.expression : e.operand;
|
|
1909
|
+
if (!ts.isCallExpression(e))
|
|
1910
|
+
return undefined;
|
|
1911
|
+
const name = this.exportName(e.expression);
|
|
1912
|
+
return name === "merkleVerify" || name === "verifyEd25519" || name === "verifySecp256k1" ? name : undefined;
|
|
1913
|
+
}
|
|
1914
|
+
/**
|
|
1915
|
+
* `const ok = merkleVerify(…)` (or `ok = …`, the call in parentheses or under `as`) where the function that
|
|
1916
|
+
* declares `ok` never reads it: nothing is checked. A read anywhere in that function counts, in a closure inside it
|
|
1917
|
+
* and before the assignment too: this catches a result left unused, not every path.
|
|
1918
|
+
*/
|
|
1919
|
+
refuseUnreadVerifier(name, value) {
|
|
1920
|
+
while (ts.isParenthesizedExpression(value) || ts.isAsExpression(value))
|
|
1921
|
+
value = value.expression;
|
|
1922
|
+
const checker = this.verifierCall(value);
|
|
1923
|
+
if (!checker)
|
|
1924
|
+
return;
|
|
1925
|
+
const symbol = this.checker.getSymbolAtLocation(name);
|
|
1926
|
+
let fn = symbol?.valueDeclaration;
|
|
1927
|
+
while (fn && !ts.isFunctionLike(fn))
|
|
1928
|
+
fn = fn.parent;
|
|
1929
|
+
if (!symbol || !fn)
|
|
1930
|
+
return;
|
|
1931
|
+
let read = false;
|
|
1932
|
+
const visit = (n) => {
|
|
1933
|
+
if (read)
|
|
1934
|
+
return;
|
|
1935
|
+
if (ts.isIdentifier(n) && n !== name && this.checker.getSymbolAtLocation(n) === symbol) {
|
|
1936
|
+
const p = n.parent;
|
|
1937
|
+
const written = (ts.isVariableDeclaration(p) && p.name === n) || (ts.isBinaryExpression(p) && p.left === n && p.operatorToken.kind === ts.SyntaxKind.EqualsToken);
|
|
1938
|
+
if (!written)
|
|
1939
|
+
read = true;
|
|
1940
|
+
}
|
|
1941
|
+
ts.forEachChild(n, visit);
|
|
1942
|
+
};
|
|
1943
|
+
visit(fn);
|
|
1944
|
+
if (!read)
|
|
1945
|
+
this.fail(value, VERIFIER_UNUSED(checker, `is never read (${name.text} is not used)`));
|
|
1946
|
+
}
|
|
1895
1947
|
expressionStatement(e, topLevel) {
|
|
1896
1948
|
if (ts.isBinaryExpression(e)) {
|
|
1897
1949
|
const op = e.operatorToken.kind;
|
|
1898
|
-
if (op === ts.SyntaxKind.EqualsToken)
|
|
1950
|
+
if (op === ts.SyntaxKind.EqualsToken) {
|
|
1951
|
+
if (ts.isIdentifier(e.left))
|
|
1952
|
+
this.refuseUnreadVerifier(e.left, e.right);
|
|
1899
1953
|
return this.assign(e.left, e.right, undefined, topLevel);
|
|
1954
|
+
}
|
|
1900
1955
|
const compound = {
|
|
1901
1956
|
[ts.SyntaxKind.PlusEqualsToken]: ts.SyntaxKind.PlusToken,
|
|
1902
1957
|
[ts.SyntaxKind.MinusEqualsToken]: ts.SyntaxKind.MinusToken,
|
|
@@ -1933,6 +1988,7 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
1933
1988
|
if ((ts.isPostfixUnaryExpression(e) || ts.isPrefixUnaryExpression(e)) &&
|
|
1934
1989
|
(e.operator === ts.SyntaxKind.PlusPlusToken || e.operator === ts.SyntaxKind.MinusMinusToken)) {
|
|
1935
1990
|
const place = this.place(e.operand);
|
|
1991
|
+
this.refuseF64(e, e.operator === ts.SyntaxKind.PlusPlusToken ? "++" : "--", { c: place.read, type: place.type });
|
|
1936
1992
|
if (place.type.kind !== "int")
|
|
1937
1993
|
this.fail(e, "++ and -- need an integer");
|
|
1938
1994
|
const one = this.literal(1n, place.type, e);
|
|
@@ -1940,6 +1996,10 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
1940
1996
|
this.line(place.write(this.arith(op, { c: place.read, type: place.type }, one, e).c) + ";");
|
|
1941
1997
|
return;
|
|
1942
1998
|
}
|
|
1999
|
+
// These return whether a proof or signature holds and fail nothing by themselves: a bare call checks nothing.
|
|
2000
|
+
const checker = this.verifierCall(e);
|
|
2001
|
+
if (checker)
|
|
2002
|
+
this.fail(e, VERIFIER_UNUSED(checker, "is discarded"));
|
|
1943
2003
|
if (ts.isCallExpression(e)) {
|
|
1944
2004
|
this.call(e, topLevel);
|
|
1945
2005
|
return;
|
|
@@ -2002,9 +2062,14 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
2002
2062
|
if (rhs.type.kind !== "int")
|
|
2003
2063
|
this.fail(valueNode, "shift amounts must be integers");
|
|
2004
2064
|
}
|
|
2005
|
-
else
|
|
2006
|
-
rhs = this.
|
|
2065
|
+
else {
|
|
2066
|
+
rhs = this.expr(valueNode, place.type);
|
|
2067
|
+
// `n += x` with an f64 on either side is the operator on an f64, refused as such (not as a conversion).
|
|
2068
|
+
this.refuseF64(target, `${ts.tokenToString(op)}=`, current, rhs);
|
|
2069
|
+
rhs = this.expect(rhs, place.type, valueNode);
|
|
2070
|
+
}
|
|
2007
2071
|
current = this.settle(current, mark);
|
|
2072
|
+
this.refuseF64(target, `${ts.tokenToString(op)}=`, current, rhs);
|
|
2008
2073
|
this.line(place.write(this.arith(op, current, rhs, target).c) + ";");
|
|
2009
2074
|
return;
|
|
2010
2075
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
import { heapVecOf, resolveScalarType, structNameOf, typesolName } from "../analyze.js";
|
|
3
3
|
import { INT_INFO, isScalar, isWritable, sameType, scalarSize, typeName } from "../model.js";
|
|
4
|
-
import { U64, BOOL, PUBKEY, STRING, optC, fieldC, fixedVecC, storageDecl, vecCount, vecPlace, emptySizeC } from "./shared.js";
|
|
4
|
+
import { U64, BOOL, PUBKEY, STRING, F64_TO_INT, optC, fieldC, fixedVecC, storageDecl, vecCount, vecPlace, emptySizeC } from "./shared.js";
|
|
5
5
|
import { AliasEmitter } from "./alias.js";
|
|
6
6
|
/**
|
|
7
7
|
* Places in account data and locals: references, reads and writes, Vec/array indexing, aliasing rules and
|
|
@@ -470,6 +470,8 @@ export class StorageEmitter extends AliasEmitter {
|
|
|
470
470
|
const idx = v.literal !== undefined ? this.literal(v.literal, U64, n) : v;
|
|
471
471
|
if (idx.type.kind !== "int")
|
|
472
472
|
this.fail(n, "array index must be an integer");
|
|
473
|
+
if (idx.type.f64)
|
|
474
|
+
this.fail(n, F64_TO_INT("an index"));
|
|
473
475
|
const t = this.indexTemp(idx);
|
|
474
476
|
return t;
|
|
475
477
|
}
|
|
@@ -500,6 +502,8 @@ export class StorageEmitter extends AliasEmitter {
|
|
|
500
502
|
const idx = v.literal !== undefined ? this.literal(v.literal, U64, n) : v;
|
|
501
503
|
if (idx.type.kind !== "int")
|
|
502
504
|
this.fail(n, "array index must be an integer");
|
|
505
|
+
if (idx.type.f64)
|
|
506
|
+
this.fail(n, F64_TO_INT("an index"));
|
|
503
507
|
const t = this.indexTemp(idx);
|
|
504
508
|
this.line(`if (${t} >= ${typeof len === "number" ? `${len}ULL` : len}) TS_FAIL(TS_ERR_INDEX, "TypeSOL: array index out of bounds");`);
|
|
505
509
|
return t;
|
package/dist/compiler/hints.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
+
/** Date.now() and new Date() have no meaning on chain; the cluster's clock does. */
|
|
3
|
+
export declare const CLOCK_HINT = "the time on chain is the cluster's clock: now() from \"typesol\" gives its Unix timestamp in seconds (an i64)";
|
|
4
|
+
export declare const ASYNC_HINT = "async and generators are not supported: an instruction runs start to finish within one call; remove async (or the *) and return void or a value, not a Promise";
|
|
5
|
+
/** The message to show for a TypeScript diagnostic: TypeScript's own, except where its advice points the wrong way. */
|
|
6
|
+
export declare function messageForTypeScript(d: ts.Diagnostic, message: string): string;
|
|
7
|
+
/** A hint for a struct field whose type cannot be account data. */
|
|
8
|
+
export declare function fieldTypeHint(node: ts.Node): string | undefined;
|
|
2
9
|
/** A sentence to add to a TypeScript diagnostic, or undefined when TypeScript's own message says enough. */
|
|
3
10
|
export declare function hintForTypeScript(d: ts.Diagnostic, message: string, libPath: string): string | undefined;
|
|
4
11
|
/** "ClassDeclaration is not supported …" in words a TypeScript developer recognizes. */
|
package/dist/compiler/hints.js
CHANGED
|
@@ -3,13 +3,16 @@
|
|
|
3
3
|
import { readFileSync } from "node:fs";
|
|
4
4
|
import ts from "typescript";
|
|
5
5
|
let libNames;
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* The names lib/typesol.d.ts exports: values (functions, constants, enums) and types (types, interfaces), so
|
|
8
|
+
* "Cannot find name 'Signer'" can say where Signer comes from. u64 and the other integer casts are both.
|
|
9
|
+
*/
|
|
7
10
|
function typesolExports(libPath) {
|
|
8
11
|
if (!libNames) {
|
|
9
|
-
libNames = new Set();
|
|
12
|
+
libNames = { values: new Set(), types: new Set() };
|
|
10
13
|
try {
|
|
11
|
-
for (const m of readFileSync(libPath, "utf8").matchAll(/^export declare (
|
|
12
|
-
libNames.add(m[
|
|
14
|
+
for (const m of readFileSync(libPath, "utf8").matchAll(/^export (?:declare )?(function|const|enum|type|interface|class) (\w+)/gm))
|
|
15
|
+
(m[1] === "type" || m[1] === "interface" ? libNames.types : libNames.values).add(m[2]);
|
|
13
16
|
}
|
|
14
17
|
catch {
|
|
15
18
|
// Without the library file there is nothing to suggest.
|
|
@@ -19,6 +22,38 @@ function typesolExports(libPath) {
|
|
|
19
22
|
}
|
|
20
23
|
const MINMAX_HINT = 'Math works on numbers; use min(a, b) and max(a, b) from "typesol", which take bigints';
|
|
21
24
|
const BIGINT_HINT = "integers (u64, u32, i64 …) are bigint in TypeSOL: write 1n, not 1, and take u64, not number";
|
|
25
|
+
/** Date.now() and new Date() have no meaning on chain; the cluster's clock does. */
|
|
26
|
+
export const CLOCK_HINT = 'the time on chain is the cluster\'s clock: now() from "typesol" gives its Unix timestamp in seconds (an i64)';
|
|
27
|
+
export const ASYNC_HINT = "async and generators are not supported: an instruction runs start to finish within one call; remove async (or the *) and return void or a value, not a Promise";
|
|
28
|
+
/**
|
|
29
|
+
* Globals a program does not have, which TypeScript reports with advice to add a library or @types/node: the
|
|
30
|
+
* compiler's message in place of TypeScript's, and what to write instead (undefined: nothing to suggest).
|
|
31
|
+
*/
|
|
32
|
+
const MISSING_GLOBALS = {
|
|
33
|
+
console: 'a program writes logs with log("…") from "typesol"',
|
|
34
|
+
process: 'a program has no process: stop with require(condition, error) from "typesol", which fails the transaction',
|
|
35
|
+
Buffer: "a program has no Buffer: account data is typed fields (FixedArray<u8, N> for raw bytes)",
|
|
36
|
+
setTimeout: "an instruction runs start to finish within one call: there is nothing to wait for",
|
|
37
|
+
setInterval: "an instruction runs start to finish within one call: there is nothing to wait for",
|
|
38
|
+
fetch: "a program cannot reach the network: what it reads comes in as accounts",
|
|
39
|
+
window: undefined,
|
|
40
|
+
document: undefined,
|
|
41
|
+
};
|
|
42
|
+
/** The message to show for a TypeScript diagnostic: TypeScript's own, except where its advice points the wrong way. */
|
|
43
|
+
export function messageForTypeScript(d, message) {
|
|
44
|
+
const name = /^Cannot find name '([^']+)'/.exec(message)?.[1];
|
|
45
|
+
if (name !== undefined && Object.hasOwn(MISSING_GLOBALS, name))
|
|
46
|
+
return `'${name}' does not exist in a Solana program`;
|
|
47
|
+
return message;
|
|
48
|
+
}
|
|
49
|
+
/** A hint for a struct field whose type cannot be account data. */
|
|
50
|
+
export function fieldTypeHint(node) {
|
|
51
|
+
if (node.kind === ts.SyntaxKind.NumberKeyword)
|
|
52
|
+
return 'integers are bigint types from "typesol": u64 for amounts and counts, or u8, u16, u32, i64 … (write 1n, not 1)';
|
|
53
|
+
if (ts.isTypeReferenceNode(node) && ts.isIdentifier(node.typeName) && node.typeName.text === "Date")
|
|
54
|
+
return 'store a time as an i64 of Unix seconds; now() from "typesol" gives the cluster\'s clock';
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
22
57
|
/** A sentence to add to a TypeScript diagnostic, or undefined when TypeScript's own message says enough. */
|
|
23
58
|
export function hintForTypeScript(d, message, libPath) {
|
|
24
59
|
// The whole source line, so a Math.max(…) is seen even when TypeScript marks only the assignment around it.
|
|
@@ -33,12 +68,22 @@ export function hintForTypeScript(d, message, libPath) {
|
|
|
33
68
|
// (2582, 2593) or the dom library (2584).
|
|
34
69
|
const name = /^Cannot find name '([^']+)'/.exec(message)?.[1];
|
|
35
70
|
if (name !== undefined) {
|
|
36
|
-
if (name
|
|
37
|
-
return
|
|
71
|
+
if (Object.hasOwn(MISSING_GLOBALS, name))
|
|
72
|
+
return MISSING_GLOBALS[name];
|
|
38
73
|
if (name === "Math")
|
|
39
74
|
return MINMAX_HINT;
|
|
40
|
-
|
|
75
|
+
const lib = typesolExports(libPath);
|
|
76
|
+
if (lib.values.has(name))
|
|
41
77
|
return `import { ${name} } from "typesol"`;
|
|
78
|
+
if (lib.types.has(name))
|
|
79
|
+
return `import { type ${name} } from "typesol"`;
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
// 'u64' cannot be used as a value because it was imported using 'import type' (1361; 1362 for 'export type').
|
|
83
|
+
const typeOnly = /^'([^']+)' cannot be used as a value because it was (?:imported|exported) using '(?:import|export) type'/.exec(message)?.[1];
|
|
84
|
+
if (typeOnly !== undefined) {
|
|
85
|
+
if (typesolExports(libPath).values.has(typeOnly))
|
|
86
|
+
return `${typeOnly} is a type and a function: import it without "type" to call it (import { ${typeOnly} } from "typesol"; "type ${typeOnly}" brings in only the type)`;
|
|
42
87
|
return undefined;
|
|
43
88
|
}
|
|
44
89
|
switch (d.code) {
|
|
@@ -46,6 +91,8 @@ export function hintForTypeScript(d, message, libPath) {
|
|
|
46
91
|
case 2365: // Operator '+=' cannot be applied to types 'bigint' and 'number'.
|
|
47
92
|
case 2322: // Type 'number' is not assignable to type 'bigint'.
|
|
48
93
|
case 2345: // Argument of type 'bigint' is not assignable to parameter of type 'number'.
|
|
94
|
+
if (/\bDate\.now\(|\bnew Date\b/.test(line))
|
|
95
|
+
return CLOCK_HINT;
|
|
49
96
|
if (/\bMath\.\w+\(/.test(line))
|
|
50
97
|
return MINMAX_HINT;
|
|
51
98
|
if (/\bbigint\b/.test(message) && (/\bnumber\b/.test(message) || /'-?\d+'/.test(message)))
|
|
@@ -70,14 +117,16 @@ export function hintForTypeScript(d, message, libPath) {
|
|
|
70
117
|
return undefined;
|
|
71
118
|
}
|
|
72
119
|
}
|
|
120
|
+
/** A function written where the compiler takes a value (in parentheses, say): where functions are allowed instead. */
|
|
121
|
+
const FUNCTION_VALUE = "a function cannot be used here: bind it with const (const f = (…) => …, without parentheses around it) or write it in the call that takes it (xs.map((x) => …), a helper's function parameter)";
|
|
73
122
|
/** Plain words for the syntax kinds the compiler refuses, and what to use instead. */
|
|
74
123
|
const SYNTAX = {
|
|
75
124
|
[ts.SyntaxKind.ClassDeclaration]: "classes are not supported: describe account data with an interface and logic with functions",
|
|
76
125
|
[ts.SyntaxKind.ElementAccessExpression]: "indexing with [ ] is not supported: use .get(i) and .set(i, value) (i is a bigint)",
|
|
77
|
-
[ts.SyntaxKind.TemplateExpression]: "template strings are not supported: log the parts one by one, or use an error enum member for a message",
|
|
78
126
|
[ts.SyntaxKind.SpreadElement]: "spread (...) is not supported: copy the fields one by one",
|
|
79
127
|
[ts.SyntaxKind.ArrayLiteralExpression]: "array literals are only allowed inline as seeds or CPI lists; declare local arrays with fixedArray<T, N>(fill)",
|
|
80
|
-
[ts.SyntaxKind.FunctionExpression]:
|
|
128
|
+
[ts.SyntaxKind.FunctionExpression]: FUNCTION_VALUE,
|
|
129
|
+
[ts.SyntaxKind.ArrowFunction]: FUNCTION_VALUE,
|
|
81
130
|
[ts.SyntaxKind.AwaitExpression]: "await is not supported: an instruction runs start to finish within one call",
|
|
82
131
|
[ts.SyntaxKind.TypeOfExpression]: "typeof is not supported at run time: the types are known when the program compiles",
|
|
83
132
|
[ts.SyntaxKind.DeleteExpression]: "delete is not supported: account fields always exist; set the value instead",
|
package/dist/compiler/index.d.ts
CHANGED
|
@@ -8,6 +8,11 @@ export type { ProgramModel } from "./model.js";
|
|
|
8
8
|
export { SBPF_VERSIONS, TESTED_TOOLCHAIN, type Sbpf } from "./toolchain.js";
|
|
9
9
|
/** The hint the CLI adds under a TypeScript error (what to write instead in a TypeSOL program), for the editor plugin. */
|
|
10
10
|
export declare function typescriptHint(d: ts.Diagnostic): string | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* The message the CLI shows in place of a TypeScript error's, where TypeScript's advice points the wrong way (add the
|
|
13
|
+
* dom library, install @types/node, for a global a program does not have); undefined to keep TypeScript's.
|
|
14
|
+
*/
|
|
15
|
+
export declare function typescriptMessage(d: ts.Diagnostic): string | undefined;
|
|
11
16
|
export interface Generated {
|
|
12
17
|
model: ProgramModel;
|
|
13
18
|
c: string;
|
package/dist/compiler/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import ts from "typescript";
|
|
|
4
4
|
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { analyze, LIB_PATH } from "./analyze.js";
|
|
7
|
-
import { hintForTypeScript } from "./hints.js";
|
|
7
|
+
import { hintForTypeScript, messageForTypeScript } from "./hints.js";
|
|
8
8
|
import { emitProgram } from "./emit/index.js";
|
|
9
9
|
import { buildIdl } from "./idl.js";
|
|
10
10
|
import { buildSharedObject, checkToolchain, shortClang, TESTED_TOOLCHAIN } from "./toolchain.js";
|
|
@@ -15,6 +15,15 @@ export { SBPF_VERSIONS, TESTED_TOOLCHAIN } from "./toolchain.js";
|
|
|
15
15
|
export function typescriptHint(d) {
|
|
16
16
|
return hintForTypeScript(d, ts.flattenDiagnosticMessageText(d.messageText, "\n"), LIB_PATH);
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* The message the CLI shows in place of a TypeScript error's, where TypeScript's advice points the wrong way (add the
|
|
20
|
+
* dom library, install @types/node, for a global a program does not have); undefined to keep TypeScript's.
|
|
21
|
+
*/
|
|
22
|
+
export function typescriptMessage(d) {
|
|
23
|
+
const flat = ts.flattenDiagnosticMessageText(d.messageText, "\n");
|
|
24
|
+
const shown = messageForTypeScript(d, flat);
|
|
25
|
+
return shown === flat ? undefined : shown;
|
|
26
|
+
}
|
|
18
27
|
/** Typecheck, enforce the TypeSOL subset, and generate C. Throws CompileError with every diagnostic. */
|
|
19
28
|
export function generate(entry, opts = {}) {
|
|
20
29
|
const analysis = analyze(entry, opts.files);
|
package/dist/compiler/model.js
CHANGED
|
@@ -37,7 +37,7 @@ export function isWritable(k) {
|
|
|
37
37
|
export function typeName(t) {
|
|
38
38
|
switch (t.kind) {
|
|
39
39
|
case "int":
|
|
40
|
-
return t.name;
|
|
40
|
+
return t.f64 ? "f64" : t.name;
|
|
41
41
|
case "bool":
|
|
42
42
|
return "boolean";
|
|
43
43
|
case "pubkey":
|
|
@@ -97,7 +97,8 @@ export function sameType(a, b) {
|
|
|
97
97
|
return false;
|
|
98
98
|
switch (a.kind) {
|
|
99
99
|
case "int":
|
|
100
|
-
|
|
100
|
+
// An f64 is held as a u64 of its bits, but it is not an integer: neither converts to the other implicitly.
|
|
101
|
+
return a.name === b.name && !a.f64 === !b.f64;
|
|
101
102
|
case "array": {
|
|
102
103
|
const o = b;
|
|
103
104
|
return a.len === o.len && sameType(a.elem, o.elem);
|
|
@@ -18,8 +18,23 @@ export interface Toolchain {
|
|
|
18
18
|
}
|
|
19
19
|
/** The runtime's compiler builtins, compiled with each program and linked in place of compiler-builtins. */
|
|
20
20
|
export declare const BUILTINS_SOURCE: string;
|
|
21
|
+
/** What is missing when there is no toolchain to build with, so `doctor` and `init` can say which commands to run. */
|
|
22
|
+
export declare class ToolchainMissing extends Error {
|
|
23
|
+
/** "agave": no Agave CLI (no SDK); "platform-tools": the CLI without its platform-tools; "sbpf-libraries": platform-tools without this SBPF version. */
|
|
24
|
+
readonly kind: "agave" | "platform-tools" | "sbpf-libraries";
|
|
25
|
+
/** What is missing, in one clause. */
|
|
26
|
+
readonly summary: string;
|
|
27
|
+
constructor(
|
|
28
|
+
/** "agave": no Agave CLI (no SDK); "platform-tools": the CLI without its platform-tools; "sbpf-libraries": platform-tools without this SBPF version. */
|
|
29
|
+
kind: "agave" | "platform-tools" | "sbpf-libraries",
|
|
30
|
+
/** What is missing, in one clause. */
|
|
31
|
+
summary: string, fix: string);
|
|
32
|
+
}
|
|
21
33
|
/** Locate the Solana C SDK that ships with the Agave CLI's platform-tools. */
|
|
22
|
-
export declare function findToolchain(sbpf?: Sbpf
|
|
34
|
+
export declare function findToolchain(sbpf?: Sbpf, host?: {
|
|
35
|
+
platform: string;
|
|
36
|
+
arch: string;
|
|
37
|
+
}): Toolchain;
|
|
23
38
|
/** Compile generated C to an SBF shared object (same flags as the SDK's sbf.mk for the chosen SBPF version). */
|
|
24
39
|
/** The SBF stack frame of one function. */
|
|
25
40
|
export declare const STACK_FRAME = 4096;
|
|
@@ -53,6 +68,39 @@ export declare const TESTED_TOOLCHAIN: {
|
|
|
53
68
|
};
|
|
54
69
|
/** The commands that install the tested toolchain (the Agave CLI, then its platform-tools). */
|
|
55
70
|
export declare const INSTALL_TESTED_TOOLCHAIN: readonly ["sh -c \"$(curl -sSfL https://release.anza.xyz/v3.1.10/install)\"", "cargo-build-sbf --install-only --force-tools-install --tools-version v1.52"];
|
|
71
|
+
/**
|
|
72
|
+
* The Agave installer adds its bin directory to PATH in ~/.profile only, which the shell that ran it does not read:
|
|
73
|
+
* without this line the next command is "cargo-build-sbf: command not found".
|
|
74
|
+
*/
|
|
75
|
+
export declare const AGAVE_PATH = "export PATH=\"$HOME/.local/share/solana/install/active_release/bin:$PATH\"";
|
|
76
|
+
export declare const linuxArm64: (host?: {
|
|
77
|
+
platform: string;
|
|
78
|
+
arch: string;
|
|
79
|
+
}) => boolean;
|
|
80
|
+
export declare const LINUX_ARM64_INSTALL: readonly string[];
|
|
81
|
+
/**
|
|
82
|
+
* What to run to get the tested toolchain on this host: from nothing ("agave"), or when the Agave CLI is there and
|
|
83
|
+
* its platform-tools are not. `cargo-build-sbf --version` downloads nothing and `cargo-build-sbf --force-tools-install`
|
|
84
|
+
* alone needs a Cargo project; only `--install-only` installs the platform-tools by themselves.
|
|
85
|
+
*/
|
|
86
|
+
export declare function installCommands(from: "agave" | "platform-tools", host?: {
|
|
87
|
+
platform: string;
|
|
88
|
+
arch: string;
|
|
89
|
+
}): string[];
|
|
90
|
+
/**
|
|
91
|
+
* The note printed on its own line above an install command, when what it does is not obvious; undefined for the
|
|
92
|
+
* others. A note is a `#` line holding no character a shell treats specially (no parentheses, `;`, `$`, quotes,
|
|
93
|
+
* `<`, `>`, `|`, `&`, `!`, `*`, `?`, brackets or braces), so a pasted block runs the same: bash and fish skip it as
|
|
94
|
+
* a comment, and zsh, whose interactive shells do not read comments unless `interactive_comments` is set (the macOS
|
|
95
|
+
* default), answers "command not found: #" and runs nothing from that line. A command line never carries a trailing
|
|
96
|
+
* comment: zsh would hand it to the command as arguments (test/cli/doctor.test.ts pastes the blocks into each shell).
|
|
97
|
+
*/
|
|
98
|
+
export declare function installNote(command: string): string | undefined;
|
|
99
|
+
/** installCommands with each command's note on the line above it: the lines `init`, `doctor` and the build print. */
|
|
100
|
+
export declare function installLines(from: "agave" | "platform-tools", host?: {
|
|
101
|
+
platform: string;
|
|
102
|
+
arch: string;
|
|
103
|
+
}): string[];
|
|
56
104
|
/** The clang version string (the first line of `clang --version`), for build manifests. */
|
|
57
105
|
export declare function toolchainVersion(tc?: Toolchain): string;
|
|
58
106
|
/**
|
|
@@ -72,7 +120,7 @@ export interface ToolchainInfo {
|
|
|
72
120
|
/** The active toolchain's versions, and whether it is the tested one (the clang is what compiles, so it decides). */
|
|
73
121
|
export declare function toolchainInfo(tc?: Toolchain): ToolchainInfo;
|
|
74
122
|
/** What to say about a toolchain that is not the tested one; undefined for the tested one. */
|
|
75
|
-
export declare function untestedToolchainMessage(info: ToolchainInfo): string | undefined;
|
|
123
|
+
export declare function untestedToolchainMessage(info: ToolchainInfo, strict?: boolean): string | undefined;
|
|
76
124
|
/** `--toolchain-strict`: refuse an untested toolchain for the rest of the process, as TYPESOL_TOOLCHAIN=strict does. */
|
|
77
125
|
export declare function setToolchainStrict(on?: boolean): void;
|
|
78
126
|
/** Whether an untested toolchain is an error: `--toolchain-strict` or TYPESOL_TOOLCHAIN=strict ("warn", the default). */
|
|
@@ -9,8 +9,23 @@ const EXE = (name) => (process.platform === "win32" ? `${name}.exe` : name);
|
|
|
9
9
|
export const SBPF_VERSIONS = ["v0", "v1", "v2"];
|
|
10
10
|
/** The runtime's compiler builtins, compiled with each program and linked in place of compiler-builtins. */
|
|
11
11
|
export const BUILTINS_SOURCE = path.join(RUNTIME_DIR, "typesol_builtins.c");
|
|
12
|
+
/** What is missing when there is no toolchain to build with, so `doctor` and `init` can say which commands to run. */
|
|
13
|
+
export class ToolchainMissing extends Error {
|
|
14
|
+
kind;
|
|
15
|
+
summary;
|
|
16
|
+
constructor(
|
|
17
|
+
/** "agave": no Agave CLI (no SDK); "platform-tools": the CLI without its platform-tools; "sbpf-libraries": platform-tools without this SBPF version. */
|
|
18
|
+
kind,
|
|
19
|
+
/** What is missing, in one clause. */
|
|
20
|
+
summary, fix) {
|
|
21
|
+
super(`${summary}. ${fix}`);
|
|
22
|
+
this.kind = kind;
|
|
23
|
+
this.summary = summary;
|
|
24
|
+
this.name = "ToolchainMissing";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
12
27
|
/** Locate the Solana C SDK that ships with the Agave CLI's platform-tools. */
|
|
13
|
-
export function findToolchain(sbpf = "v0") {
|
|
28
|
+
export function findToolchain(sbpf = "v0", host = process) {
|
|
14
29
|
// Where the Agave installer puts it, or next to `cargo-build-sbf` on PATH (custom --data-dir, Homebrew, Docker…).
|
|
15
30
|
const onPath = (process.env.PATH ?? "")
|
|
16
31
|
.split(path.delimiter)
|
|
@@ -28,11 +43,17 @@ export function findToolchain(sbpf = "v0") {
|
|
|
28
43
|
].filter((p) => !!p);
|
|
29
44
|
const sdk = candidates.find((p) => existsSync(path.join(p, "c/sbf.mk")));
|
|
30
45
|
if (!sdk)
|
|
31
|
-
throw
|
|
46
|
+
throw linuxArm64(host)
|
|
47
|
+
? new ToolchainMissing("agave", "no Solana SBF SDK: TYPESOL_SBF_SDK is not set, and there is no Agave CLI for Linux arm64", `Install the platform-tools' linux-aarch64 package and the SDK's C files:${block(installLines("agave", host))}`)
|
|
48
|
+
: new ToolchainMissing("agave", "the Agave CLI is not installed (no Solana SBF SDK under ~/.local/share/solana/install/active_release or next to cargo-build-sbf on PATH)", `Set TYPESOL_SBF_SDK to a platform-tools-sdk/sbf directory, or install the Agave CLI and its platform-tools:${block(installLines("agave", host))}`);
|
|
32
49
|
const llvm = path.join(sdk, "dependencies/platform-tools/llvm");
|
|
33
50
|
const clang = path.join(llvm, "bin", EXE("clang"));
|
|
34
51
|
if (!existsSync(clang))
|
|
35
|
-
throw new
|
|
52
|
+
throw new ToolchainMissing("platform-tools",
|
|
53
|
+
// On Linux arm64 there is no Agave CLI: the SDK found is the one TYPESOL_SBF_SDK names.
|
|
54
|
+
linuxArm64(host)
|
|
55
|
+
? `the SBF SDK at ${sdk} has no platform-tools (nothing under ${sdk}/dependencies)`
|
|
56
|
+
: `the Agave CLI is installed, but not its platform-tools (nothing under ${sdk}/dependencies)`, `Install them once:${block(installLines("platform-tools", host))}`);
|
|
36
57
|
// Newest clang resource directory (numeric order: "9" < "19").
|
|
37
58
|
const clangInc = readdirSync(path.join(llvm, "lib/clang"))
|
|
38
59
|
.sort((a, b) => Number.parseFloat(a) - Number.parseFloat(b))
|
|
@@ -41,7 +62,7 @@ export function findToolchain(sbpf = "v0") {
|
|
|
41
62
|
const rustlib = path.join(sdk, `dependencies/platform-tools/rust/lib/rustlib/${target}-solana-solana/lib`);
|
|
42
63
|
const builtins = existsSync(rustlib) ? readdirSync(rustlib).find((f) => f.startsWith("libcompiler_builtins-") && f.endsWith(".rlib")) : undefined;
|
|
43
64
|
if (!builtins)
|
|
44
|
-
throw new
|
|
65
|
+
throw new ToolchainMissing("sbpf-libraries", `these platform-tools have no SBPF ${sbpf} libraries (${rustlib})`, `Build for another --sbpf, or install the tested ones:${block(installLines("platform-tools", host))}`);
|
|
45
66
|
return {
|
|
46
67
|
sdk,
|
|
47
68
|
clang,
|
|
@@ -154,10 +175,11 @@ export function linkSharedObject(objects, outSo, tc, extra = []) {
|
|
|
154
175
|
throw new Error((info && !info.tested
|
|
155
176
|
? `this toolchain is not the tested one (${shortClang(info.clang)}${info.platformTools ? `, platform-tools ${info.platformTools}` : ""}; ` +
|
|
156
177
|
`TESTED_TOOLCHAIN is ${shortClang(t.clang)}, platform-tools ${t.platformTools}), and its clang may call compiler builtins the tested one ` +
|
|
157
|
-
`does not, which runtime/typesol_builtins.c does not define: build with the tested toolchain (
|
|
178
|
+
`does not, which runtime/typesol_builtins.c does not define: build with the tested toolchain (the command is below). `
|
|
158
179
|
: "") +
|
|
159
180
|
`the program calls ${missing.join(", ")}, which neither the runtime nor libc defines and the SBF loader cannot resolve as a syscall ` +
|
|
160
|
-
`(with the tested toolchain, a compiler builtin missing from runtime/typesol_builtins.c is a TypeSOL bug: please report it with the program)`
|
|
181
|
+
`(with the tested toolchain, a compiler builtin missing from runtime/typesol_builtins.c is a TypeSOL bug: please report it with the program)` +
|
|
182
|
+
(info && !info.tested ? `. The tested toolchain:${block([INSTALL_TESTED_TOOLCHAIN[1]])}` : ""));
|
|
161
183
|
}
|
|
162
184
|
}
|
|
163
185
|
/**
|
|
@@ -252,6 +274,68 @@ export const INSTALL_TESTED_TOOLCHAIN = [
|
|
|
252
274
|
`sh -c "$(curl -sSfL https://release.anza.xyz/v${TESTED_TOOLCHAIN.agave}/install)"`,
|
|
253
275
|
`cargo-build-sbf --install-only --force-tools-install --tools-version ${TESTED_TOOLCHAIN.platformTools}`,
|
|
254
276
|
];
|
|
277
|
+
/**
|
|
278
|
+
* The Agave installer adds its bin directory to PATH in ~/.profile only, which the shell that ran it does not read:
|
|
279
|
+
* without this line the next command is "cargo-build-sbf: command not found".
|
|
280
|
+
*/
|
|
281
|
+
export const AGAVE_PATH = 'export PATH="$HOME/.local/share/solana/install/active_release/bin:$PATH"';
|
|
282
|
+
/**
|
|
283
|
+
* Linux arm64: Anza publishes no Agave CLI for it (its installer fails with a 404), but platform-tools has a
|
|
284
|
+
* linux-aarch64 package. The C SDK (headers and linker script, text) comes from the x86_64 Agave release, as in the
|
|
285
|
+
* --docker image, and TYPESOL_SBF_SDK points the compiler at the result. Building and LiteSVM tests work; deploying
|
|
286
|
+
* needs a Solana CLI from another host.
|
|
287
|
+
*/
|
|
288
|
+
const ARM64_SDK = "$HOME/.local/share/typesol-sdk";
|
|
289
|
+
/** A path under ARM64_SDK as a shell word: quoted, as AGAVE_PATH quotes $HOME, so a HOME with a space works. */
|
|
290
|
+
const arm64Path = (rest) => `"${ARM64_SDK}${rest}"`;
|
|
291
|
+
export const linuxArm64 = (host = process) => host.platform === "linux" && host.arch === "arm64";
|
|
292
|
+
export const LINUX_ARM64_INSTALL = [
|
|
293
|
+
`mkdir -p ${arm64Path("/sbf/dependencies/platform-tools")}`,
|
|
294
|
+
`curl -sSfL https://github.com/anza-xyz/agave/releases/download/v${TESTED_TOOLCHAIN.agave}/solana-release-x86_64-unknown-linux-gnu.tar.bz2 | tar -xj -C ${arm64Path("")} --strip-components=3 solana-release/bin/platform-tools-sdk/sbf/c`,
|
|
295
|
+
`curl -sSfL https://github.com/anza-xyz/platform-tools/releases/download/${TESTED_TOOLCHAIN.platformTools}/platform-tools-linux-aarch64.tar.bz2 | tar -xj -C ${arm64Path("/sbf/dependencies/platform-tools")}`,
|
|
296
|
+
`touch ${arm64Path(`/sbf/dependencies/platform-tools-${TESTED_TOOLCHAIN.platformTools}.md`)}`,
|
|
297
|
+
`export TYPESOL_SBF_SDK=${arm64Path("/sbf")}`,
|
|
298
|
+
];
|
|
299
|
+
/**
|
|
300
|
+
* What to run to get the tested toolchain on this host: from nothing ("agave"), or when the Agave CLI is there and
|
|
301
|
+
* its platform-tools are not. `cargo-build-sbf --version` downloads nothing and `cargo-build-sbf --force-tools-install`
|
|
302
|
+
* alone needs a Cargo project; only `--install-only` installs the platform-tools by themselves.
|
|
303
|
+
*/
|
|
304
|
+
export function installCommands(from, host = process) {
|
|
305
|
+
if (linuxArm64(host))
|
|
306
|
+
return [...LINUX_ARM64_INSTALL];
|
|
307
|
+
return from === "agave" ? [INSTALL_TESTED_TOOLCHAIN[0], AGAVE_PATH, INSTALL_TESTED_TOOLCHAIN[1]] : [INSTALL_TESTED_TOOLCHAIN[1]];
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* The note printed on its own line above an install command, when what it does is not obvious; undefined for the
|
|
311
|
+
* others. A note is a `#` line holding no character a shell treats specially (no parentheses, `;`, `$`, quotes,
|
|
312
|
+
* `<`, `>`, `|`, `&`, `!`, `*`, `?`, brackets or braces), so a pasted block runs the same: bash and fish skip it as
|
|
313
|
+
* a comment, and zsh, whose interactive shells do not read comments unless `interactive_comments` is set (the macOS
|
|
314
|
+
* default), answers "command not found: #" and runs nothing from that line. A command line never carries a trailing
|
|
315
|
+
* comment: zsh would hand it to the command as arguments (test/cli/doctor.test.ts pastes the blocks into each shell).
|
|
316
|
+
*/
|
|
317
|
+
export function installNote(command) {
|
|
318
|
+
if (command === AGAVE_PATH)
|
|
319
|
+
return "# The installer adds this line to ~/.profile for new shells, and this shell needs it now:";
|
|
320
|
+
if (command === INSTALL_TESTED_TOOLCHAIN[1])
|
|
321
|
+
return "# Once: this downloads the platform-tools, about 0.5 GB and up to 1.6 GB unpacked, and prints nothing for a minute or two:";
|
|
322
|
+
if (command.startsWith("export TYPESOL_SBF_SDK="))
|
|
323
|
+
return "# Add this line to ~/.profile too, for new shells:";
|
|
324
|
+
if (command.includes("/platform-tools-linux-aarch64."))
|
|
325
|
+
return "# About 515 MB, 1.6 GB unpacked, and quiet for a minute or two:";
|
|
326
|
+
if (command.includes("/solana-release-"))
|
|
327
|
+
return "# About 215 MB, for the C headers and linker script of the SDK only:";
|
|
328
|
+
return undefined;
|
|
329
|
+
}
|
|
330
|
+
/** installCommands with each command's note on the line above it: the lines `init`, `doctor` and the build print. */
|
|
331
|
+
export function installLines(from, host = process) {
|
|
332
|
+
return installCommands(from, host).flatMap((c) => {
|
|
333
|
+
const note = installNote(c);
|
|
334
|
+
return note ? [note, c] : [c];
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
/** An install block inside an error message: each line on its own, indented, after the sentence that ends with ":". */
|
|
338
|
+
const block = (lines) => lines.map((l) => `\n ${l}`).join("");
|
|
255
339
|
// One `clang --version` per clang a process uses: up-to-date checks and manifests ask for it on every build.
|
|
256
340
|
const clangVersions = new Map();
|
|
257
341
|
/** The clang version string (the first line of `clang --version`), for build manifests. */
|
|
@@ -301,14 +385,15 @@ export function toolchainInfo(tc = findToolchain()) {
|
|
|
301
385
|
return { clang, platformTools: platformToolsVersion(tc), tested: clang === TESTED_TOOLCHAIN.clang };
|
|
302
386
|
}
|
|
303
387
|
/** What to say about a toolchain that is not the tested one; undefined for the tested one. */
|
|
304
|
-
export function untestedToolchainMessage(info) {
|
|
388
|
+
export function untestedToolchainMessage(info, strict = false) {
|
|
305
389
|
if (info.tested)
|
|
306
390
|
return undefined;
|
|
307
391
|
const t = TESTED_TOOLCHAIN;
|
|
308
|
-
return (
|
|
392
|
+
return (`${strict ? "refusing to build with" : "this build uses"} ${shortClang(info.clang)}${info.platformTools ? ` (platform-tools ${info.platformTools})` : ""}; ` +
|
|
309
393
|
`TypeSOL is tested with ${shortClang(t.clang)} (platform-tools ${t.platformTools}, Agave ${t.agave}). ` +
|
|
310
|
-
`Binaries from an untested toolchain are not covered by the project's tests
|
|
311
|
-
|
|
394
|
+
`Binaries from an untested toolchain are not covered by the project's tests` +
|
|
395
|
+
(strict ? ", and an untested toolchain is an error with --toolchain-strict or TYPESOL_TOOLCHAIN=strict" : "") +
|
|
396
|
+
`. Install the tested one:${block([INSTALL_TESTED_TOOLCHAIN[1]])}`);
|
|
312
397
|
}
|
|
313
398
|
let strictFlag = false;
|
|
314
399
|
let warned = false;
|
|
@@ -332,7 +417,7 @@ export function checkToolchain(info = toolchainInfo(), warn = (m) => console.err
|
|
|
332
417
|
if (!message)
|
|
333
418
|
return info;
|
|
334
419
|
if (toolchainStrict())
|
|
335
|
-
throw new Error(
|
|
420
|
+
throw new Error(untestedToolchainMessage(info, true));
|
|
336
421
|
if (!warned) {
|
|
337
422
|
warned = true;
|
|
338
423
|
warn(message);
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** The oldest Node.js the CLI runs on (package.json "engines"). */
|
|
2
|
+
export declare const MIN_NODE: readonly [22, 12];
|
|
3
|
+
/** The one line to print when `version` (process.versions.node) is too old, or undefined when it is new enough. */
|
|
4
|
+
export declare function nodeVersionProblem(version: string): string | undefined;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// The Node.js check the CLI runs before it loads anything else: on an older Node the compiler or its dependencies
|
|
2
|
+
// fail later with errors that do not say why (npm crashing during an install, `Map.groupBy is not a function`).
|
|
3
|
+
// No imports here, so nothing can fail before the check.
|
|
4
|
+
/** The oldest Node.js the CLI runs on (package.json "engines"). */
|
|
5
|
+
export const MIN_NODE = [22, 12];
|
|
6
|
+
/** The one line to print when `version` (process.versions.node) is too old, or undefined when it is new enough. */
|
|
7
|
+
export function nodeVersionProblem(version) {
|
|
8
|
+
const [major = 0, minor = 0] = version.split(".").map(Number);
|
|
9
|
+
if (major > MIN_NODE[0] || (major === MIN_NODE[0] && minor >= MIN_NODE[1]))
|
|
10
|
+
return undefined;
|
|
11
|
+
return `error: TypeSOL needs Node.js ${MIN_NODE.join(".")} or later, and this is Node.js ${version}. Install a newer one from https://nodejs.org (or \`nvm install 22\` with nvm), then run the command again.`;
|
|
12
|
+
}
|