Skip to content

Commit

Permalink
fix: fuels dev hangs after compilation errors (#3646)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk authored Jan 31, 2025
1 parent 06e70dc commit 43dc5ee
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-pianos-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels": patch
---

fix: `fuels dev` hangs after compilation errors
9 changes: 2 additions & 7 deletions packages/fuels/src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { error } from './cli/utils/logger';
import { run } from './run';

try {
run(process.argv).catch((x) => {
// eslint-disable-next-line no-console
console.log(x);
});
} catch (err: unknown) {
run(process.argv).catch((err) => {
error((err as Error)?.message || err);
process.exit(1);
}
});
3 changes: 1 addition & 2 deletions packages/fuels/src/cli/commands/build/forcHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ type OnErrorFn = (reason?: number | Error) => void;
export const onForcExit =
(onResultFn: OnResultFn, onErrorFn: OnErrorFn) => (code: number | null) => {
if (code) {
onErrorFn(code);
// process.exit()?
onErrorFn(new Error(`forc exited with error code ${code}`));
} else {
onResultFn();
}
Expand Down
4 changes: 2 additions & 2 deletions packages/fuels/src/cli/commands/withConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('withConfig', () => {
shouldErrorOnDeploy: true,
});

await withConfig(command, Commands.deploy, deploy)();
await expect(withConfig(command, Commands.deploy, deploy)).rejects.toThrow();

expect(loadConfig).toHaveBeenCalledTimes(1);
expect(loadConfig.mock.calls[0][0]).toEqual(configPath);
Expand All @@ -84,7 +84,7 @@ describe('withConfig', () => {
shouldErrorOnLoadConfig: true,
});

await withConfig(command, Commands.deploy, deploy)();
await expect(withConfig(command, Commands.deploy, deploy)).rejects.toThrow();

expect(loadConfig).toHaveBeenCalledTimes(1);
expect(loadConfig.mock.calls[0][0]).toEqual(configPath);
Expand Down
7 changes: 3 additions & 4 deletions packages/fuels/src/cli/commands/withConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import { loadConfig } from '../config/loadConfig';
import type { Commands, FuelsConfig, CommandEvent } from '../types';
import { error, log } from '../utils/logger';

export const withConfigErrorHandler = async (err: Error, config?: FuelsConfig) => {
export const withConfigErrorHandler = async (err: Error, config?: FuelsConfig): Promise<void> => {
error(err.message);
if (config) {
await config.onFailure?.(config, <Error>err);
}
await config?.onFailure?.(config, <Error>err);
throw err;
};

export function withConfig<CType extends Commands>(
Expand Down
42 changes: 42 additions & 0 deletions packages/fuels/test/features/dev.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { deferPromise } from '@fuel-ts/account';
import { spawn } from 'child_process';
import * as chokidar from 'chokidar';
import { readFileSync, writeFileSync } from 'node:fs';
import { join } from 'path';
import { cwd } from 'process';

Expand Down Expand Up @@ -82,6 +83,47 @@ describe('dev', () => {
expect(on).toHaveBeenCalledTimes(2);
});

it('exits when build fails', { timeout: 30_000 }, async () => {
const mainSw = readFileSync(`${paths.contractsBarDir}/src/main.sw`).toString();
const invalidSwayCode = `${mainSw}\nabi `;
writeFileSync(`${paths.contractsBarDir}/src/main.sw`, invalidSwayCode);

await runInit({
root: paths.root,
output: paths.outputDir,
workspace: paths.workspaceDir,
forcPath: paths.forcPath,
fuelCorePath: paths.fuelCorePath,
fuelCorePort: '0',
});

const devProcess = spawn(`pnpm fuels dev --path ${paths.root}`, {
detached: true,
shell: 'bash',
/**
* pnpm fuels dev fails because the test is run in the root directory
* and there is no `fuels` dependency in `package.json` there,
* so we have to give the spawn a cwd which has `fuels` as a dependency.
*/
cwd: join(cwd(), 'packages/fuel-gauge'),
});

const data: string[] = [];

devProcess.stdout?.on('data', (chunk) => {
data.push(chunk.toString());
});

await new Promise((resolve) => {
devProcess.on('exit', (code) => {
expect(code).not.toEqual(0);
resolve(undefined);
});
});

expect(data.join('')).toContain('forc exited with error code 1');
});

test('`dev` command can work with ephemeral port 0', { timeout: 25000 }, async () => {
await runInit({
root: paths.root,
Expand Down

0 comments on commit 43dc5ee

Please sign in to comment.