Skip to content

Commit 769f4f7

Browse files
feat: use pre-built Cargo target in CI (#505)
* feat: use pre-built Cargo target in CI Signed-off-by: David Dal Busco <david.dalbusco@outlook.com> * feat: same RUSTFLAGS as on Juno main repo Signed-off-by: David Dal Busco <david.dalbusco@outlook.com> * feat: flags order Signed-off-by: David Dal Busco <david.dalbusco@outlook.com> * docs: move comment Signed-off-by: David Dal Busco <david.dalbusco@outlook.com> --------- Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>
1 parent cfad7d0 commit 769f4f7

2 files changed

Lines changed: 35 additions & 4 deletions

File tree

src/constants/dev.constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export const SPUTNIK_INDEX_RS = 'sputnik_index.rs';
5454
export const DEPLOY_SPUTNIK_SCRIPT_PATH = join(DEPLOY_LOCAL_REPLICA_PATH, SPUTNIK_INDEX_MJS);
5555
export const DEPLOY_SPUTNIK_FUNCTIONS_PATH = join(DEPLOY_LOCAL_REPLICA_PATH, SPUTNIK_INDEX_RS);
5656

57-
export const JUNO_ACTION_SPUTNIK_PATH = '/juno/src/sputnik';
57+
export const JUNO_ACTION_PROJECT_PATH = '/juno';
58+
export const JUNO_ACTION_SPUTNIK_PATH = join(JUNO_ACTION_PROJECT_PATH, 'src', 'sputnik');
5859
export const SPUTNIK_CARGO_TOML = join(JUNO_ACTION_SPUTNIK_PATH, CARGO_TOML);
5960

6061
export const SATELLITE_WASM = 'satellite.wasm';

src/services/functions/build/build.rust.services.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import {
1414
} from '../../../constants/build.constants';
1515
import {
1616
DEPLOY_SPUTNIK_SCRIPT_PATH,
17+
JUNO_ACTION_PROJECT_PATH,
1718
JUNO_PACKAGE_JSON_PATH,
1819
SATELLITE_OUTPUT,
1920
SATELLITE_PROJECT_NAME,
2021
SPUTNIK_PROJECT_NAME,
2122
TARGET_PATH
2223
} from '../../../constants/dev.constants';
24+
import {ENV} from '../../../env';
2325
import type {BuildArgs, BuildType} from '../../../types/build';
2426
import {checkIcpBindgen} from '../../../utils/build.bindgen.utils';
2527
import {checkCandidExtractor, checkIcWasm, checkWasi2ic} from '../../../utils/build.cargo.utils';
@@ -69,8 +71,10 @@ export const buildRust = async ({
6971
const defaultProjectArgs = ['-p', SATELLITE_PROJECT_NAME];
7072

7173
const cargoTarget = target ?? 'wasm32-unknown-unknown';
72-
const cargoReleaseDir = join(process.cwd(), 'target');
73-
const cargoOutputWasm = join(cargoReleaseDir, cargoTarget, 'release', 'satellite.wasm');
74+
const cargoReleaseDir = join(ENV.ci ? JUNO_ACTION_PROJECT_PATH : process.cwd(), 'target');
75+
76+
const cargoOutputPath = join(process.cwd(), 'target', cargoTarget, 'release');
77+
const cargoOutputWasm = join(cargoOutputPath, `${SATELLITE_PROJECT_NAME}.wasm`);
7478

7579
const args = [
7680
'build',
@@ -83,9 +87,22 @@ export const buildRust = async ({
8387
cargoReleaseDir
8488
];
8589

90+
const REMAP_PATH_PREFIX = '{REMAP_PATH_PREFIX}';
91+
92+
// ⚠️ To get the same fingerprints for the Cargo build, the order of the flags must be similar between the CLI and the Docker scripts.
93+
const baseRustFlags = `-A deprecated ${REMAP_PATH_PREFIX} -C link-args=-zstack-size=3000000 --cfg getrandom_backend="custom"`;
94+
95+
// In CI, RUSTFLAGS must exactly match those used during the pre-build step in the Docker image
96+
// (see ./docker/build-canister in the Juno repo). Any difference invalidates Cargo's fingerprints
97+
// and causes a full recompile of all dependencies. In other words, it would slow down the build.
98+
const rustFlags = baseRustFlags.replace(
99+
REMAP_PATH_PREFIX,
100+
ENV.ci ? ' --remap-path-prefix /home/apprunner/.cargo=/cargo' : ''
101+
);
102+
86103
const env = {
87104
...process.env,
88-
RUSTFLAGS: '--cfg getrandom_backend="custom" -A deprecated',
105+
RUSTFLAGS: rustFlags,
89106
...(target === 'wasm32-wasip1' && {DEV_SCRIPT_PATH: DEPLOY_SPUTNIK_SCRIPT_PATH})
90107
};
91108

@@ -108,6 +125,10 @@ export const buildRust = async ({
108125
return;
109126
}
110127

128+
if (ENV.ci) {
129+
await mkdir(cargoOutputPath, {recursive: true});
130+
}
131+
111132
switch (target) {
112133
case 'wasm32-wasip1': {
113134
spinner.text = 'Converting WASI to IC...';
@@ -130,6 +151,15 @@ export const buildRust = async ({
130151
break;
131152
}
132153
default: {
154+
if (ENV.ci) {
155+
// When build in the CI the satellite.wasm needs to be copied as if had been buildin the projects target
156+
// to comply with the next steps of the tooling
157+
await copyFile(
158+
join(cargoReleaseDir, cargoTarget, 'release', `${SATELLITE_PROJECT_NAME}.wasm`),
159+
cargoOutputWasm
160+
);
161+
}
162+
133163
await prepareJunoPkgForSatellite({buildType});
134164
}
135165
}

0 commit comments

Comments
 (0)