Fix #1555: Ensure working directory is prepared when adding new subscription

When adding a new subscription that becomes the current profile with
'diffWorkDir' enabled, 'generateProfile()' was not being called to
prepare the profile's working directory before the core was restarted.

This caused the working directory to be empty and missing necessary
database files (geoip, geosite, etc.), leading to proxies appearing
to work (showing green) but actually failing to function properly.

The fix ensures that when a new profile is added and becomes current
with 'diffWorkDir' enabled, 'generateProfile()' is explicitly called
before 'changeCurrentProfile()' to prepare the working directory.

Co-authored-by: Your Name <your-email@example.com>
Co-authored-by: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
Daniel Bates 2026-03-17 20:12:19 -07:00 committed by GitHub
parent 34fc4d2d96
commit 5d4c0cbfb9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -107,6 +107,7 @@ export async function updateProfileItem(item: IProfileItem): Promise<void> {
export async function addProfileItem(item: Partial<IProfileItem>): Promise<void> {
const newItem = await createProfile(item)
let shouldChangeCurrent = false
let newProfileIsCurrentAfterUpdate = false
await updateProfileConfig((config) => {
const existingIndex = config.items.findIndex((i) => i.id === newItem.id)
if (existingIndex !== -1) {
@ -116,10 +117,25 @@ export async function addProfileItem(item: Partial<IProfileItem>): Promise<void>
}
if (!config.current) {
shouldChangeCurrent = true
newProfileIsCurrentAfterUpdate = true
}
return config
})
// If the new profile will become the current profile, ensure generateProfile is called
// to prepare working directory before restarting core
if (newProfileIsCurrentAfterUpdate) {
const { diffWorkDir } = await getAppConfig()
if (diffWorkDir) {
try {
const { generateProfile } = await import('../core/factory')
await generateProfile()
} catch (error) {
profileLogger.warn('Failed to generate profile for new subscription', error)
}
}
}
if (shouldChangeCurrent) {
await changeCurrentProfile(newItem.id)
}