Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsModel/CascadiaSettingsSerialization.cpp (C++)

Code before:
            std::mutex mtx2;
            Windows::Storage::StorageFolder foundFolder{ nullptr };

            auto findPublicFolder = [&]() -> winrt::fire_and_forget {
                co_await resume_background();
                const auto localFolder = ext.GetPublicFolderAsync().get();
                std::unique_lock<std::mutex> lock{ mtx2 };
                foundFolder = localFolder;
                cv2.notify_all();
            };

            std::unique_lock<std::mutex> lock2{ mtx2 };
            findPublicFolder();
            cv2.wait(lock2);

            // the StorageFolder class has its own methods for obtaining the files within the folder
            // however, all those methods are Async methods
            // you may have noticed that we need to resort to clunky implementations for async operations
            // (they're right above)
            // so for now we will just take the folder path and access the files that way
            auto path = winrt::to_string(foundFolder.Path());
            path.append(FragmentsSubDirectory);
            const auto jsonFiles = _AccumulateJsonFilesInDirectory(til::u8u16(path));

            // Provide the package name as the source
            _AddOrModifyProfiles(jsonFiles, ext.Package().DisplayName().c_str());
        }
    }
}

// Method Description:
// - Helper function to apply json stubs in the local app data folder and the global program data folder
// Arguments:
// - The directory to find json files in
// - The set of ignored namespaces
void CascadiaSettings::_ApplyJsonStubsHelper(const std::wstring_view directory, const std::unordered_set<std::wstring>& ignoredNamespaces)
{
    const std::filesystem::path root{ wil::ExpandEnvironmentStringsW<std::wstring>(directory.data()) };

    // The json files should be within subdirectories where the subdirectory name is the app name
    for (const auto& protoExtFolder : std::filesystem::directory_iterator(root))
    {
        // We only want the parent folder name as the source (not the full path)
        const auto source = protoExtFolder.path().filename().wstring();

        // Only apply the stubs if the parent folder name is not in ignored namespaces
        // (also make sure this is a directory for sanity)
        if (std::filesystem::is_directory(protoExtFolder) && ignoredNamespaces.find(source) == ignoredNamespaces.end())
        {
            const auto jsonFiles = _AccumulateJsonFilesInDirectory(protoExtFolder.path().c_str());
            _AddOrModifyProfiles(jsonFiles, winrt::hstring{ source });

Review comment (suggestion):
display name might not be the right choice here. Prefer package family name (since it is required to be unique for the package _and_ it cannot be changed on an update)

Code after:
    // Search through the program data folder
    wil::unique_cotaskmem_string programDataFolder;
    THROW_IF_FAILED(SHGetKnownFolderPath(FOLDERID_ProgramData, 0, nullptr, &programDataFolder));
    auto programDataFragments = std::wstring(programDataFolder.get()) + FragmentsPath.data();
    if (std::filesystem::exists(programDataFragments))
    {
        _ApplyJsonStubsHelper(programDataFragments, ignoredNamespaces);
    }

    // Search through app extensions
    // Gets the catalog of extensions with the name "com.microsoft.windows.terminal.settings"
    const auto catalog = Windows::ApplicationModel::AppExtensions::AppExtensionCatalog::Open(winrt::to_hstring(AppExtensionHostName));

    auto extensions = _extractValueFromTaskWithoutMainThreadAwait(catalog.FindAllAsync());

    for (const auto& ext : extensions)
    {
        // Only apply the stubs if the package name is not in ignored namespaces
        if (ignoredNamespaces.find(ext.Package().Id().FamilyName().c_str()) == ignoredNamespaces.end())
        {
            // Likewise, getting the public folder from an extension is an async operation
            // So we use another mutex and condition variable
            auto foundFolder = _extractValueFromTaskWithoutMainThreadAwait(ext.GetPublicFolderAsync());

            // the StorageFolder class has its own methods for obtaining the files within the folder
            // however, all those methods are Async methods
            // you may have noticed that we need to resort to clunky implementations for async operations
            // (they are in _extractValueFromTaskWithoutMainThreadAwait)
            // so for now we will just take the folder path and access the files that way
            auto path = winrt::to_string(foundFolder.Path());
            path.append(FragmentsSubDirectory);

            // If the directory exists, use the fragments in it
            if (std::filesystem::exists(path))
            {
                const auto jsonFiles = _AccumulateJsonFilesInDirectory(til::u8u16(path));

                // Provide the package name as the source
                _ParseAndLayerFragmentFiles(jsonFiles, ext.Package().Id().FamilyName().c_str());
            }
        }
    }
}

// Method Description:
// - Helper function to apply json stubs in the local app data folder and the global program data folder
// Arguments:
// - The directory to find json files in
// - The set of ignored namespaces
void CascadiaSettings::_ApplyJsonStubsHelper(const std::wstring_view directory, const std::unordered_set<std::wstring>& ignoredNamespaces)
{