Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fuels dev hangs after compilation errors #3646

Merged
merged 6 commits into from
Jan 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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();
}
4 changes: 2 additions & 2 deletions packages/fuels/src/cli/commands/withConfig.test.ts
Original file line number Diff line number Diff line change
@@ -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);
@@ -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);
7 changes: 3 additions & 4 deletions packages/fuels/src/cli/commands/withConfig.ts
Original file line number Diff line number Diff line change
@@ -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>(
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';

@@ -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,