Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs (C#)

Code before:

            if (newPaths.Length is 0)
            {
                // The 'pathToAdd' doesn't really contain any paths to add.
                return basePath;
            }

            var result = new StringBuilder(basePath);
            var addedPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
            string[] initialPaths = basePath.Split(
                Path.PathSeparator,
                StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

            foreach (string p in initialPaths)
            {
                // Remove the trailing directory separators.
                // Trailing white spaces were already removed by 'StringSplitOptions.TrimEntries'.
                addedPaths.Add(NormalizePath(p));
            }

            foreach (string subPathToAdd in newPaths)
            {
                // Remove the trailing directory separators.
                // Trailing white spaces were already removed by 'StringSplitOptions.TrimEntries'.
                string normalizedPath = NormalizePath(subPathToAdd);
                if (addedPaths.Contains(normalizedPath))
                {
                    // The normalized sub path was already added - skip it.
                    continue;
                }

                // The normalized sub path was not found - add it.
                if (insertPosition is -1 || insertPosition >= result.Length)
                {
                    // Append the normalized sub path to the end.
                    if (result.Length > 0 && result[^1] != Path.PathSeparator)
                    {
                        result.Append(Path.PathSeparator);
                    }

                    result.Append(normalizedPath);
                    // Next insertion should happen at the end.
                    insertPosition = result.Length;
                }
                else
                {
                    // Insert at the requested location.
                    // This is used by the user-specific module path, the shared module path (<Program Files> location), and the PSHome module path.
                    string strToInsert = normalizedPath + Path.PathSeparator;
                    result.Insert(insertPosition, strToInsert);


Review comment (suggestion):
We could remove line 1158 if we used Add():
```suggestion
                if (!addedPaths.Add(normalizedPath))
```

Code after:

            if (newPaths.Length is 0)
            {
                // The 'pathToAdd' doesn't really contain any paths to add.
                return basePath;
            }

            var result = new StringBuilder(basePath, capacity: basePath.Length + pathToAdd.Length + newPaths.Length);
            var addedPaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
            string[] initialPaths = basePath.Split(
                Path.PathSeparator,
                StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

            foreach (string p in initialPaths)
            {
                // Remove the trailing directory separators.
                // Trailing white spaces were already removed by 'StringSplitOptions.TrimEntries'.
                addedPaths.Add(Path.TrimEndingDirectorySeparator(p));
            }

            foreach (string subPathToAdd in newPaths)
            {
                // Remove the trailing directory separators.
                // Trailing white spaces were already removed by 'StringSplitOptions.TrimEntries'.
                string normalizedPath = Path.TrimEndingDirectorySeparator(subPathToAdd);
                if (addedPaths.Contains(normalizedPath))
                {
                    // The normalized sub path was already added - skip it.
                    continue;
                }

                // The normalized sub path was not found - add it.
                if (insertPosition is -1 || insertPosition >= result.Length)
                {
                    // Append the normalized sub path to the end.
                    if (result.Length > 0 && result[^1] != Path.PathSeparator)
                    {
                        result.Append(Path.PathSeparator);
                    }

                    result.Append(normalizedPath);
                    // Next insertion should happen at the end.
                    insertPosition = result.Length;
                }
                else
                {
                    // Insert at the requested location.
                    // This is used by the user-specific module path, the shared module path (<Program Files> location), and the PSHome module path.
                    string strToInsert = normalizedPath + Path.PathSeparator;
                    result.Insert(insertPosition, strToInsert);