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: enable usage of 0 port for the fuelCorePort field #3528

Draft
wants to merge 24 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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/spicy-oranges-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels": patch
---

fix: enable usage of `0` port for the `fuelCorePort` field
9 changes: 4 additions & 5 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ jobs:
strategy:
fail-fast: false
matrix:
env:
[
{ name: node, version: 18.20.3, display-name: "node@18" },
{ name: node, version: 20.14.0, display-name: "node@20" },
env: [
# { name: node, version: 18.20.3, display-name: "node@18" },
# { name: node, version: 20.14.0, display-name: "node@20" },
{ name: node, version: 22.3.0, display-name: "node@22" },
{ name: browser, display-name: "browser" },
# { name: browser, display-name: "browser" },
]
timeout-minutes: 25
steps:
Expand Down
1 change: 1 addition & 0 deletions apps/docs/fuels.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default createConfig({
forcBuildFlags: ['--release'],
forcPath: 'fuels-forc',
fuelCorePath: 'fuels-core',
fuelCorePort: 0,
});
3 changes: 1 addition & 2 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
"dev": "nodemon --config nodemon.config.json -x 'run-s build:snippets dev:docs'",
"build": "run-s build:snippets build:docs",
"preview": "run-s build:snippets preview:docs",
"pretest": "./scripts/pretest.sh",
"test": "cd ../.. && pnpm run test:filter apps/docs",
"build:snippets": "run-s wrap:snippets build:forc",
"build:docs": "vitepress build",
"preview:docs": "vitepress preview",
"dev:docs": "vitepress dev",
"wrap:snippets": "tsx ./scripts/wrap-snippets.ts",
"build:forc": "pnpm fuels build",
"build:forc": "pnpm fuels build --deploy",
"type:check": "pnpm tsc --noEmit --project tsconfig.emit.json"
},
"keywords": [],
Expand Down
16 changes: 0 additions & 16 deletions apps/docs/scripts/pretest.sh

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"packageManager": "[email protected]",
"scripts": {
"dev": "nodemon --config nodemon.config.json -x 'pnpm build:packages'",
"build": "turbo run build --cache-dir=.turbo",
"build": "turbo run build --cache-dir=.turbo --filter=fuels",
"build:packages": "turbo run build --filter=!docs --filter=!template-*",
"ci:test": "./scripts/tests-ci.sh",
"pretest": "turbo run pretest",
Expand All @@ -20,7 +20,7 @@
"depsync:lint": "syncpack list-mismatches",
"depsync:fix": "syncpack fix-mismatches",
"test": "vitest --run --coverage --config vitest.node.config.mts $(scripts/tests-find.sh --node)",
"test:node": "vitest --run --coverage --config vitest.node.config.mts $(scripts/tests-find.sh --node)",
"test:node": "vitest --run --coverage --config vitest.node.config.mts packages/fuel-gauge/src/fuels-dev.test.ts",
"test:browser": "vitest --run --coverage --config vitest.browser.config.mts $(scripts/tests-find.sh --browser)",
"test:all": "run-p test:node test:browser",
"test:filter": "vitest --run --coverage false --config vitest.node.config.mts",
Expand Down
1 change: 1 addition & 0 deletions packages/fuel-gauge/fuels.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export default createConfig({
forcBuildFlags: ['--release'],
forcPath: 'fuels-forc',
fuelCorePath: 'fuels-core',
fuelCorePort: 0,
});
2 changes: 1 addition & 1 deletion packages/fuel-gauge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Fuel Labs <[email protected]> (https://fuel.network/)",
"scripts": {
"pretest": "run-s build:forc build:process-predicates",
"build:forc": "pnpm fuels build",
"build:forc": "pnpm fuels build --deploy",
"build:process-predicates": "tsx ./scripts/process-predicates.ts",
"type:check": "tsc --noEmit"
},
Expand Down
12 changes: 12 additions & 0 deletions packages/fuel-gauge/src/fuels-dev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { execSync } from 'child_process';

/**
* @group node
*/
describe('fuels-dev', () => {
test('it works', () => {
const res = execSync('pnpm fuels init').toString();

Check failure on line 8 in packages/fuel-gauge/src/fuels-dev.test.ts

View workflow job for this annotation

GitHub Actions / node@22

packages/fuel-gauge/src/fuels-dev.test.ts > fuels-dev > it works

Error: Command failed: pnpm fuels init ❯ packages/fuel-gauge/src/fuels-dev.test.ts:8:17 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { status: 254, signal: null, output: [ null, { type: 'Buffer', data: [ 117, 110, 100, 101, 102, 105, 110, 101, 100, 10, 226, 128, 137, 69, 82, 82, 95, 80, 78, 80, 77, 95, 82, 69, 67, 85, 82, 83, 73, 86, 69, 95, 69, 88, 69, 67, 95, 70, 73, 82, 83, 84, 95, 70, 65, 73, 76, 226, 128, 137, 32, 67, 111, 109, 109, 97, 110, 100, 32, 34, 102, 117, 101, 108, 115, 34, 32, 110, 111, 116, 32, 102, 111, 117, 110, 100, 10, 10, 68, 105, 100, 32, 121, 111, 117, 32, 109, 101, 97, 110, 32, 34, 112, 110, 112, 109, 32, 98, 117, 105, 108, 100, 34, 63, 10 ] }, { type: 'Buffer', data: [] } ], pid: 7238, stdout: { type: 'Buffer', data: [ 117, 110, 100, 101, 102, 105, 110, 101, 100, 10, 226, 128, 137, 69, 82, 82, 95, 80, 78, 80, 77, 95, 82, 69, 67, 85, 82, 83, 73, 86, 69, 95, 69, 88, 69, 67, 95, 70, 73, 82, 83, 84, 95, 70, 65, 73, 76, 226, 128, 137, 32, 67, 111, 109, 109, 97, 110, 100, 32, 34, 102, 117, 101, 108, 115, 34, 32, 110, 111, 116, 32, 102, 111, 117, 110, 100, 10, 10, 68, 105, 100, 32, 121, 111, 117, 32, 109, 101, 97, 110, 32, 34, 112, 110, 112, 109, 32, 98, 117, 105, 108, 100, 34, 63, 10 ] }, stderr: { type: 'Buffer', data: [] } }

console.log(res);
});
});
6 changes: 2 additions & 4 deletions packages/fuels/src/cli/commands/dev/autoStartFuelCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ export const autoStartFuelCore = async (config: FuelsConfig) => {

const port = config.fuelCorePort ?? (await getPortPromise({ port: 4000 }));

const providerUrl = `http://${accessIp}:${port}/v1/graphql`;

const { cleanup, snapshotDir } = await launchNode({
const { cleanup, url, snapshotDir } = await launchNode({
args: [
['--snapshot', config.snapshotDir],
['--db-type', 'in-memory'],
Expand All @@ -44,7 +42,7 @@ export const autoStartFuelCore = async (config: FuelsConfig) => {
bindIp,
accessIp,
port,
providerUrl,
providerUrl: url,
snapshotDir,
killChildProcess: cleanup,
};
Expand Down
49 changes: 49 additions & 0 deletions packages/fuels/test/features/dev.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { deferPromise } from '@fuel-ts/account';
import { spawn } from 'child_process';
import * as chokidar from 'chokidar';
import { join } from 'path';
import { cwd } from 'process';

import * as buildMod from '../../src/cli/commands/build/index';
import * as deployMod from '../../src/cli/commands/deploy/index';
Expand Down Expand Up @@ -77,4 +81,49 @@ describe('dev', () => {
expect(watch).toHaveBeenCalledTimes(2);
expect(on).toHaveBeenCalledTimes(2);
});

test('`dev` command can work with ephemeral port 0', async () => {
await runInit({
root: paths.root,
workspace: paths.workspaceDir,
output: paths.outputDir,
forcPath: paths.forcPath,
fuelCorePath: paths.fuelCorePath,
fuelCorePort: '0',
});

/**
* TODO: This is a workaround to get to fuels.js
* because it's not available as a binary in the fuels package itself.
* I must test this in fuels-gauge to verify.
*/
const buildFuelsPath = join(cwd(), 'packages/fuels/fuels.js');

const devProcess = spawn(`node ${buildFuelsPath} dev --path ${paths.root}`, {
shell: 'bash',
});

const nodeLaunched = deferPromise<string>();

const graphQLStartSubstring = 'Binding GraphQL provider to';

devProcess.stdout.on('data', (chunk) => {
const text: string = chunk.toString();
if (text.indexOf(graphQLStartSubstring) !== -1) {
const rows = text.split('\n');
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const rowWithUrl = rows.find((row) => row.indexOf(graphQLStartSubstring) !== -1)!;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const [, port] = rowWithUrl.split(' ').at(-1)!.trim().split(':'); // e.g. "2024-02-13T12:31:44.445844Z INFO new{name=fuel-core}: fuel_core::graphql_api::service: 216: Binding GraphQL provider to 127.0.0.1:35039"

nodeLaunched.resolve(port);
}
});

const nodePort = await nodeLaunched.promise;

expect(nodePort).not.toBe('0');
// we verify it not to be the default port
expect(nodePort).not.toBe('4000');
});
});
7 changes: 5 additions & 2 deletions packages/fuels/test/utils/runCommands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cpSync, existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'fs';
import { cpSync, existsSync, mkdirSync, readFileSync, rmdirSync, rmSync, writeFileSync } from 'fs';
import { join, basename } from 'path';

import { Commands } from '../../src';
Expand Down Expand Up @@ -75,6 +75,9 @@ export function bootstrapProject(testFilepath: string) {
fooContractFactoryPath,
forcPath,
fuelCorePath,
[Symbol.dispose]: () => {
rmdirSync(root, { recursive: true });
},
};
}

Expand Down Expand Up @@ -149,7 +152,7 @@ export async function runInit(params: InitParams) {
const config = readFileSync(configPath, 'utf-8');

const search = /(^.*fuelCorePath:.*$)/m;
const replace = `$1\n privateKey: '${privateKey}',`;
const replace = `$1\n privateKey: '${privateKey}',\n`;

writeFileSync(configPath, config.replace(search, replace));
}
Expand Down
Loading