Skip to content

Commit 4131e74

Browse files
fix: enable fuel-core node cleanup to work in Bun (#3438)
* fix: issue with bun not kill processes * chore: removed `console.log`'s from tests * chore: ensure all process children killed * chore: changeset * chore: release * chore: fix test * chore: un-release * chore: pls fix * chore: include custom .d.ts files * chore: fix test * chore: removed flaky check * chore: suppressing errors
1 parent 270b507 commit 4131e74

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

.changeset/wise-ladybugs-nail.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fuel-ts/account": patch
3+
---
4+
5+
fix: enable `fuel-core` node cleanup to work in Bun

packages/account/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,16 @@
6868
"ramda": "^0.30.1"
6969
},
7070
"devDependencies": {
71-
"type-fest": "^4.26.1",
7271
"@fuel-ts/hasher": "workspace:*",
7372
"@fuel-ts/math": "workspace:*",
7473
"@fuel-ts/utils": "workspace:*",
7574
"@graphql-codegen/cli": "^5.0.3",
7675
"@graphql-codegen/typescript": "^4.0.9",
7776
"@graphql-codegen/typescript-generic-sdk": "^4.0.1",
7877
"@graphql-codegen/typescript-operations": "^4.2.3",
78+
"@internal/utils": "workspace:*",
7979
"@types/ramda": "^0.30.2",
8080
"get-graphql-schema": "^2.1.2",
81-
"@internal/utils": "workspace:*"
81+
"type-fest": "^4.26.1"
8282
}
8383
}

packages/account/src/test-utils/launchNode.test.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ vi.mock('fs', async () => {
3030
*/
3131
describe('launchNode', () => {
3232
test('using ephemeral port 0 is possible', async () => {
33-
const { cleanup, port, url } = await launchNode({ port: '0' });
33+
const { cleanup, port, url } = await launchNode({ port: '0', loggingEnabled: false });
3434
expect(await fetch(url)).toBeTruthy();
3535
expect(port).not.toEqual('0');
3636

3737
cleanup();
3838
});
3939

4040
it('cleanup kills the started node', async () => {
41-
const { cleanup, url } = await launchNode();
41+
const { cleanup, url } = await launchNode({ loggingEnabled: false });
4242
expect(await fetch(url)).toBeTruthy();
4343

4444
cleanup();
@@ -57,7 +57,7 @@ describe('launchNode', () => {
5757
const spawnSpy = vi.spyOn(childProcessMod, 'spawn');
5858
const killSpy = vi.spyOn(process, 'kill');
5959

60-
const { cleanup, pid } = await launchNode();
60+
const { cleanup, pid } = await launchNode({ loggingEnabled: false });
6161

6262
const spawnOptions = spawnSpy.mock.calls[0][2];
6363
expect(spawnOptions.detached).toBeTruthy();
@@ -74,7 +74,7 @@ describe('launchNode', () => {
7474

7575
process.env.FUEL_CORE_PATH = '';
7676

77-
const { result } = await safeExec(async () => launchNode());
77+
const { result } = await safeExec(async () => launchNode({ loggingEnabled: false }));
7878

7979
const command = spawnSpy.mock.calls[0][0];
8080
expect(command).toEqual('fuel-core');
@@ -95,7 +95,7 @@ describe('launchNode', () => {
9595

9696
const fuelCorePath = './my-fuel-core-binary-path';
9797
const { error } = await safeExec(async () => {
98-
await launchNode({ fuelCorePath, loggingEnabled: true });
98+
await launchNode({ fuelCorePath, loggingEnabled: false });
9999
});
100100

101101
expect(error).toBeTruthy();
@@ -107,7 +107,7 @@ describe('launchNode', () => {
107107
test('reads FUEL_CORE_PATH environment variable for fuel-core binary', async () => {
108108
const spawnSpy = vi.spyOn(childProcessMod, 'spawn');
109109
process.env.FUEL_CORE_PATH = 'fuels-core';
110-
const { cleanup, url } = await launchNode();
110+
const { cleanup, url } = await launchNode({ loggingEnabled: false });
111111
await Provider.create(url);
112112

113113
const command = spawnSpy.mock.calls[0][0];
@@ -152,7 +152,7 @@ describe('launchNode', () => {
152152
});
153153

154154
test('logs fuel-core outputs via console.log', async () => {
155-
const logSpy = vi.spyOn(console, 'log');
155+
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
156156

157157
const { cleanup } = await launchNode({ loggingEnabled: true });
158158
const logs = logSpy.mock.calls.map((call) => call[0]);
@@ -162,7 +162,7 @@ describe('launchNode', () => {
162162

163163
test('cleanup removes temporary directory', async () => {
164164
const mkdirSyncSpy = vi.spyOn(fsMod, 'mkdirSync');
165-
const { cleanup } = await launchNode();
165+
const { cleanup } = await launchNode({ loggingEnabled: false });
166166

167167
expect(mkdirSyncSpy).toHaveBeenCalledTimes(1);
168168
const tempDirPath = mkdirSyncSpy.mock.calls[0][0];
@@ -252,4 +252,18 @@ describe('launchNode', () => {
252252
`fuel-core node under pid ${pid} does not exist. The node might have been killed before cleanup was called. Exiting cleanly.`
253253
);
254254
});
255+
256+
test('should clean up when unable to kill process with "RangeError: pid must be a positive integer" error', async () => {
257+
const killSpy = vi.spyOn(process, 'kill').mockImplementationOnce(() => {
258+
throw new RangeError('pid must be a positive integer');
259+
});
260+
261+
const { pid, cleanup } = await launchNode({ loggingEnabled: false });
262+
263+
cleanup();
264+
265+
expect(killSpy).toBeCalledTimes(2);
266+
expect(killSpy).toBeCalledWith(-pid);
267+
expect(killSpy).toBeCalledWith(+pid);
268+
});
255269
});

packages/account/src/test-utils/launchNode.ts

+4
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ export const launchNode = async ({
236236
console.log(
237237
`fuel-core node under pid ${child.pid} does not exist. The node might have been killed before cleanup was called. Exiting cleanly.`
238238
);
239+
} else if (error.message.includes('pid must be a positive integer')) {
240+
// This is a workaround for a bug with Bun.
241+
// See: https://github.com/oven-sh/bun/issues/8787
242+
process.kill(+child.pid);
239243
} else {
240244
throw e;
241245
}

pnpm-lock.yaml

+2-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tsconfig.test.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"compilerOptions": {
44
"noEmit": true
55
},
6-
"include": ["**/*.test.ts"],
6+
"include": ["**/*.test.ts", "**/*.d.ts"],
77
"exclude": ["node_modules", "apps/docs/src/**/*.test.ts"]
88
}

0 commit comments

Comments
 (0)