Skip to content

Commit

Permalink
Merge pull request #2247 from demergent-labs/beautiful_errors_build
Browse files Browse the repository at this point in the history
Beautiful errors build
  • Loading branch information
lastmjs authored Nov 1, 2024
2 parents c6e30aa + ff99c2d commit 137bea9
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 48 deletions.
Binary file modified canister_templates/stable.wasm
Binary file not shown.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "azle",
"version": "0.25.0-pre-bifurcation",
"version": "0.25.0-dev",
"description": "TypeScript and JavaScript CDK for the Internet Computer",
"scripts": {
"typecheck": "tsc --noEmit",
Expand All @@ -11,7 +11,7 @@
},
"type": "module",
"bin": {
"azle": "./src/build/index.js"
"azle": "./src/build/index.ts"
},
"main": "./src/lib/stable/index.ts",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions src/build/experimental/utils/experimental_message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function experimentalMessageDfxJson(description: string): string {
return `Azle: Experimental mode must be enabled to use ${description}. You can enable experimental mode in your dfx.json file like this:
return `Experimental mode must be enabled to use ${description}. You can enable experimental mode in your dfx.json file like this:
{
"canisters": {
"canisterName": {
Expand All @@ -15,5 +15,5 @@ export function experimentalMessageDfxJson(description: string): string {
}

export function experimentalMessageCli(description: string): string {
return `Azle: Experimental mode must be enabled to use ${description}. You can enable experimental mode with the --experimental flag`;
return `Experimental mode must be enabled to use ${description}. You can enable experimental mode with the --experimental flag`;
}
4 changes: 0 additions & 4 deletions src/build/index.js

This file was deleted.

53 changes: 39 additions & 14 deletions src/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env -S npx tsx

import { IOType } from 'child_process';
import { join } from 'path';

Expand All @@ -20,14 +22,40 @@ import { getCanisterConfig } from './stable/utils/get_canister_config';
import { AZLE_PACKAGE_PATH } from './stable/utils/global_paths';
import { CanisterConfig, Command } from './stable/utils/types';

process.on('uncaughtException', (error: Error) => {
const prefix = 'Azle BuildError';

const message =
process.env.AZLE_VERBOSE === 'true'
? `${prefix}: ${error.stack}`
: `${prefix}: ${error}`;

console.error(message);

process.exit(1);
});

process.on('unhandledRejection', (reason: any) => {
const prefix = 'Azle BuildError';

const message =
process.env.AZLE_VERBOSE === 'true' && reason instanceof Error
? `${prefix}: ${reason.stack}`
: `${prefix}: ${reason}`;

console.error(message);

process.exit(1);
});

build();

async function build(): Promise<void> {
const command = process.argv[2] as Command | undefined;

if (command === undefined) {
throw new Error(
`Azle: No command found when running azle. Running azle should start like this: azle [commandName]`
`No command found when running azle. Running azle should start like this: azle [commandName]`
);
}

Expand All @@ -40,7 +68,7 @@ async function build(): Promise<void> {
}

if (command === 'install-global-dependencies') {
handleInstallGlobalDependenciesCommand(ioType);
handleInstallGlobalDependenciesCommand();

return;
}
Expand Down Expand Up @@ -82,7 +110,7 @@ async function build(): Promise<void> {
}

throw new Error(
`Azle: Invalid command found when running azle. Running azle ${command} is not valid`
`Invalid command found when running azle. Running azle ${command} is not valid`
);
}

Expand Down Expand Up @@ -136,24 +164,21 @@ async function handleTemplateCommand(ioType: IOType): Promise<void> {
}
}

async function handleInstallGlobalDependenciesCommand(
ioType: IOType
): Promise<void> {
async function handleInstallGlobalDependenciesCommand(): Promise<void> {
const node = process.argv.includes('--node');
const dfx = process.argv.includes('--dfx');
const rust = process.argv.includes('--rust');
const wasi2ic = process.argv.includes('--wasi2ic');

if (!node && !dfx && !rust && !wasi2ic) {
await runInstallGlobalDependenciesCommand(
{ dfx: true, node: true, rust: true, wasi2ic: true },
ioType
);
await runInstallGlobalDependenciesCommand({
dfx: true,
node: true,
rust: true,
wasi2ic: true
});
} else {
await runInstallGlobalDependenciesCommand(
{ dfx, node, rust, wasi2ic },
ioType
);
await runInstallGlobalDependenciesCommand({ dfx, node, rust, wasi2ic });
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ export async function execute(
);
const message = new TextDecoder('utf8').decode(memory);

console.error(message);

process.exit(1);
throw message;
}
}
// env: {
Expand Down
2 changes: 1 addition & 1 deletion src/build/stable/commands/compile/get_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function getContext(
const candidPath = process.env.CANISTER_CANDID_PATH;

if (candidPath === undefined) {
throw new Error(`Azle: CANISTER_CANDID_PATH is not defined`);
throw new Error(`CANISTER_CANDID_PATH is not defined`);
}

const wasmBinaryPath = join(canisterPath, `${canisterName}.wasm`);
Expand Down
6 changes: 3 additions & 3 deletions src/build/stable/commands/compile/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function bundle(buildOptions: BuildOptions): Promise<string> {

if (buildResult.outputFiles === undefined) {
throw new Error(
`Azle: Build process failed to produce JavaScript output files`
`Build process failed to produce JavaScript output files`
);
}

Expand Down Expand Up @@ -84,7 +84,7 @@ export function getBuildOptions(ts: string): BuildOptions {
filter: /^internal$|^util$|^fs$|^fs\/promises$|^fmt$|^assert$|^buffer$|^path$|^stream$|^process$|^url$|^events$|^string_decoder$|^punycode$|^querystring$|^whatwg_url$|^encoding$|^http$|^os$|^crypto$|^zlib$|^internal\/deps\/acorn\/acorn\/dist\/acorn$|^internal\/deps\/acorn\/acorn-walk\/dist\/walk$|^perf_hooks$|^async_hooks$|^https$|^_node:fs$|^_node:os$|^_node:crypto$|^qjs:os$|^_encoding$|^wasi_net$|^wasi_http$/
},
(args) => {
throw new Error(experimentalMessage(args.path));
throw experimentalMessage(args.path);
}
);
}
Expand All @@ -95,7 +95,7 @@ export function getBuildOptions(ts: string): BuildOptions {
}

function experimentalMessage(importName: string): string {
return `Azle: experimental mode must be enabled to import from ${importName}. You can enable experimental mode in your dfx.json file like this:
return `Experimental mode must be enabled to import from ${importName}. You can enable experimental mode in your dfx.json file like this:
{
"canisters": {
"canisterName": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn get_candid_and_method_meta_pointer() -> CCharPtr {
match initialize_and_get_candid() {
Ok(c_char_ptr) => c_char_ptr,
Err(error) => {
trap(&format!("Azle CandidAndMethodMetaError: {error}"));
trap(&format!("Candid and MethodMeta generation failed: {error}"));
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/build/stable/commands/install_dfx_extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ import { AZLE_PACKAGE_PATH } from '../utils/global_paths';

export function runCommand(ioType: IOType): void {
const dfxExtensionDirectoryPath = join(AZLE_PACKAGE_PATH, 'dfx_extension');
execSyncPretty(`cd ${dfxExtensionDirectoryPath} && ./install.sh`, ioType);
execSyncPretty(
`cd ${dfxExtensionDirectoryPath} && ./install.sh`,
ioType,
'dfx extension installation script execution failed'
);
}
11 changes: 4 additions & 7 deletions src/build/stable/commands/install_global_dependencies/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { IOType } from 'child_process';

import { azle } from '../../../../../package.json';
import { execSyncPretty } from '../../utils/exec_sync_pretty';
import { AZLE_PACKAGE_PATH } from '../../utils/global_paths';
Expand All @@ -11,23 +9,22 @@ type DependencyInstallInfo = {
};

export async function runCommand(
dependenciesToInstall: DependencyInstallInfo,
ioType: IOType
dependenciesToInstall: DependencyInstallInfo
): Promise<void> {
for (const key in dependenciesToInstall) {
const dependency = key as DependencyName;
if (dependenciesToInstall[dependency] === true) {
installDependency(dependency, ioType);
installDependency(dependency);
}
}
}

function installDependency(dependency: DependencyName, ioType: IOType): void {
function installDependency(dependency: DependencyName): void {
console.info(`Installing ${dependency}...`);
const version = azle.globalDependencies[dependency];
const script = `install_${dependency}.sh`;
execSyncPretty(
`${AZLE_PACKAGE_PATH}/src/build/stable/commands/install_global_dependencies/${script} ${version}`,
ioType
'inherit'
);
}
3 changes: 1 addition & 2 deletions src/build/stable/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export async function runCommand(
templatePath: string
): Promise<void> {
if (process.argv[3] === undefined) {
console.error('You must provide a name for your Azle project');
return;
throw new Error(`You must provide a name for your Azle project`);
}

const projectName = process.argv[3];
Expand Down
12 changes: 10 additions & 2 deletions src/build/stable/utils/exec_sync_pretty.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { execSync, IOType } from 'child_process';

export function execSyncPretty(command: string, stdio?: IOType): Buffer {
export function execSyncPretty(
command: string,
stdio?: IOType,
hint?: string
): Buffer {
try {
return execSync(command, { stdio });
} catch (error) {
throw new Error(`Azle build error`);
if (hint !== undefined) {
throw new Error(`${hint}: ${error}`);
} else {
throw error;
}
}
}
2 changes: 1 addition & 1 deletion src/build/stable/utils/versions/dfx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export function getLocalDfxVersion(): string {
if (match !== null && match.length > 1 && typeof match[1] === 'string') {
return match[1]; // Return the version number
} else {
throw new Error('Could not parse the dfx version');
throw new Error(`Could not parse the dfx version`);
}
}
2 changes: 1 addition & 1 deletion src/build/stable/utils/versions/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export function getLocalNodeVersion(): string {
if (match !== null && match.length > 1 && typeof match[1] === 'string') {
return match[1]; // Returns the version number (e.g., "16.13.0")
} else {
throw new Error('Could not parse node version');
throw new Error(`Could not parse node version`);
}
}
2 changes: 1 addition & 1 deletion src/build/stable/utils/versions/rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export function getLocalRustVersion(): string {
if (match !== null && match.length > 1 && typeof match[1] === 'string') {
return match[1]; // Returns the version number
} else {
throw new Error('Could not parse rustc version');
throw new Error(`Could not parse rustc version`);
}
}

0 comments on commit 137bea9

Please sign in to comment.