Repository: dotnet/roslyn
File: src/Compilers/Core/MSBuildTask/ManagedCompiler.cs (C#)

Code before:
                return sources;

            // Create a copy of the array with transformed paths
            var transformedSources = new ITaskItem[sources.Length];
            for (int i = 0; i < sources.Length; i++)
            {
                var source = sources[i];
                if (source.ItemSpec.StartsWith("/") && !source.ItemSpec.StartsWith("/./"))
                {
                    // Create a simple wrapper that returns the transformed path
                    transformedSources[i] = new TransformedTaskItem(source, "/." + source.ItemSpec);
                }
                else
                {
                    transformedSources[i] = source;
                }
            }

            return transformedSources;
        }

        /// <summary>
        /// A wrapper around ITaskItem that returns a transformed ItemSpec but delegates
        /// all other operations to the underlying item.
        /// </summary>
        private class TransformedTaskItem : ITaskItem
        {
            private readonly ITaskItem _underlyingItem;
            private readonly string _transformedItemSpec;

            public TransformedTaskItem(ITaskItem underlyingItem, string transformedItemSpec)
            {
                _underlyingItem = underlyingItem;
                _transformedItemSpec = transformedItemSpec;
            }

            public string ItemSpec
            {
                get => _transformedItemSpec;
                set => throw new NotSupportedException();
            }

            public System.Collections.ICollection MetadataNames => _underlyingItem.MetadataNames;
            public int MetadataCount => _underlyingItem.MetadataCount;

            public string GetMetadata(string metadataName) => _underlyingItem.GetMetadata(metadataName);
            public void SetMetadata(string metadataName, string metadataValue) => _underlyingItem.SetMetadata(metadataName, metadataValue);
            public void RemoveMetadata(string metadataName) => _underlyingItem.RemoveMetadata(metadataName);
            public void CopyMetadataTo(ITaskItem destinationItem) => _underlyingItem.CopyMetadataTo(destinationItem);
            public System.Collections.IDictionary CloneCustomMetadata() => _underlyingItem.CloneCustomMetadata();
        }

Review comment (suggestion):
```suggestion
        private sealed class TransformedTaskItem : ITaskItem
```

Code after:

            // Go over sources once and lazily initialize transformedSources if needed
            string[]? transformedSources = null;
            for (int i = 0; i < sources.Length; i++)
            {
                var itemSpec = sources[i].ItemSpec;

                // Check if this path needs transformation using the compiler's heuristic:
                // A path starting with '/' is treated as an option unless it contains 
                // another '/' after the first character (e.g., "/dir/file.cs" is safe)
                if (itemSpec.Length > 1 && itemSpec[0] == '/' && itemSpec.IndexOf('/', 1) < 0)
                {
                    // Lazy initialization: create the array on first transformation
                    if (transformedSources == null)
                    {
                        transformedSources = new string[sources.Length];
                        // Copy all items processed so far
                        for (int j = 0; j < i; j++)
                        {
                            transformedSources[j] = sources[j].ItemSpec;
                        }
                    }

                    // Transform this path to prevent misinterpretation as an option
                    transformedSources[i] = "/." + itemSpec;
                }
                else if (transformedSources != null)
                {
                    // If we've already started transforming, copy this item as-is
                    transformedSources[i] = itemSpec;
                }
            }

            return transformedSources;
        }

        /// <summary>
        /// Whether the command line compiler was invoked, instead
        /// of the host object compiler.
        /// </summary>
        protected bool UsedCommandLineTool
        {
            get;
            set;
        }

        private bool _hostCompilerSupportsAllParameters;
        protected bool HostCompilerSupportsAllParameters
        {
            get { return _hostCompilerSupportsAllParameters; }
            set { _hostCompilerSupportsAllParameters = value; }