Skip to content

Commit a6108cc

Browse files
Justin ChungCopilot
andcommitted
Defer experimental features from MachineFolder; restore legacy scope semantics
Revert the experimental-feature enable/disable-set redesign and keep experimental features on the legacy config scopes so they behave exactly as on non-packaged installs. Reads use CurrentUser, falling back to AllUsers ($PSHOME); writes no longer redirect to the per-machine data store (allowMachineFolderRedirect: false), so -Scope AllUsers under MSIX fails against the read-only $PSHOME as it does today. This fixes the Get-ExperimentalFeature "user config takes precedence over system config" CI failure, where the redesign merged the system list even when a user config existed. MachineFolder support for experimental features is tracked in PowerShell#27702. Co-authored-by: Copilot <[email protected]> Copilot-Session: ab738c1b-4d18-491b-a40f-0515b586857b
1 parent 2fd862f commit a6108cc

1 file changed

Lines changed: 32 additions & 78 deletions

File tree

‎src/System.Management.Automation/engine/PSConfiguration.cs‎

Lines changed: 32 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ internal sealed class PowerShellConfig
5656
{
5757
private const string ConfigFileName = "powershell.config.json";
5858
private const string ExecutionPolicyDefaultShellKey = "Microsoft.PowerShell:ExecutionPolicy";
59-
private const string ExperimentalFeaturesKey = "ExperimentalFeatures";
60-
private const string DisabledExperimentalFeaturesKey = "DisabledExperimentalFeatures";
6159
private const string DisableImplicitWinCompatKey = "DisableImplicitWinCompat";
6260
private const string WindowsPowerShellCompatibilityModuleDenyListKey = "WindowsPowerShellCompatibilityModuleDenyList";
6361
private const string WindowsPowerShellCompatibilityNoClobberModuleListKey = "WindowsPowerShellCompatibilityNoClobberModuleList";
@@ -243,92 +241,40 @@ private static string GetExecutionPolicySettingKey(string shellId)
243241
/// </summary>
244242
internal string[] GetExperimentalFeatures()
245243
{
246-
// Resolve each experimental feature's state across the config scopes using explicit per-feature
247-
// overrides, so that:
248-
// - disables accumulate and never re-enable an earlier one,
249-
// - a newly-shipped ($PSHOME) feature stays at its product default until explicitly overridden, and
250-
// - an all-disabled state is not mistaken for "unset" (which previously fell back to $PSHOME).
251-
// Explicit enable/disable precedence: CurrentUser > MachineFolder (admin) > AllUsers ($PSHOME product).
252-
// A feature with no explicit setting defaults to the product ($PSHOME) enabled list (preview = on).
253-
HashSet<string> userEnabled = ReadFeatureSet(ConfigScope.CurrentUser, ExperimentalFeaturesKey);
254-
HashSet<string> userDisabled = ReadFeatureSet(ConfigScope.CurrentUser, DisabledExperimentalFeaturesKey);
255-
HashSet<string> machineEnabled = ReadFeatureSet(ConfigScope.MachineFolder, ExperimentalFeaturesKey);
256-
HashSet<string> machineDisabled = ReadFeatureSet(ConfigScope.MachineFolder, DisabledExperimentalFeaturesKey);
257-
HashSet<string> productEnabled = ReadFeatureSet(ConfigScope.AllUsers, ExperimentalFeaturesKey);
258-
259-
var universe = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
260-
universe.UnionWith(userEnabled);
261-
universe.UnionWith(userDisabled);
262-
universe.UnionWith(machineEnabled);
263-
universe.UnionWith(machineDisabled);
264-
universe.UnionWith(productEnabled);
265-
266-
var effective = new List<string>(universe.Count);
267-
foreach (string feature in universe)
244+
string[] features = ReadValueFromFile(ConfigScope.CurrentUser, "ExperimentalFeatures", Array.Empty<string>());
245+
246+
if (features.Length == 0)
268247
{
269-
bool enabled;
270-
if (userEnabled.Contains(feature)) { enabled = true; }
271-
else if (userDisabled.Contains(feature)) { enabled = false; }
272-
else if (machineEnabled.Contains(feature)) { enabled = true; }
273-
else if (machineDisabled.Contains(feature)) { enabled = false; }
274-
else { enabled = productEnabled.Contains(feature); }
275-
276-
if (enabled) { effective.Add(feature); }
248+
features = ReadValueFromFile(ConfigScope.AllUsers, "ExperimentalFeatures", Array.Empty<string>());
277249
}
278250

279-
return effective.ToArray();
280-
}
281-
282-
private HashSet<string> ReadFeatureSet(ConfigScope scope, string key)
283-
{
284-
return new HashSet<string>(
285-
ReadValueFromFile(scope, key, Array.Empty<string>()),
286-
StringComparer.OrdinalIgnoreCase);
251+
return features;
287252
}
288253

289254
/// <summary>
290-
/// Record an explicit per-feature override (enable or disable) for an experimental feature.
255+
/// Set the enabled list of experimental features in the config file.
291256
/// </summary>
292257
/// <param name="scope">The ConfigScope of the configuration file to update.</param>
293258
/// <param name="featureName">The name of the experimental feature to change in the configuration.</param>
294-
/// <param name="setEnabled">If true, explicitly enable the feature; otherwise explicitly disable it.</param>
259+
/// <param name="setEnabled">If true, add to configuration; otherwise, remove from configuration.</param>
295260
internal void SetExperimentalFeatures(ConfigScope scope, string featureName, bool setEnabled)
296261
{
297-
// Read and write the same (write-resolved) scope so overrides accumulate. On a packaged app,
298-
// AllUsers writes and reads both resolve to the per-machine data store, so disabling several
299-
// features in a row never re-enables an earlier one, and the shipped $PSHOME product config is
300-
// never rewritten. Enabling a feature clears any disable override for it (and vice versa).
301-
scope = ResolveWriteScope(scope);
302-
303-
var enabled = new List<string>(ReadValueFromFile(scope, ExperimentalFeaturesKey, Array.Empty<string>()));
304-
var disabled = new List<string>(ReadValueFromFile(scope, DisabledExperimentalFeaturesKey, Array.Empty<string>()));
305-
306-
List<string> addTo = setEnabled ? enabled : disabled;
307-
List<string> removeFrom = setEnabled ? disabled : enabled;
308-
309-
bool changed = removeFrom.RemoveAll(f => string.Equals(f, featureName, StringComparison.OrdinalIgnoreCase)) > 0;
310-
if (!addTo.Exists(f => string.Equals(f, featureName, StringComparison.OrdinalIgnoreCase)))
311-
{
312-
addTo.Add(featureName);
313-
changed = true;
314-
}
315-
316-
if (changed)
317-
{
318-
WriteOrRemoveFeatureSet(scope, ExperimentalFeaturesKey, enabled);
319-
WriteOrRemoveFeatureSet(scope, DisabledExperimentalFeaturesKey, disabled);
320-
}
321-
}
322-
323-
private void WriteOrRemoveFeatureSet(ConfigScope scope, string key, List<string> features)
324-
{
325-
if (features.Count > 0)
262+
// Experimental features intentionally stay on the legacy scopes ($PSHOME for AllUsers) and are not
263+
// redirected to the packaged per-machine data store; MachineFolder support for experimental features
264+
// is deferred to a separate change (see https://github.com/PowerShell/PowerShell/issues/27702).
265+
// This keeps behavior identical to non-packaged installs, so an AllUsers write under MSIX fails
266+
// against the read-only $PSHOME just as it does today.
267+
var features = new List<string>(GetExperimentalFeatures());
268+
bool containsFeature = features.Contains(featureName);
269+
if (setEnabled && !containsFeature)
326270
{
327-
WriteValueToFile<string[]>(scope, key, features.ToArray());
271+
features.Add(featureName);
272+
WriteValueToFile<string[]>(scope, "ExperimentalFeatures", features.ToArray(), allowMachineFolderRedirect: false);
328273
}
329-
else
274+
else if (!setEnabled && containsFeature)
330275
{
331-
RemoveValueFromFile<string[]>(scope, key);
276+
features.Remove(featureName);
277+
WriteValueToFile<string[]>(scope, "ExperimentalFeatures", features.ToArray(), allowMachineFolderRedirect: false);
332278
}
333279
}
334280

@@ -586,13 +532,20 @@ private static FileStream OpenFileStreamWithRetry(string fullPath, FileMode mode
586532
/// <param name="key">The string key of the value.</param>
587533
/// <param name="value">The value to set.</param>
588534
/// <param name="addValue">Whether the key-value pair should be added to or removed from the file.</param>
589-
private void UpdateValueInFile<T>(ConfigScope scope, string key, T value, bool addValue)
535+
/// <param name="allowMachineFolderRedirect">When true (default), system-wide (AllUsers) writes are redirected to the writable per-machine data store on a packaged install; pass false to write to the literal scope.</param>
536+
private void UpdateValueInFile<T>(ConfigScope scope, string key, T value, bool addValue, bool allowMachineFolderRedirect = true)
590537
{
591538
try
592539
{
593540
// Redirect system-wide writes to the writable per-machine data store when packaged, so only
594541
// the changed keys are stored there and the read-only $PSHOME product config is untouched.
595-
scope = ResolveWriteScope(scope);
542+
// Callers that must target the literal scope (e.g. experimental features, whose MachineFolder
543+
// support is deferred) pass allowMachineFolderRedirect: false.
544+
if (allowMachineFolderRedirect)
545+
{
546+
scope = ResolveWriteScope(scope);
547+
}
548+
596549
string fileName = GetConfigFilePath(scope);
597550
fileLock.EnterWriteLock();
598551

@@ -690,14 +643,15 @@ private void UpdateValueInFile<T>(ConfigScope scope, string key, T value, bool a
690643
/// <param name="scope">The ConfigScope of the file to update.</param>
691644
/// <param name="key">The string key of the value.</param>
692645
/// <param name="value">The value to write.</param>
693-
private void WriteValueToFile<T>(ConfigScope scope, string key, T value)
646+
/// <param name="allowMachineFolderRedirect">When true (default), system-wide (AllUsers) writes are redirected to the writable per-machine data store on a packaged install; pass false to write to the literal scope.</param>
647+
private void WriteValueToFile<T>(ConfigScope scope, string key, T value, bool allowMachineFolderRedirect = true)
694648
{
695649
if (scope == ConfigScope.CurrentUser && !Directory.Exists(perUserConfigDirectory))
696650
{
697651
Directory.CreateDirectory(perUserConfigDirectory);
698652
}
699653

700-
UpdateValueInFile<T>(scope, key, value, true);
654+
UpdateValueInFile<T>(scope, key, value, true, allowMachineFolderRedirect);
701655
}
702656

703657
/// <summary>

0 commit comments

Comments
 (0)