Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions packages/cli-repl/src/build-info.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os from 'os';
import { NodeDriverServiceProvider } from '@mongosh/service-provider-node-driver';
import { promises as fs } from 'fs';

export interface BuildInfo {
version: string;
nodeVersion: string;
distributionKind: 'unpackaged' | 'packaged' | 'compiled';
installationMethod: 'npx' | 'homebrew' | 'linux-system-wide' | 'other';
runtimeArch: (typeof process)['arch'];
runtimePlatform: (typeof process)['platform'];
buildArch: (typeof process)['arch'];
Expand Down Expand Up @@ -34,7 +36,36 @@ function getSystemArch(): (typeof process)['arch'] {
: process.arch;
}

export function baseBuildInfo(): Omit<BuildInfo, 'deps'> {
async function getInstallationMethod(
info: Pick<BuildInfo, 'distributionKind' | 'buildPlatform'>
): Promise<BuildInfo['installationMethod']> {
if (info.distributionKind !== 'compiled') {
if (
process.env.npm_lifecycle_event === 'npx' &&
process.env.npm_lifecycle_script?.includes('mongosh')
)
return 'npx';
if (
__filename.match(/\bhomebrew\b/i) &&
process.execPath.match(/\bhomebrew\b/i)
)
return 'homebrew';
} else {
if (
info.buildPlatform === 'linux' &&
process.execPath.startsWith('/usr/bin/') &&
(await fs.stat(process.execPath)).uid === 0
Comment on lines +56 to +57
Copy link

Copilot AI Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition checks if the file owner's UID is 0 (root), but this doesn't guarantee system-wide installation. Files in /usr/bin/ could be owned by root even in local installations. Consider checking if the current process has root privileges instead, or remove this condition since the path check may be sufficient.

Suggested change
process.execPath.startsWith('/usr/bin/') &&
(await fs.stat(process.execPath)).uid === 0
process.execPath.startsWith('/usr/bin/')

Copilot uses AI. Check for mistakes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ I don't think Copilot is making a lot of sense here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah seems like it thinks we're trying to check our own permissions or something 🤷

) {
return 'linux-system-wide'; // e.g. deb or rpm
}
}
return 'other';
}

export function baseBuildInfo(): Omit<
BuildInfo,
'deps' | 'installationMethod'
> {
const runtimeData = {
nodeVersion: process.version,
opensslVersion: process.versions.openssl,
Expand Down Expand Up @@ -86,7 +117,10 @@ export async function buildInfo({
if (!withSegmentApiKey) {
delete buildInfo.segmentApiKey;
}
return buildInfo;
return {
installationMethod: await getInstallationMethod(buildInfo),
...buildInfo,
};
}

let cachedGlibcVersion: string | undefined | null = null;
Expand Down
1 change: 1 addition & 0 deletions packages/e2e-tests/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('e2e', function () {

const data = JSON.parse(shell.output);
expect(Object.keys(data)).to.deep.equal([
'installationMethod',
'version',
'distributionKind',
'buildArch',
Expand Down