Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix/Refactor: missing siblingVersion from sibling check #829

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
> [migration guide](https://docs.sentry.io/platforms/javascript/guides/capacitor/migration/) first.
<!-- prettier-ignore-end -->

## Unreleased

### Fixes

- Undefined siblingVersion error when updating the Sentry Capacitor Package ([#829](https://github.com/getsentry/sentry-capacitor/pull/829))

## 1.1.0

### Features
Expand Down
60 changes: 40 additions & 20 deletions scripts/check-siblings.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const fs = require('fs');
const path = require('path');
const { env } = require('process');
const { env, exit } = require('process');

const updateArgument = '--update-sentry-capacitor';

// Filters all Sentry packages but Capacitor, CLI and Wizard.
const jsonFilter = /\s*\"\@sentry\/(?!capacitor|wizard|cli|typescript|electron)(?<packageName>[a-zA-Z]+)\"\:\s*\"(?<version>.+)\"/;

const IsE2E = env.sentry_e2e == 'true';

function LogE2E(message) {
IsE2E && console.log("E2E_TEST: " + message);
}

lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
/**
* If user requested to ignore the post-install
* @return {Boolean} true if requested to skip the post-install check, false otherwise.
Expand All @@ -29,22 +35,17 @@ function SkipPostInstall() {
*/
function GetRequiredSiblingVersion() {
if (env.npm_package_dependencies__sentry_browser) {
// Yarn.
// Yarn V1.
return env.npm_package_dependencies__sentry_browser;
}

let capacitorPackagePath = '';
if (env.npm_package_json) {
// NPM.
capacitorPackagePath = env.npm_package_json;
}
else if (__dirname) {
if (__dirname) {
capacitorPackagePath = path.join(__dirname, '..', 'package.json');
}
else {
return undefined;
}

const capacitorPackageJson = fs.readFileSync(capacitorPackagePath, 'utf8');

const version = capacitorPackageJson.match(jsonFilter);
Expand All @@ -59,7 +60,7 @@ function GetRequiredSiblingVersion() {
* This function will throw if the paramater contains a sibling with different version to the one used
* by the SDK or if no version were specified by the user.
*/
function ValidateSentryPackageParameters(packages) {
function ValidateSentryPackageParameters(packages, siblingVersion) {
let errorMessages = [];
var packageFilter = /.*(capacitor|cli|wizard|typescript)/;
for (const argPackage of packages) {
Expand All @@ -75,18 +76,28 @@ function ValidateSentryPackageParameters(packages) {
}

if (errorMessages.length > 0) {
throw errorMessages.join("\n");
LogE2E("Incompatibility found");
console.error(`⚠️ ${errorMessages.join("\n")}`);
exit(1);
}
}

/**
* @return {String} The path where package.json is located.
*/
function GetPackageJsonRootPath() {

// Avaliable when using NPM.
if (env.INIT_CWD) {
// Avaliable when using NPM.
return env.INIT_CWD + '/';
}

// Unix only.
if (env.PWD) {
return env.PWD + '/';
}

let packagePath = __dirname + '/../../';
while (!fs.existsSync(path.resolve(packagePath, 'package.json'))) {
packagePath += '../';
Expand All @@ -99,8 +110,8 @@ function GetPackageJsonRootPath() {
* @return {String} The path where package.json is located.
*/
function FormatPackageInstallCommand(sentryPackages) {
// Yarn
if (env.npm_config_argv) {
// Yarn V1 || Yarn V3/V4.
if (env.npm_config_argv || env.npm_config_user_agent?.startsWith('yarn')) {
return "yarn add --exact " + sentryPackages + " " + updateArgument;
}
else {
Expand All @@ -110,22 +121,26 @@ function FormatPackageInstallCommand(sentryPackages) {
}

function CheckSiblings() {

LogE2E("Invoked");
if (SkipPostInstall()) {
LogE2E("Skipped");
return;
}

const siblingVersion = GetRequiredSiblingVersion();
if (siblingVersion === undefined) {
LogE2E("Sibling not set");
return;
}
LogE2E(`Expecting sibling version ${siblingVersion}`);

// Method 1: Validate user parameters when requesting to install/update a new Package.
if (env.npm_config_argv) {
// Only available on Yarn.
const npmAction = JSON.parse(env.npm_config_argv);
if (npmAction.original && npmAction.original.length > 1) {
ValidateSentryPackageParameters(npmAction.original);
ValidateSentryPackageParameters(npmAction.original, siblingVersion);
LogE2E("OK");
return;
}
}
Expand All @@ -134,25 +149,30 @@ function CheckSiblings() {
let rootPath = GetPackageJsonRootPath();
let incompatiblePackages = [];
const packageJson = fs.readFileSync(rootPath + 'package.json', 'utf8').split("\n");

for (const lineData of packageJson) {
let sentryRef = lineData.match(jsonFilter);
if (sentryRef && sentryRef[2] !== siblingVersion && !sentryRef[2].includes('%3A' + siblingVersion + '#')) {
incompatiblePackages.push(['@sentry/' + sentryRef[1], sentryRef[2]]);
}
}
if (incompatiblePackages.length > 0) {
const IncompatibilityError = ["This version of Sentry Capacitor is incompatible with the following installed packages: "];
const IncompatibilityError = ["This version of Sentry Capacitor is incompatible with the following installed packages:"];
let packagesList = ''
for (const sentryPackage of incompatiblePackages) {
IncompatibilityError.push(sentryPackage[0] + ' version ' + sentryPackage[1]);
packagesList += sentryPackage[0] + '@' + siblingVersion + ' ';
}
IncompatibilityError.push("Please install the mentioned packages exactly with version " + siblingVersion + " and with the argument " + updateArgument);
IncompatibilityError.push(FormatPackageInstallCommand(packagesList));
throw IncompatibilityError.join("\n");
}
IncompatibilityError.push(
`Please install the mentioned packages exactly with version ${siblingVersion} and with the argument ${updateArgument}.
Your project will build with the wrong package but you may face Runtime errors.
You can use the below command to fix your package.json:`);
LogE2E("Incompatibility found");

console.error(`⚠️ ${IncompatibilityError.join("\n")}`);
console.warn(` ${FormatPackageInstallCommand(packagesList)}`);
exit(1);
}
LogE2E("OK");
}

CheckSiblings();
Loading