Skip to content

Commit

Permalink
Merge pull request #1751 from demergent-labs/improve_terminal_error_o…
Browse files Browse the repository at this point in the history
…utput

Improve terminal error output
  • Loading branch information
lastmjs authored Apr 22, 2024
2 parents 98381d7 + 0cacf22 commit 3b94a2f
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 83 deletions.
42 changes: 22 additions & 20 deletions src/compiler/compile_rust_code.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { execSync, IOType } from 'child_process';
import { IOType } from 'child_process';

import { execSyncPretty } from './utils/exec_sync_pretty';

export function compileRustCode(
dockerContainerName: string,
Expand All @@ -18,47 +20,47 @@ function compileRustCodeWithPodman(
canisterName: string,
stdio: IOType
) {
execSync(
execSyncPretty(
`podman exec ${dockerContainerName} rm -rf /.azle/${canisterName}`,
{ stdio }
stdio
);

execSync(`podman exec ${dockerContainerName} mkdir -p /.azle`, {
stdio
});
execSyncPretty(`podman exec ${dockerContainerName} mkdir -p /.azle`, stdio);

execSync(`podman exec ${dockerContainerName} mkdir -p /global_target_dir`, {
execSyncPretty(
`podman exec ${dockerContainerName} mkdir -p /global_target_dir`,
stdio
});
);

execSync(`podman cp .azle/${canisterName} ${dockerContainerName}:/.azle`, {
execSyncPretty(
`podman cp .azle/${canisterName} ${dockerContainerName}:/.azle`,
stdio
});
);

execSync(
execSyncPretty(
`podman exec -w /.azle/${canisterName} ${dockerContainerName} env CARGO_TARGET_DIR=/global_target_dir cargo build --target wasm32-wasi --manifest-path canister/Cargo.toml --release`,
{ stdio }
stdio
);

execSync(
execSyncPretty(
`podman exec -w /.azle/${canisterName} ${dockerContainerName} wasi2ic /global_target_dir/wasm32-wasi/release/canister.wasm /global_target_dir/wasm32-wasi/release/canister.wasm`,
{ stdio }
stdio
);

execSync(
execSyncPretty(
`podman cp ${dockerContainerName}:/global_target_dir/wasm32-wasi/release/canister.wasm .azle/${canisterName}/${canisterName}.wasm`,
{ stdio }
stdio
);
}

function compileRustCodeNatively(canisterName: string, stdio: IOType) {
execSync(
execSyncPretty(
`CARGO_TARGET_DIR=target cargo build --target wasm32-wasi --manifest-path .azle/${canisterName}/canister/Cargo.toml --release`,
{ stdio }
stdio
);

execSync(
execSyncPretty(
`wasi2ic target/wasm32-wasi/release/canister.wasm .azle/${canisterName}/${canisterName}.wasm`,
{ stdio }
stdio
);
}
6 changes: 4 additions & 2 deletions src/compiler/file_watcher/setup_file_watcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { execSync, spawn } from 'child_process';
import { spawn } from 'child_process';
import { join } from 'path';

import { execSyncPretty } from '../utils/exec_sync_pretty';

export function setupFileWatcher(
reloadedJsPath: string,
canisterId: string,
Expand All @@ -14,7 +16,7 @@ export function setupFileWatcher(
// TODO should we figure out why the || true
// TODO does not result in a 0 exit code
// TODO and look into removing the try catch?
execSync(`pkill -f ./file_watcher_loader.js || true`);
execSyncPretty(`pkill -f ./file_watcher_loader.js || true`);
} catch (error) {
// For some reason pkill throws even with || true
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/get_names.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { execSync } from 'child_process';
import { createHash } from 'crypto';
import { readFileSync } from 'fs';
import { hashOfDirectory } from 'hash-of-directory';
Expand All @@ -11,6 +10,7 @@ import {
getStdIoType,
unwrap
} from './utils';
import { execSyncPretty } from './utils/exec_sync_pretty';
import { GLOBAL_AZLE_CONFIG_DIR } from './utils/global_paths';
import { JSCanisterConfig } from './utils/types';

Expand Down Expand Up @@ -38,7 +38,7 @@ export async function getNamesBeforeCli() {
wasmedgeQuickJsName
);

const replicaWebServerPort = execSync(`dfx info webserver-port`)
const replicaWebServerPort = execSyncPretty(`dfx info webserver-port`)
.toString()
.trim();

Expand Down
25 changes: 9 additions & 16 deletions src/compiler/handle_cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { execSync, IOType } from 'child_process';
import { IOType } from 'child_process';
import { rmSync } from 'fs';

import { version as azleVersion } from '../../package.json';
import { uploadFiles } from './file_uploader';
import { getFilesToUpload } from './file_uploader/get_files_to_upload';
import { generateNewAzleProject } from './new_command';
import { execSyncPretty } from './utils/exec_sync_pretty';
import { GLOBAL_AZLE_CONFIG_DIR } from './utils/global_paths';

export function handleCli(
Expand Down Expand Up @@ -53,9 +54,7 @@ function handleCommandNew() {
}

function handleCommandDockerfileHash(dockerfileHash: string) {
execSync(`echo -n "${dockerfileHash}"`, {
stdio: 'inherit'
});
execSyncPretty(`echo -n "${dockerfileHash}"`, 'inherit');
}

function handleCommandClean(
Expand All @@ -77,29 +76,23 @@ function handleCommandClean(

console.info(`.azle directory deleted`);

execSync(
execSyncPretty(
`podman stop $(podman ps --filter "name=${dockerContainerPrefix}" --format "{{.ID}}") || true`,
{
stdio: stdioType
}
stdioType
);

console.info(`azle containers stopped`);

execSync(
execSyncPretty(
`podman rm $(podman ps -a --filter "name=${dockerContainerPrefix}" --format "{{.ID}}") || true`,
{
stdio: stdioType
}
stdioType
);

console.info(`azle containers removed`);

execSync(
execSyncPretty(
`podman image rm $(podman images --filter "reference=${dockerImagePrefix}" --format "{{.ID}}") || true`,
{
stdio: stdioType
}
stdioType
);

console.info(`azle images removed`);
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ async function azle() {
prepareRustStagingArea(
canisterConfig,
canisterPath,
canisterJavaScript
canisterJavaScript,
stdioType
);

const { candid, canisterMethods } = getCandidAndCanisterMethods(
Expand Down
62 changes: 23 additions & 39 deletions src/compiler/prepare_docker_image.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { execSync, IOType } from 'child_process';
import { IOType } from 'child_process';
import { existsSync } from 'fs';

import { version as azleVersion } from '../../package.json';
import { yellow } from './utils/colors';
import { execSyncPretty } from './utils/exec_sync_pretty';

export function prepareDockerImage(
stdioType: IOType,
Expand Down Expand Up @@ -39,18 +40,14 @@ function initAndStartVm(stdioType: IOType) {

// TODO detect mac and only run these commands on mac

execSync(
execSyncPretty(
`if [ "$(uname -s)" != "Linux" ]; then podman machine init || true; fi`,
{
stdio: stdioType
}
stdioType
);

execSync(
execSyncPretty(
`if [ "$(uname -s)" != "Linux" ]; then podman machine start || true; fi`,
{
stdio: stdioType
}
stdioType
);
}

Expand Down Expand Up @@ -94,9 +91,7 @@ function hasImageAlreadyBeenLoaded(
dockerImageName: string
): boolean {
try {
execSync(`podman image inspect ${dockerImageName}`, {
stdio: stdioType
});
execSyncPretty(`podman image inspect ${dockerImageName}`, stdioType);

return true;
} catch (error) {
Expand All @@ -111,23 +106,17 @@ function loadExistingLocalImage(
if (existsSync(dockerImagePathTar)) {
console.info(yellow(`\nLoading image...\n`));

execSync(`podman load -i ${dockerImagePathTar}`, {
stdio: 'inherit'
});
execSyncPretty(`podman load -i ${dockerImagePathTar}`, 'inherit');

return true;
}

if (existsSync(dockerImagePathTarGz)) {
console.info(yellow(`\nLoading image...\n`));

execSync(`gzip -d ${dockerImagePathTarGz}`, {
stdio: 'inherit'
});
execSyncPretty(`gzip -d ${dockerImagePathTarGz}`, 'inherit');

execSync(`podman load -i ${dockerImagePathTar}`, {
stdio: 'inherit'
});
execSyncPretty(`podman load -i ${dockerImagePathTar}`, 'inherit');

return true;
}
Expand All @@ -141,18 +130,17 @@ function buildAndLoadImageWithDockerfile(
) {
console.info(yellow(`\nBuilding image...\n`));

execSync(
execSyncPretty(
`podman build -f ${__dirname}/Dockerfile -t ${dockerImageName} ${__dirname}`,
{
stdio: 'inherit'
}
'inherit'
);

console.info(yellow(`\nSaving image...\n`));

execSync(`podman save -o ${dockerImagePathTar} ${dockerImageName}`, {
stdio: 'inherit'
});
execSyncPretty(
`podman save -o ${dockerImagePathTar} ${dockerImageName}`,
'inherit'
);

console.info(yellow(`\nCompiling...`));
}
Expand All @@ -164,11 +152,9 @@ function downloadAndLoadRemoteImage(
) {
console.info(yellow(`\nDownloading image...\n`));

execSync(
execSyncPretty(
`curl -L -f https://github.com/demergent-labs/azle/releases/download/${azleVersion}/${dockerImageName}.tar.gz -o ${dockerImagePathTarGz}`,
{
stdio: 'inherit'
}
'inherit'
);

loadExistingLocalImage(dockerImagePathTar, dockerImagePathTarGz);
Expand All @@ -182,19 +168,17 @@ function createAndStartContainer(
dockerContainerName: string,
wasmedgeQuickJsPath: string
) {
execSync(
execSyncPretty(
`podman create --name ${dockerContainerName} ${dockerImageName} tail -f /dev/null || true`,
{ stdio: stdioType }
stdioType
);

execSync(`podman start ${dockerContainerName}`, {
stdio: stdioType
});
execSyncPretty(`podman start ${dockerContainerName}`, stdioType);

if (!existsSync(wasmedgeQuickJsPath)) {
execSync(
execSyncPretty(
`podman cp ${dockerContainerName}:/wasmedge-quickjs ${wasmedgeQuickJsPath}`,
{ stdio: stdioType }
stdioType
);
}
}
8 changes: 5 additions & 3 deletions src/compiler/prepare_rust_staging_area.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { execSync } from 'child_process';
import { IOType } from 'child_process';
import { existsSync, mkdirSync, rmSync, writeFileSync } from 'fs';
import { copySync } from 'fs-extra';
import { join } from 'path';

import { generateWorkspaceCargoToml } from './generate_cargo_toml_files';
import { execSyncPretty } from './utils/exec_sync_pretty';
import { JSCanisterConfig, Toml } from './utils/types';

export function prepareRustStagingArea(
canisterConfig: JSCanisterConfig,
canisterPath: string,
canisterJavaScript: string
canisterJavaScript: string,
stdioType: IOType
) {
const workspaceCargoToml: Toml = generateWorkspaceCargoToml(
canisterConfig.opt_level ?? '0'
Expand Down Expand Up @@ -44,7 +46,7 @@ export function prepareRustStagingArea(
canisterConfig.build_assets !== undefined &&
canisterConfig.build_assets !== null
) {
execSync(canisterConfig.build_assets);
execSyncPretty(canisterConfig.build_assets, stdioType);
}

for (const [src, dest] of canisterConfig.assets ?? []) {
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/utils/exec_sync_pretty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { execSync, IOType } from 'child_process';

export function execSyncPretty(command: string, stdio?: IOType): Buffer {
try {
return execSync(command, { stdio });
} catch (error) {
throw new Error(`Azle build error`);
}
}

0 comments on commit 3b94a2f

Please sign in to comment.