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(build): catch spawn err #193

Merged
merged 4 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 34 additions & 9 deletions scripts/check-build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,57 @@ import cp from 'child_process';
import { existsSync } from 'fs';
import process from 'process';
import { target } from './binaries.mjs';
import { log } from 'console';

import { createRequire } from 'module';
const require = createRequire(import.meta.url);

function clean(err) {
return err.toString().trim();
}

function recompileFromSource() {
console.log('@sentry/profiling-node: Compiling from source...');
cp.spawnSync('npm', ['run', 'build:configure'], { env: process.env });
cp.spawnSync('npm', ['run', 'build:bindings'], { env: process.env });
cp.spawnSync('node', ['scripts/copy-target.mjs'], { env: process.env });
log('@sentry/profiling-node: Compiling from source...');
let spawn = cp.spawnSync('npm', ['run', 'build:configure'], {
stdio: ['inherit', 'inherit', 'pipe'],
env: process.env,
shell: true
});

if (spawn.status !== 0) {
log('@sentry/profiling-node: Failed to configure gyp');
log('@sentry/profiling-node:', clean(spawn.stderr));
return;
}

spawn = cp.spawnSync('npm', ['run', 'build:bindings'], {
stdio: ['inherit', 'inherit', 'pipe'],
env: process.env,
shell: true
});
if (spawn.status !== 0) {
log('@sentry/profiling-node: Failed to build bindings');
log('@sentry/profiling-node:', clean(spawn.stderr));
return;
}
}

if (existsSync(target)) {
try {
console.log(`@sentry/profiling-node: Precompiled binary found, attempting to load ${target}`);
log(`@sentry/profiling-node: Precompiled binary found, attempting to load ${target}`);
require(target);
console.log('@sentry/profiling-node: Precompiled binary found, skipping build from source.');
log('@sentry/profiling-node: Precompiled binary found, skipping build from source.');
} catch (e) {
console.log('@sentry/profiling-node: Precompiled binary found but failed loading');
log('@sentry/profiling-node: Precompiled binary found but failed loading');
log('@sentry/profiling-node:', e);
try {
recompileFromSource();
} catch (e) {
console.log('@sentry/profiling-node: Failed to compile from source');
log('@sentry/profiling-node: Failed to compile from source');
throw e;
}
}
} else {
console.log('@sentry/profiling-node: No precompiled binary found');
log('@sentry/profiling-node: No precompiled binary found');
recompileFromSource();
}
8 changes: 4 additions & 4 deletions scripts/copy-target.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { existsSync, mkdirSync, renameSync } from 'fs';
import path from 'path';
import { fileURLToPath, URL } from 'url';
import { exit } from 'process';

import { getModuleName } from './binaries.mjs';
import { log } from 'console';

const __dirname = fileURLToPath(new URL('.', import.meta.url));
const lib = path.resolve(__dirname, '..', 'lib');
Expand All @@ -16,12 +16,12 @@ const source = path.join(__dirname, '..', 'build', 'Release', 'sentry_cpu_profil
const target = path.join(__dirname, '..', 'lib', getModuleName());

if (!existsSync(source)) {
console.log('Source file does not exist:', source);
log('Source file does not exist:', source);
exit(1);
} else {
if (existsSync(target)) {
console.log('Target file already exists, overwriting it');
log('Target file already exists, overwriting it');
}
console.log('Renaming', source, 'to', target);
log('Renaming', source, 'to', target);
renameSync(source, target);
}