feat: add a warning that shows if user uses node -v <20 #2930 (#3371)

Co-authored-by: Jacob Richman <jacob314@gmail.com>
This commit is contained in:
Devansh Sharma 2025-07-15 11:49:46 +02:00 committed by GitHub
parent f5d5213504
commit 123c3e7c7f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { getUserStartupWarnings } from './userStartupWarnings.js'; import { getUserStartupWarnings } from './userStartupWarnings.js';
import * as os from 'os'; import * as os from 'os';
import fs from 'fs/promises'; import fs from 'fs/promises';
import semver from 'semver';
vi.mock('os', () => ({ vi.mock('os', () => ({
default: { homedir: vi.fn() }, default: { homedir: vi.fn() },
@ -18,6 +19,13 @@ vi.mock('fs/promises', () => ({
default: { realpath: vi.fn() }, default: { realpath: vi.fn() },
})); }));
vi.mock('semver', () => ({
default: {
major: vi.fn(),
},
major: vi.fn(),
}));
describe('getUserStartupWarnings', () => { describe('getUserStartupWarnings', () => {
const homeDir = '/home/user'; const homeDir = '/home/user';
@ -66,6 +74,44 @@ describe('getUserStartupWarnings', () => {
}); });
}); });
function setNodeVersionMajor(majorVersion: number) {
vi.mocked(semver.major).mockReturnValue(majorVersion);
}
describe('node version check', () => {
afterEach(() => {
setNodeVersionMajor(20);
});
it('should return a warning if Node.js version is less than minMajor', async () => {
setNodeVersionMajor(18);
const warnings = await getUserStartupWarnings('');
expect(warnings).toHaveLength(1);
expect(warnings[0]).toContain('Node.js');
expect(warnings[0]).toContain('requires Node.js 20 or higher');
});
it('should not return a warning if Node.js version is equal to minMajor', async () => {
setNodeVersionMajor(20);
const warnings = await getUserStartupWarnings('');
expect(warnings).toEqual([]);
});
it('should not return a warning if Node.js version is greater than minMajor', async () => {
setNodeVersionMajor(22);
const warnings = await getUserStartupWarnings('');
expect(warnings).toEqual([]);
});
it('should use default minMajor=20 if not provided', async () => {
setNodeVersionMajor(18);
const warnings = await getUserStartupWarnings('');
expect(warnings).toHaveLength(1);
expect(warnings[0]).toContain('Node.js');
expect(warnings[0]).toContain('requires Node.js 20 or higher');
});
});
// // Example of how to add a new check: // // Example of how to add a new check:
// describe('node version check', () => { // describe('node version check', () => {
// // Tests for node version check would go here // // Tests for node version check would go here

View File

@ -6,6 +6,7 @@
import fs from 'fs/promises'; import fs from 'fs/promises';
import * as os from 'os'; import * as os from 'os';
import semver from 'semver';
type WarningCheck = { type WarningCheck = {
id: string; id: string;
@ -32,8 +33,23 @@ const homeDirectoryCheck: WarningCheck = {
}, },
}; };
const nodeVersionCheck: WarningCheck = {
id: 'node-version',
check: async (_workspaceRoot: string) => {
const minMajor = 20;
const major = semver.major(process.versions.node);
if (major < minMajor) {
return `You are using Node.js v${process.versions.node}. Gemini CLI requires Node.js ${minMajor} or higher for best results.`;
}
return null;
},
};
// All warning checks // All warning checks
const WARNING_CHECKS: readonly WarningCheck[] = [homeDirectoryCheck]; const WARNING_CHECKS: readonly WarningCheck[] = [
homeDirectoryCheck,
nodeVersionCheck,
];
export async function getUserStartupWarnings( export async function getUserStartupWarnings(
workspaceRoot: string, workspaceRoot: string,