Address code review feedback

- Add error handling for file read operations (getOSReleaseInfo, getCurrentMirrorDomain)
- Fix trailing slash bug: add slash before replacement, not after
- Escape special regex characters in domain names to prevent incorrect replacements
- Fix OS detection order: check Ubuntu before Debian to avoid misidentification
- Add escapeRegExp helper function for regex escaping

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-01-29 11:38:54 +00:00
parent d43d563622
commit c329c8acd4

View File

@ -596,8 +596,13 @@ export function isDemoEnv() {
let osType: 'Debian' | 'Ubuntu' | 'Alpine' | undefined;
async function getOSReleaseInfo(): Promise<string> {
try {
const osRelease = await fs.readFile('/etc/os-release', 'utf8');
return osRelease;
} catch (error) {
Logger.error(`Failed to read /etc/os-release: ${error}`);
return '';
}
}
function isDebian(osReleaseInfo: string): boolean {
@ -620,10 +625,11 @@ export async function detectOS(): Promise<
if (platform === 'linux') {
const osReleaseInfo = await getOSReleaseInfo();
if (isDebian(osReleaseInfo)) {
osType = 'Debian';
} else if (isUbuntu(osReleaseInfo)) {
// Check Ubuntu before Debian since Ubuntu is based on Debian
if (isUbuntu(osReleaseInfo)) {
osType = 'Ubuntu';
} else if (isDebian(osReleaseInfo)) {
osType = 'Debian';
} else if (isAlpine(osReleaseInfo)) {
osType = 'Alpine';
} else {
@ -643,6 +649,7 @@ export async function detectOS(): Promise<
async function getCurrentMirrorDomain(
filePath: string,
): Promise<string | null> {
try {
const fileContent = await fs.readFile(filePath, 'utf8');
const lines = fileContent.split('\n');
for (const line of lines) {
@ -655,6 +662,14 @@ async function getCurrentMirrorDomain(
}
}
return null;
} catch (error) {
Logger.error(`Failed to read mirror configuration file ${filePath}: ${error}`);
return null;
}
}
function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
async function replaceDomainInFile(
@ -662,16 +677,19 @@ async function replaceDomainInFile(
oldDomainWithScheme: string,
newDomainWithScheme: string,
): Promise<void> {
let fileContent = await fs.readFile(filePath, 'utf8');
let updatedContent = fileContent.replace(
new RegExp(oldDomainWithScheme, 'g'),
newDomainWithScheme,
);
// Ensure the new domain has a trailing slash before replacement
if (!newDomainWithScheme.endsWith('/')) {
newDomainWithScheme += '/';
}
let fileContent = await fs.readFile(filePath, 'utf8');
// Escape special regex characters in the old domain
const escapedOldDomain = escapeRegExp(oldDomainWithScheme);
let updatedContent = fileContent.replace(
new RegExp(escapedOldDomain, 'g'),
newDomainWithScheme,
);
await writeFileWithLock(filePath, updatedContent);
}