diff --git a/documentation/specs/directory-parse-config.md b/documentation/specs/directory-parse-config.md new file mode 100644 index 00000000000..900620c9459 --- /dev/null +++ b/documentation/specs/directory-parse-config.md @@ -0,0 +1,133 @@ +# Directory.Parse.config — Ignoring Unexpected Attributes and Elements + +## Summary + +MSBuild now supports a configuration file (`Directory.Parse.config`) that allows specific unrecognized attributes or child elements to be silently skipped during project parsing, instead of throwing `InvalidProjectFileException` (MSB4066/MSB4067). + +This enables compatibility scenarios where project files may include attributes/elements intended for newer MSBuild versions or third-party tools that can still build without those attributes/elements. + +## Configuration File Format + +`Directory.Parse.config` is an XML file: + +```xml + + + + + + + + + + +``` + +- **Root element**: `` +- **``**: Contains `` entries for attributes to skip. Appears 0 or 1 times. +- **``**: Contains `` entries for child elements to skip. Appears 0 or 1 times. +- **``**: Each entry has `Element` (the parent/owner element name) and `Name` (the attribute or child to allow). + +Matching is case-insensitive. Entries with missing or empty `Element`/`Name` attributes are silently ignored. Unrecognized sections or elements are ignored. + +### Valid `IgnoreAttributes` Elements + +The `Element` attribute in `` entries can be: +- `Target` +- `PropertyGroup` +- `ItemGroup` +- `Import` +- `ImportGroup` +- `UsingTask` +- `OnError` +- `Output` +- `Choose` +- `Otherwise` +- `ProjectExtensions` + +Generic names for dynamically-named elements: + +| Generic Name | Applies To | +|---|---| +| `Property` | Any property element (child of `PropertyGroup`) | +| `Item` | Any item element (child of `ItemGroup`) | +| `ItemDefinition` | Any item definition element (child of `ItemDefinitionGroup`) | +| `Metadata` | Any metadata element (child of an `Item` or `ItemDefinition`) | +| `Parameter` | Any parameter element in a `ParameterGroup` inside `UsingTask` | +| `UsingTaskBody` | The `Task` element inside `UsingTask` | + +Note: `Item` only applies to attributes with leading underscores as any other attributes are interpreted as metadata names. + +### Valid `IgnoreChildren` Elements + +The `Element` attribute in `` entries can be: +- `Project` +- `Import` +- `ImportGroup` +- `Task` +- `UsingTask` +- `OnError` +- `Output` +- `Choose` +- `When` +- `Otherwise` + +Not applicable (children are free-form by design): + +- `Target` +- `PropertyGroup` +- `ItemGroup` +- `ItemDefinitionGroup` +- `ProjectExtensions` + +## Discovery and Loading + +Configuration is discovered from two sources: + +1. **`MSBUILD_PARSE_CONFIG` environment variable** — Semicolon-separated (Windows) or colon-separated (Unix) list of config file paths. Always loaded when a `ProjectCollection` is created. + +2. **Project directory walk** — The CLI walks up from the target project file's directory looking for `Directory.Parse.config` (same pattern as `Directory.Build.rsp`). This is passed as `parseConfigDirectory` to the `ProjectCollection` constructor. + +These are merged additively — entries from either source are combined. + +## Integration + +The `ProjectCollection` constructor accepts an optional `parseConfigDirectory` parameter. When provided, it walks up from that directory to find a `Directory.Parse.config` and merges it with the environment variable config. XMake passes the project file's directory. + +The `ParserIgnoreConfiguration` property on `ProjectCollection` and `BuildParameters` is `internal`. External hosts that need to configure parse rules should use the constructor parameter or the environment variable. + +## Logging + +- **Config files loaded**: Logged at evaluation start (`MessageImportance.Low`). Lists all loaded config file paths. +- **Config files embedded**: Each loaded config file is embedded in the binary log archive (visible as an embedded file, not as a project import). +- **Skipped items summary**: Logged after each evaluation completes (`MessageImportance.Low`). Lists skipped attributes/elements with occurrence counts for that evaluation. + +## Feature Flag + +Set `MSBUILD_DISABLE_PARSE_CONFIG=1` to disable all automatic config loading. + +## Examples + +### Allow a custom attribute on all Target elements + +```xml + + + + + +``` + +Allows `` without error. + +### Allow a custom child element under Project + +```xml + + + + + +``` + +Allows `` as a direct child of `` without error. diff --git a/src/Build.UnitTests/Evaluation/ParserIgnoreConfiguration_Tests.cs b/src/Build.UnitTests/Evaluation/ParserIgnoreConfiguration_Tests.cs new file mode 100644 index 00000000000..af001490354 --- /dev/null +++ b/src/Build.UnitTests/Evaluation/ParserIgnoreConfiguration_Tests.cs @@ -0,0 +1,284 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Generic; +using System.IO; +using Microsoft.Build.Exceptions; +using Microsoft.Build.Evaluation; +using Shouldly; +using Xunit; + + +namespace Microsoft.Build.UnitTests.Evaluation +{ + public class ParserIgnoreConfiguration_Tests : IDisposable + { + private readonly string _testDir; + + public ParserIgnoreConfiguration_Tests() + { + _testDir = Path.Combine(Path.GetTempPath(), "MSBuildTest_UnknownElements_" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(_testDir); + } + + public void Dispose() + { + if (Directory.Exists(_testDir)) + { + Directory.Delete(_testDir, recursive: true); + } + } + + [Fact] + public void ParsesValidConfigFile() + { + string configPath = Path.Combine(_testDir, ParserIgnoreConfiguration.ConfigFileName); + File.WriteAllText(configPath, @" + + + + + + + +"); + + var config = ParserIgnoreConfiguration.LoadFromFile(configPath); + + config.LoadedConfigFiles.Count.ShouldBe(1); + config.CheckSkipAttribute("Target", "Foo").ShouldBeTrue(); + config.CheckSkipElement("Project", "CustomThing").ShouldBeTrue(); + config.CheckSkipAttribute("PropertyGroup", "Bar").ShouldBeTrue(); + } + + [Fact] + public void IsCaseInsensitive() + { + string configPath = Path.Combine(_testDir, ParserIgnoreConfiguration.ConfigFileName); + File.WriteAllText(configPath, @""); + + var config = ParserIgnoreConfiguration.LoadFromFile(configPath); + + config.CheckSkipAttribute("target", "foo").ShouldBeTrue(); + config.CheckSkipAttribute("TARGET", "FOO").ShouldBeTrue(); + config.CheckSkipAttribute("Target", "Foo").ShouldBeTrue(); + } + + [Fact] + public void IgnoresInvalidEntries() + { + string configPath = Path.Combine(_testDir, ParserIgnoreConfiguration.ConfigFileName); + File.WriteAllText(configPath, @" + + + + + + + + + + + +"); + + var config = ParserIgnoreConfiguration.LoadFromFile(configPath); + + config.CheckSkipAttribute("Target", "ValidOne").ShouldBeTrue(); + config.CheckSkipAttribute("Target", "Foo").ShouldBeFalse(); + config.CheckSkipAttribute("Target", "Bar").ShouldBeFalse(); + config.CheckSkipAttribute("Target", "Nope").ShouldBeFalse(); + config.CheckSkipAttribute("Target", "Nope2").ShouldBeFalse(); + } + + [Fact] + public void ReturnsEmptyWhenNoConfigExists() + { + var config = ParserIgnoreConfiguration.LoadFromFile(Path.Combine(_testDir, ParserIgnoreConfiguration.ConfigFileName)); + + config.LoadedConfigFiles.Count.ShouldBe(0); + config.CheckSkipAttribute("Target", "Foo").ShouldBeFalse(); + } + + [Fact] + public void MergeCombinesEntriesAndDeduplicatesFiles() + { + string configPath = Path.Combine(_testDir, ParserIgnoreConfiguration.ConfigFileName); + File.WriteAllText(configPath, @""); + + string extraConfigPath = Path.Combine(_testDir, "extra.config"); + File.WriteAllText(extraConfigPath, @""); + + var merged = ParserIgnoreConfiguration.Merge( + ParserIgnoreConfiguration.LoadFromFile(configPath), + ParserIgnoreConfiguration.LoadFromFile(extraConfigPath)); + merged = ParserIgnoreConfiguration.Merge(merged, ParserIgnoreConfiguration.LoadFromFile(configPath)); + + merged.CheckSkipAttribute("Target", "Foo").ShouldBeTrue(); + merged.CheckSkipElement("Project", "CustomThing").ShouldBeTrue(); + merged.LoadedConfigFiles.Count.ShouldBe(2); + } + + [Fact] + public void LoadGlobalConfigLoadsEnvironmentVariablePaths() + { + string envConfigPath = Path.Combine(_testDir, "env.config"); + File.WriteAllText(envConfigPath, @""); + + string? oldEnv = Environment.GetEnvironmentVariable(ParserIgnoreConfiguration.EnvironmentVariableName); + try + { + Environment.SetEnvironmentVariable(ParserIgnoreConfiguration.EnvironmentVariableName, envConfigPath); + var config = ParserIgnoreConfiguration.LoadGlobalConfig(); + + config.CheckSkipElement("Project", "CustomThing").ShouldBeTrue(); + } + finally + { + Environment.SetEnvironmentVariable(ParserIgnoreConfiguration.EnvironmentVariableName, oldEnv); + } + } + + [Fact] + public void ConfigurationIsAppliedToSimpleProjectRootElementCache() + { + string envConfigPath = Path.Combine(_testDir, "env.config"); + File.WriteAllText(envConfigPath, @""); + + using TestEnvironment env = TestEnvironment.Create(); + env.SetEnvironmentVariable("MsBuildUseSimpleProjectRootElementCacheConcurrency", "true"); + env.SetEnvironmentVariable(ParserIgnoreConfiguration.EnvironmentVariableName, envConfigPath); + + using var projectCollection = new ProjectCollection(); + + projectCollection.ProjectRootElementCache.ShouldBeOfType(); + projectCollection.ParserIgnoreConfiguration.CheckSkipElement("Project", "CustomThing").ShouldBeTrue(); + + // Replacing the configuration invalidates what the cache parsed under the old one, which the simple + // cache has to be able to do as well. + projectCollection.ParserIgnoreConfiguration = ParserIgnoreConfiguration.Empty; + projectCollection.ParserIgnoreConfiguration.CheckSkipElement("Project", "CustomThing").ShouldBeFalse(); + } + + [Fact] + public void RecordsSkippedItems() + { + string configPath = Path.Combine(_testDir, ParserIgnoreConfiguration.ConfigFileName); + File.WriteAllText(configPath, @""); + + var config = ParserIgnoreConfiguration.LoadFromFile(configPath); + + config.GetSkippedSummaryMessage().ShouldBeNull(); + + config.CheckSkipAttribute("Target", "Foo"); + config.CheckSkipAttribute("Target", "Foo"); + + string? summary = config.GetSkippedSummaryMessage(); + summary.ShouldNotBeNull(); + summary.ShouldContain("Attribute:Target:Foo"); + summary.ShouldContain("2 occurrences"); + } + + [Theory] + [MemberData(nameof(AttributeSkipCases))] + public void AllowedAttributeIsSkipped(string configElement, string configName, string projectXml) + { + string configPath = Path.Combine(_testDir, ParserIgnoreConfiguration.ConfigFileName); + File.WriteAllText(configPath, $@""); + string projectFile = Path.Combine(_testDir, "test.proj"); + File.WriteAllText(projectFile, projectXml); + + using (var pc = CreateProjectCollection(ParserIgnoreConfiguration.LoadFromFile(configPath))) + { + var project = new Project(projectFile, null, null, pc, ProjectLoadSettings.IgnoreMissingImports | ProjectLoadSettings.IgnoreEmptyImports | ProjectLoadSettings.IgnoreInvalidImports); + project.ShouldNotBeNull(); + } + } + + [Theory] + [MemberData(nameof(AttributeSkipCases))] + public void NonAllowedAttributeThrows(string configElement, string configName, string projectXml) + { + _ = configElement; + _ = configName; + string projectFile = Path.Combine(_testDir, "test.proj"); + File.WriteAllText(projectFile, projectXml); + + using (var pc = CreateProjectCollection(ParserIgnoreConfiguration.Empty)) + { + Should.Throw(() => new Project(projectFile, null, null, pc, ProjectLoadSettings.IgnoreMissingImports | ProjectLoadSettings.IgnoreEmptyImports | ProjectLoadSettings.IgnoreInvalidImports)); + } + } + + [Theory] + [MemberData(nameof(ChildSkipCases))] + public void AllowedChildIsSkipped(string configElement, string configName, string projectXml) + { + string configPath = Path.Combine(_testDir, ParserIgnoreConfiguration.ConfigFileName); + File.WriteAllText(configPath, $@""); + string projectFile = Path.Combine(_testDir, "test.proj"); + File.WriteAllText(projectFile, projectXml); + + using (var pc = CreateProjectCollection(ParserIgnoreConfiguration.LoadFromFile(configPath))) + { + var project = new Project(projectFile, null, null, pc, ProjectLoadSettings.IgnoreMissingImports | ProjectLoadSettings.IgnoreEmptyImports | ProjectLoadSettings.IgnoreInvalidImports); + project.ShouldNotBeNull(); + } + } + + [Theory] + [MemberData(nameof(ChildSkipCases))] + public void NonAllowedChildThrows(string configElement, string configName, string projectXml) + { + _ = configElement; + _ = configName; + string projectFile = Path.Combine(_testDir, "test.proj"); + File.WriteAllText(projectFile, projectXml); + + using (var pc = CreateProjectCollection(ParserIgnoreConfiguration.Empty)) + { + Should.Throw(() => new Project(projectFile, null, null, pc, ProjectLoadSettings.IgnoreMissingImports | ProjectLoadSettings.IgnoreEmptyImports | ProjectLoadSettings.IgnoreInvalidImports)); + } + } + + public static IEnumerable AttributeSkipCases => new[] + { + new object[] { "Target", "X", @"" }, + new object[] { "PropertyGroup", "X", @"1" }, + new object[] { "Property", "X", @"1" }, + new object[] { "ItemGroup", "X", @"" }, + new object[] { "Item", "_X", @"" }, + new object[] { "Import", "X", @"" }, + new object[] { "ImportGroup", "X", @"" }, + new object[] { "UsingTask", "X", @"" }, + new object[] { "OnError", "X", @"" }, + new object[] { "Output", "X", @"" }, + new object[] { "Choose", "X", @"1" }, + new object[] { "Otherwise", "X", @"12" }, + new object[] { "ItemDefinition", "_X", @"" }, + new object[] { "Metadata", "X", @"val" }, + new object[] { "UsingTaskBody", "X", @"" }, + new object[] { "Parameter", "X", @"" }, + new object[] { "ProjectExtensions", "X", @"bar" }, + }; + + public static IEnumerable ChildSkipCases => new[] + { + new object[] { "Project", "Custom", @"" }, + new object[] { "UsingTask", "Custom", @"" }, + new object[] { "ImportGroup", "Custom", @"" }, + new object[] { "Choose", "Custom", @"1" }, + new object[] { "When", "Custom", @"1" }, + new object[] { "Otherwise", "Custom", @"12" }, + new object[] { "Task", "Custom", @"" }, + }; + + private static ProjectCollection CreateProjectCollection(ParserIgnoreConfiguration config) + { + var projectCollection = new ProjectCollection(); + projectCollection.ParserIgnoreConfiguration = config; + return projectCollection; + } + } +} diff --git a/src/Build.UnitTests/Evaluation/SimpleProjectRootElementCache_Tests.cs b/src/Build.UnitTests/Evaluation/SimpleProjectRootElementCache_Tests.cs index 947d1dc22c6..10f35453870 100644 --- a/src/Build.UnitTests/Evaluation/SimpleProjectRootElementCache_Tests.cs +++ b/src/Build.UnitTests/Evaluation/SimpleProjectRootElementCache_Tests.cs @@ -26,6 +26,20 @@ public void Dispose() ProjectCollection.GlobalProjectCollection.UnloadAllProjects(); } + [Fact] + public void DiscardImplicitReferences_RemovesAllEntries() + { + string projectFile = NativeMethodsShared.IsUnixLike ? "/foo" : "c:\\foo"; + ProjectRootElement rootElementToCache = ProjectRootElement.Create(projectFile); + + var cache = new SimpleProjectRootElementCache(); + cache.AddEntry(rootElementToCache); + + cache.DiscardImplicitReferences(); + + cache.Get(projectFile, null, false, null).ShouldBeNull(); + } + [Fact] public void Get_GivenCachedProjectFile_ReturnsRootElement() { diff --git a/src/Build/BackEnd/BuildManager/BuildManager.cs b/src/Build/BackEnd/BuildManager/BuildManager.cs index 2a4eaefea24..d1f9048d640 100644 --- a/src/Build/BackEnd/BuildManager/BuildManager.cs +++ b/src/Build/BackEnd/BuildManager/BuildManager.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -618,6 +618,11 @@ public void BeginBuild(BuildParameters parameters) // Initialize additional build parameters. _buildParameters.BuildId = GetNextBuildId(); + if (!Traits.Instance.EscapeHatches.DisableParseConfig) + { + _buildParameters.ParserIgnoreConfiguration = _buildParameters.ProjectRootElementCache.ParserIgnoreConfiguration; + } + if (_buildParameters.UsesCachedResults() && _buildParameters.ProjectIsolationMode == ProjectIsolationMode.False) { // If input or output caches are used and the project isolation mode is set to diff --git a/src/Build/BackEnd/BuildManager/BuildParameters.cs b/src/Build/BackEnd/BuildManager/BuildParameters.cs index 4e6733aec3e..ae8b126be22 100644 --- a/src/Build/BackEnd/BuildManager/BuildParameters.cs +++ b/src/Build/BackEnd/BuildManager/BuildParameters.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -236,6 +236,12 @@ public class BuildParameters : ITranslatable private bool _reportFileAccesses; + /// + /// The configuration for allowed unknown attributes/elements during parsing. + /// Loaded on the main node and serialized to worker nodes. + /// + private ParserIgnoreConfiguration _ParserIgnoreConfiguration = ParserIgnoreConfiguration.Empty; + /// /// Constructor for those who intend to set all properties themselves. /// @@ -262,6 +268,7 @@ public BuildParameters(ProjectCollection projectCollection) _globalProperties = new PropertyDictionary(projectCollection.GlobalPropertiesCollection); _propertiesFromCommandLine = projectCollection.PropertiesFromCommandLine; + _ParserIgnoreConfiguration = projectCollection.ParserIgnoreConfiguration; } /// @@ -330,6 +337,7 @@ internal BuildParameters(BuildParameters other, bool resetEnvironment = false) IsTelemetryEnabled = other.IsTelemetryEnabled; ProjectCacheDescriptor = other.ProjectCacheDescriptor; _enableTargetOutputLogging = other.EnableTargetOutputLogging; + _ParserIgnoreConfiguration = other._ParserIgnoreConfiguration; } /// @@ -822,6 +830,16 @@ public ProjectLoadSettings ProjectLoadSettings set => _projectLoadSettings = value; } + /// + /// Gets or sets the configuration for allowed unknown attributes/elements during parsing. + /// When set, this configuration is used for parsing and evaluation. + /// + internal ParserIgnoreConfiguration ParserIgnoreConfiguration + { + get => _ParserIgnoreConfiguration; + set => _ParserIgnoreConfiguration = value; + } + /// /// Gets or sets a value indicating if the build is allowed to interact with the user. /// @@ -994,6 +1012,7 @@ void ITranslatable.Translate(ITranslator translator) translator.Translate(ref _reportFileAccesses); translator.Translate(ref _enableTargetOutputLogging); translator.Translate(ref _multiThreaded); + translator.Translate(ref _ParserIgnoreConfiguration, ParserIgnoreConfiguration.FactoryForDeserialization); // ProjectRootElementCache is not transmitted. // ResetCaches is not transmitted. diff --git a/src/Build/BackEnd/Node/OutOfProcNode.cs b/src/Build/BackEnd/Node/OutOfProcNode.cs index 30bd35c548f..31996716697 100644 --- a/src/Build/BackEnd/Node/OutOfProcNode.cs +++ b/src/Build/BackEnd/Node/OutOfProcNode.cs @@ -715,6 +715,8 @@ private void HandleNodeConfiguration(NodeConfiguration configuration) // Grab the system parameters. _buildParameters = configuration.BuildParameters; + s_projectRootElementCacheBase.SetParserIgnoreConfiguration(configuration.BuildParameters.ParserIgnoreConfiguration); + _buildParameters.ProjectRootElementCache = s_projectRootElementCacheBase; // Snapshot the current environment diff --git a/src/Build/Construction/ProjectRootElement.cs b/src/Build/Construction/ProjectRootElement.cs index ca5e64245be..1a0aaeb1aaf 100644 --- a/src/Build/Construction/ProjectRootElement.cs +++ b/src/Build/Construction/ProjectRootElement.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -181,7 +181,7 @@ internal ProjectRootElement(XmlReader xmlReader, ProjectRootElementCacheBase pro XmlDocumentWithLocation document = LoadDocument(xmlReader, preserveFormatting); - ProjectParser.Parse(document, this); + ProjectParser.Parse(document, this, ProjectRootElementCache.ParserIgnoreConfiguration); } private readonly bool _isEphemeral = false; @@ -217,7 +217,7 @@ private ProjectRootElement(ProjectRootElementCacheBase projectRootElementCache, document.Load(xr); } - ProjectParser.Parse(document, this); + ProjectParser.Parse(document, this, ProjectRootElementCache.ParserIgnoreConfiguration); } /// @@ -241,7 +241,7 @@ private ProjectRootElement( XmlDocumentWithLocation document = LoadDocument(path, preserveFormatting, projectRootElementCache.LoadProjectsReadOnly); - ProjectParser.Parse(document, this); + ProjectParser.Parse(document, this, ProjectRootElementCache.ParserIgnoreConfiguration); } /// @@ -261,7 +261,7 @@ private ProjectRootElement(XmlDocumentWithLocation document, ProjectRootElementC _directory = Environment.CurrentDirectory; IncrementVersion(); - ProjectParser.Parse(document, this); + ProjectParser.Parse(document, this, ProjectRootElementCache.ParserIgnoreConfiguration); } /// @@ -273,7 +273,9 @@ private ProjectRootElement(XmlDocumentWithLocation document, ProjectRootElementC /// private ProjectRootElement(XmlDocumentWithLocation document) { - ProjectParser.Parse(document, this); + // This constructor is only used by ThrowIfDocumentHasParsingErrors to check whether a document + // parses; it deliberately has no cache, so there is no configuration to apply. + ProjectParser.Parse(document, this, ProjectRootElementCache?.ParserIgnoreConfiguration); } /// @@ -1721,7 +1723,7 @@ private void ReloadFrom(Func documentProducer, bo RemoveAllChildren(); - ProjectParser.Parse(newDocument, this); + ProjectParser.Parse(newDocument, this, ProjectRootElementCache.ParserIgnoreConfiguration); MarkDirty("Project reloaded", null); } diff --git a/src/Build/Definition/ProjectCollection.cs b/src/Build/Definition/ProjectCollection.cs index 9bb0a95a5ab..0e45791b17e 100644 --- a/src/Build/Definition/ProjectCollection.cs +++ b/src/Build/Definition/ProjectCollection.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -251,7 +251,7 @@ public ProjectCollection(IDictionary globalProperties) /// The loggers to register. May be null. /// The locations from which to load toolsets. public ProjectCollection(IDictionary globalProperties, IEnumerable loggers, ToolsetDefinitionLocations toolsetDefinitionLocations) - : this(globalProperties, loggers, toolsetDefinitionLocations, 1 /* node count */, false /* do not only log critical events */, loadProjectsReadOnly: false, useAsynchronousLogging: false, reuseProjectRootElementCache: false, enableTargetOutputLogging: false) + : this(globalProperties, loggers, toolsetDefinitionLocations, 1 /* node count */, false /* do not only log critical events */, loadProjectsReadOnly: false, useAsynchronousLogging: false, reuseProjectRootElementCache: false, enableTargetOutputLogging: false, parseConfigDirectory: null) { } @@ -338,12 +338,27 @@ public ProjectCollection(IDictionary globalProperties, IEnumerab /// If set to true, loggers will collect and send Target outputs when targets are finished executing. [RequiresUnreferencedCode("Registers loggers, which can load forwarding logger assemblies by reflection at runtime; incompatible with trimming.")] public ProjectCollection(IDictionary globalProperties, IEnumerable loggers, IEnumerable remoteLoggers, ToolsetDefinitionLocations toolsetDefinitionLocations, int maxNodeCount, bool onlyLogCriticalEvents, bool loadProjectsReadOnly, bool useAsynchronousLogging, bool reuseProjectRootElementCache, bool enableTargetOutputLogging) - : this(globalProperties, loggers, toolsetDefinitionLocations, maxNodeCount, onlyLogCriticalEvents, loadProjectsReadOnly, useAsynchronousLogging, reuseProjectRootElementCache, enableTargetOutputLogging) + : this(globalProperties, loggers, toolsetDefinitionLocations, maxNodeCount, onlyLogCriticalEvents, loadProjectsReadOnly, useAsynchronousLogging, reuseProjectRootElementCache, enableTargetOutputLogging, parseConfigDirectory: null) + { + try + { + RegisterForwardingLoggers(remoteLoggers); + } + catch (Exception) + { + ShutDownLoggingService(); + throw; + } + } + + /// + /// Instantiates a project collection with the specified parameters and a directory to search + /// for a Directory.Parse.config file (walked upward from the given directory). + /// + [RequiresUnreferencedCode("Registers loggers, which can load forwarding logger assemblies by reflection at runtime; incompatible with trimming.")] + public ProjectCollection(IDictionary globalProperties, IEnumerable loggers, IEnumerable remoteLoggers, ToolsetDefinitionLocations toolsetDefinitionLocations, int maxNodeCount, bool onlyLogCriticalEvents, bool loadProjectsReadOnly, bool useAsynchronousLogging, bool reuseProjectRootElementCache, bool enableTargetOutputLogging, string parseConfigDirectory) + : this(globalProperties, loggers, toolsetDefinitionLocations, maxNodeCount, onlyLogCriticalEvents, loadProjectsReadOnly, useAsynchronousLogging, reuseProjectRootElementCache, enableTargetOutputLogging, parseConfigDirectory) { - // Forwarding loggers load logger assemblies by reflection, which is incompatible with - // trimming - that is why every overload taking forwarding loggers is RequiresUnreferencedCode - // while the overloads taking none are not. The trim-safe construction happens in the chained - // private constructor above; only this reflective registration is gated behind the attribute. try { RegisterForwardingLoggers(remoteLoggers); @@ -360,7 +375,7 @@ public ProjectCollection(IDictionary globalProperties, IEnumerab /// registers ordinary (non-forwarding) loggers, but does not load forwarding loggers by /// reflection, so the overloads that take none can be called without RequiresUnreferencedCode. /// - private ProjectCollection(IDictionary globalProperties, IEnumerable loggers, ToolsetDefinitionLocations toolsetDefinitionLocations, int maxNodeCount, bool onlyLogCriticalEvents, bool loadProjectsReadOnly, bool useAsynchronousLogging, bool reuseProjectRootElementCache, bool enableTargetOutputLogging) + private ProjectCollection(IDictionary globalProperties, IEnumerable loggers, ToolsetDefinitionLocations toolsetDefinitionLocations, int maxNodeCount, bool onlyLogCriticalEvents, bool loadProjectsReadOnly, bool useAsynchronousLogging, bool reuseProjectRootElementCache, bool enableTargetOutputLogging, string parseConfigDirectory) { _loadedProjects = new LoadedProjectCollection(); ToolsetLocations = toolsetDefinitionLocations; @@ -381,12 +396,31 @@ private ProjectCollection(IDictionary globalProperties, IEnumera // we do not need to auto reload. bool autoReloadFromDisk = reuseProjectRootElementCache; ProjectRootElementCache = new ProjectRootElementCache(autoReloadFromDisk, loadProjectsReadOnly); + if (reuseProjectRootElementCache) { s_projectRootElementCache = ProjectRootElementCache; } } + if (!Traits.Instance.EscapeHatches.DisableParseConfig) + { + ParserIgnoreConfiguration.ClearBinlogEmbedPaths(); + + var config = ParserIgnoreConfiguration.LoadGlobalConfig(); + + if (!string.IsNullOrEmpty(parseConfigDirectory)) + { + string configPath = FileUtilities.GetPathOfFileAbove(ParserIgnoreConfiguration.ConfigFileName, parseConfigDirectory); + if (!string.IsNullOrEmpty(configPath) && !config.ContainsLoadedFile(configPath)) + { + config = ParserIgnoreConfiguration.Merge(config, ParserIgnoreConfiguration.LoadFromFile(configPath)); + } + } + + ProjectRootElementCache.SetParserIgnoreConfiguration(config); + } + OnlyLogCriticalEvents = onlyLogCriticalEvents; EnableTargetOutputLogging = enableTargetOutputLogging; @@ -492,7 +526,7 @@ public static ProjectCollection GlobalProjectCollection // Take care to ensure that there is never more than one value observed // from this property even in the case of race conditions while lazily initializing. var local = new ProjectCollection(null, null, ToolsetDefinitionLocations.Default, - maxNodeCount: 1, onlyLogCriticalEvents: false, loadProjectsReadOnly: false, useAsynchronousLogging: true, reuseProjectRootElementCache: false, enableTargetOutputLogging: false); + maxNodeCount: 1, onlyLogCriticalEvents: false, loadProjectsReadOnly: false, useAsynchronousLogging: true, reuseProjectRootElementCache: false, enableTargetOutputLogging: false, parseConfigDirectory: null); if (Interlocked.CompareExchange(ref s_globalProjectCollection, local, null) != null) { @@ -563,6 +597,16 @@ public static string DisplayVersion /// public ICollection PropertiesFromCommandLine { get; set; } + /// + /// Gets or sets the configuration for allowed unknown attributes/elements during parsing. + /// When set, this configuration will be used by builds started from this collection. + /// + internal ParserIgnoreConfiguration ParserIgnoreConfiguration + { + get => ProjectRootElementCache.ParserIgnoreConfiguration; + set => ProjectRootElementCache.SetParserIgnoreConfiguration(value); + } + /// /// The default tools version of this project collection. Projects use this tools version if they /// aren't otherwise told what tools version to use. diff --git a/src/Build/Evaluation/Evaluator.cs b/src/Build/Evaluation/Evaluator.cs index feec137d877..49ad7302692 100644 --- a/src/Build/Evaluation/Evaluator.cs +++ b/src/Build/Evaluation/Evaluator.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -307,6 +307,7 @@ private Evaluator( _streamImports.Add(string.Empty); } + /// /// Delegate passed to methods to provide basic expression evaluation /// ability, without having a language service. @@ -392,6 +393,12 @@ internal static void Evaluate( items = evaluator._data.Items; } + string skippedMessage = evaluator._projectRootElementCache.ParserIgnoreConfiguration?.GetSkippedSummaryMessage(); + if (skippedMessage is not null) + { + evaluator._evaluationLoggingContext.LogCommentFromText(MessageImportance.Low, skippedMessage); + } + evaluator._evaluationLoggingContext.LogProjectEvaluationFinished(globalProperties, properties, items, evaluator._evaluationProfiler.ProfiledResult); } @@ -662,6 +669,12 @@ private void Evaluate() _data.EvaluationId = _evaluationLoggingContext.BuildEventContext.EvaluationId; _evaluationLoggingContext.LogProjectEvaluationStarted(); + string configMessage = _projectRootElementCache.ParserIgnoreConfiguration?.GetLoadedConfigsMessage(); + if (configMessage is not null) + { + _evaluationLoggingContext.LogCommentFromText(MessageImportance.Low, configMessage); + } + // Track loads only after start of evaluation was actually logged using var assemblyLoadsTracker = AssemblyLoadsTracker.StartTracking(_evaluationLoggingContext, AssemblyLoadingContext.Evaluation); diff --git a/src/Build/Evaluation/ParserIgnoreConfiguration.cs b/src/Build/Evaluation/ParserIgnoreConfiguration.cs new file mode 100644 index 00000000000..a2eb61ea7c7 --- /dev/null +++ b/src/Build/Evaluation/ParserIgnoreConfiguration.cs @@ -0,0 +1,367 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.IO; +using System.Text; +using Microsoft.Build.BackEnd; +using Microsoft.Build.Framework; +using Microsoft.Build.Shared; +using Microsoft.Build.Shared.FileSystem; + +namespace Microsoft.Build.Evaluation +{ + /// + /// Manages a set of allowed unknown attributes and elements that should be silently skipped + /// during project parsing instead of throwing an InvalidProjectFileException. + /// Configuration is loaded from Directory.Parse.config files discovered from: + /// 1. MSBUILD_PARSE_CONFIG environment variable paths + /// 2. Walking up from the project file's directory (passed via ProjectCollection constructor) + /// + internal sealed class ParserIgnoreConfiguration : ITranslatable + { + internal const string ConfigFileName = "Directory.Parse.config"; + internal const string EnvironmentVariableName = "MSBUILD_PARSE_CONFIG"; + + // Generic element names for dynamically-named elements in the config. + // These allow rules like Attribute:Property:X to apply to all property elements. + internal const string GenericPropertyElement = "Property"; + internal const string GenericItemElement = "Item"; + internal const string GenericItemDefinitionElement = "ItemDefinition"; + internal const string GenericMetadataElement = "Metadata"; + internal const string GenericUsingTaskBodyElement = "UsingTaskBody"; + + /// + /// Static collection of config file paths to embed in the binlog. + /// Populated during config loading, consumed by BinaryLogger.Shutdown. + /// + private static ConcurrentBag s_binlogEmbedPaths = new(); + + /// Paths collected for binlog embedding. + internal static IEnumerable BinlogEmbedPaths => s_binlogEmbedPaths; + + /// Clears the embed paths after they've been written to the binlog. + internal static void ClearBinlogEmbedPaths() + { + s_binlogEmbedPaths = new ConcurrentBag(); + } + + private Dictionary> _allowedAttributes; + private Dictionary> _allowedChildren; + private HashSet _loadedConfigFiles; + private readonly ConcurrentDictionary _skippedItems = new(StringComparer.OrdinalIgnoreCase); + + private ParserIgnoreConfiguration() + { + _allowedAttributes = new Dictionary>(StringComparer.OrdinalIgnoreCase); + _allowedChildren = new Dictionary>(StringComparer.OrdinalIgnoreCase); + _loadedConfigFiles = new HashSet(StringComparer.OrdinalIgnoreCase); + } + + private ParserIgnoreConfiguration(ITranslator translator) + : this() + { + ((ITranslatable)this).Translate(translator); + } + + internal IReadOnlyCollection LoadedConfigFiles => _loadedConfigFiles; + + internal static ParserIgnoreConfiguration Empty { get; } = new ParserIgnoreConfiguration(); + + public static bool Equals(ParserIgnoreConfiguration? left, ParserIgnoreConfiguration? right) + { + if (ReferenceEquals(left, right)) + { + return true; + } + + if (left is null || right is null) + { + return false; + } + + return left._loadedConfigFiles.SetEquals(right._loadedConfigFiles) + && CollectionHelpers.DictionaryEquals(left._allowedAttributes, right._allowedAttributes, HashSet.CreateSetComparer()) + && CollectionHelpers.DictionaryEquals(left._allowedChildren, right._allowedChildren, HashSet.CreateSetComparer()); + } + + /// + /// Checks whether the given file path has already been loaded into this configuration. + /// + internal bool ContainsLoadedFile(string filePath) + { + if (string.IsNullOrEmpty(filePath)) + { + return false; + } + + try + { + return _loadedConfigFiles.Contains(FileUtilities.NormalizePath(filePath)); + } + catch + { + return false; + } + } + + internal bool CheckSkipAttribute(string elementName, string attributeName) + { + if (_allowedAttributes.Count == 0) + { + return false; + } + + if (!_allowedAttributes.TryGetValue(elementName, out HashSet? allowedAttributes) || !allowedAttributes.Contains(attributeName)) + { + return false; + } + + RecordSkippedItem($"Attribute:{elementName}:{attributeName}"); + return true; + } + + internal bool CheckSkipElement(string parentElementName, string childElementName) + { + if (_allowedChildren.Count == 0) + { + return false; + } + + if (!_allowedChildren.TryGetValue(parentElementName, out HashSet? allowedChildren) || !allowedChildren.Contains(childElementName)) + { + return false; + } + + RecordSkippedItem($"Element:{parentElementName}:{childElementName}"); + return true; + } + + internal string? GetSkippedSummaryMessage() + { + if (_skippedItems.IsEmpty) + { + return null; + } + + var sb = new StringBuilder("Skipped unrecognized items allowed by Directory.Parse.config:"); + foreach (var kvp in _skippedItems) + { + sb.Append($" {kvp.Key} ({kvp.Value} occurrence{(kvp.Value > 1 ? "s" : string.Empty)});"); + } + + _skippedItems.Clear(); + + sb.Length--; + return sb.ToString(); + } + + internal string? GetLoadedConfigsMessage() + { + if (_loadedConfigFiles.Count == 0) + { + return null; + } + + return $"Loaded Directory.Parse.config from: {string.Join(", ", _loadedConfigFiles)}"; + } + + internal static ParserIgnoreConfiguration Merge(ParserIgnoreConfiguration left, ParserIgnoreConfiguration right) + { + ArgumentNullException.ThrowIfNull(left); + ArgumentNullException.ThrowIfNull(right); + + var merged = new ParserIgnoreConfiguration(); + + UnionEntries(merged._allowedAttributes, left._allowedAttributes); + UnionEntries(merged._allowedAttributes, right._allowedAttributes); + UnionEntries(merged._allowedChildren, left._allowedChildren); + UnionEntries(merged._allowedChildren, right._allowedChildren); + merged._loadedConfigFiles.UnionWith(left._loadedConfigFiles); + merged._loadedConfigFiles.UnionWith(right._loadedConfigFiles); + MergeSkippedItems(merged._skippedItems, left._skippedItems); + MergeSkippedItems(merged._skippedItems, right._skippedItems); + + return merged; + } + + internal static ParserIgnoreConfiguration LoadFromFile(string filePath) + { + var config = new ParserIgnoreConfiguration(); + config.LoadFile(filePath); + return config; + } + + internal static ParserIgnoreConfiguration LoadGlobalConfig() + { + var config = new ParserIgnoreConfiguration(); + + string? envValue = Environment.GetEnvironmentVariable(EnvironmentVariableName); + if (!string.IsNullOrEmpty(envValue)) + { + string[] paths = envValue.Split(new[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries); + foreach (string path in paths) + { + string trimmedPath = path.Trim(); + if (trimmedPath.Length > 0) + { + config.LoadFile(trimmedPath); + } + } + } + + return config; + } + + void ITranslatable.Translate(ITranslator translator) + { + translator.TranslateDictionary(ref _allowedAttributes, StringComparer.OrdinalIgnoreCase, TranslateHashSetValue, HashSetValueFactory); + translator.TranslateDictionary(ref _allowedChildren, StringComparer.OrdinalIgnoreCase, TranslateHashSetValue, HashSetValueFactory); + translator.Translate(ref _loadedConfigFiles); + } + + private static void TranslateHashSetValue(ITranslator translator, NodePacketValueFactory> factory, ref HashSet value) + { + translator.Translate(ref value); + } + + private static HashSet HashSetValueFactory(ITranslator translator) + { + HashSet? set = null; + translator.Translate(ref set); + return set ?? new HashSet(StringComparer.OrdinalIgnoreCase); + } + + internal static ParserIgnoreConfiguration FactoryForDeserialization(ITranslator translator) + { + return new ParserIgnoreConfiguration(translator); + } + + private static void UnionEntries(Dictionary> destination, Dictionary> source) + { + foreach (var kvp in source) + { + if (!destination.TryGetValue(kvp.Key, out HashSet? names)) + { + names = new HashSet(StringComparer.OrdinalIgnoreCase); + destination[kvp.Key] = names; + } + + names.UnionWith(kvp.Value); + } + } + + private static void MergeSkippedItems(ConcurrentDictionary destination, ConcurrentDictionary source) + { + foreach (var kvp in source) + { + destination.AddOrUpdate(kvp.Key, kvp.Value, (_, count) => count + kvp.Value); + } + } + + private void RecordSkippedItem(string key) + { + _skippedItems.AddOrUpdate(key, 1, static (_, count) => count + 1); + } + + private void LoadFile(string filePath) + { + if (string.IsNullOrEmpty(filePath)) + { + return; + } + + string fullPath; + try + { + fullPath = FileUtilities.NormalizePath(filePath); + } + catch + { + return; + } + + if (!FileSystems.Default.FileExists(fullPath) || !_loadedConfigFiles.Add(fullPath)) + { + return; + } + + s_binlogEmbedPaths.Add(fullPath); + + try + { + var settings = new System.Xml.XmlReaderSettings { DtdProcessing = System.Xml.DtdProcessing.Prohibit }; + var doc = new System.Xml.XmlDocument(); + using (var stream = File.OpenRead(fullPath)) + using (var reader = System.Xml.XmlReader.Create(stream, settings)) + { + doc.Load(reader); + } + + System.Xml.XmlElement? root = doc.DocumentElement; + if (root is null || !root.Name.Equals("ParseConfig", StringComparison.OrdinalIgnoreCase)) + { + return; + } + + foreach (System.Xml.XmlNode child in root.ChildNodes) + { + if (child.NodeType != System.Xml.XmlNodeType.Element) + { + continue; + } + + Dictionary>? target = null; + if (child.Name.Equals("IgnoreAttributes", StringComparison.OrdinalIgnoreCase)) + { + target = _allowedAttributes; + } + else if (child.Name.Equals("IgnoreChildren", StringComparison.OrdinalIgnoreCase)) + { + target = _allowedChildren; + } + + if (target is null) + { + continue; + } + + foreach (System.Xml.XmlNode ignoreNode in child.ChildNodes) + { + if (ignoreNode.NodeType != System.Xml.XmlNodeType.Element + || !ignoreNode.Name.Equals("Ignore", StringComparison.OrdinalIgnoreCase)) + { + continue; + } + + System.Xml.XmlElement ignoreElement = (System.Xml.XmlElement)ignoreNode; + string elementName = ignoreElement.GetAttribute("Element"); + string name = ignoreElement.GetAttribute("Name"); + + if (!string.IsNullOrEmpty(elementName) && !string.IsNullOrEmpty(name)) + { + AddAllowedName(target, elementName, name); + } + } + } + } + catch + { + // If the file can't be parsed as XML, silently skip it + } + } + + private static void AddAllowedName(Dictionary> destination, string elementName, string unknownName) + { + if (!destination.TryGetValue(elementName, out HashSet? names)) + { + names = new HashSet(StringComparer.OrdinalIgnoreCase); + destination[elementName] = names; + } + + names.Add(unknownName); + } + } +} diff --git a/src/Build/Evaluation/ProjectParser.cs b/src/Build/Evaluation/ProjectParser.cs index 5170d52db24..e70f0889606 100644 --- a/src/Build/Evaluation/ProjectParser.cs +++ b/src/Build/Evaluation/ProjectParser.cs @@ -1,9 +1,10 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using System.Collections.Generic; using System.Xml; +using Microsoft.Build.Evaluation; using Microsoft.Build.Eventing; using Microsoft.Build.Framework; using Microsoft.Build.Shared; @@ -85,6 +86,8 @@ internal class ProjectParser /// private readonly XmlDocumentWithLocation _document; + private readonly ParserIgnoreConfiguration _ParserIgnoreConfiguration; + /// /// Whether a ProjectExtensions node has been encountered already. /// It's not supposed to appear more than once. @@ -94,13 +97,14 @@ internal class ProjectParser /// /// Private constructor to give static semantics /// - private ProjectParser(XmlDocumentWithLocation document, ProjectRootElement project) + private ProjectParser(XmlDocumentWithLocation document, ProjectRootElement project, ParserIgnoreConfiguration ParserIgnoreConfiguration) { Assumed.NotNull(project); Assumed.NotNull(document); _document = document; _project = project; + _ParserIgnoreConfiguration = ParserIgnoreConfiguration; } /// @@ -111,11 +115,11 @@ private ProjectParser(XmlDocumentWithLocation document, ProjectRootElement proje /// The code markers here used to be around the Project class constructor in the old code. /// In the new code, that's not very interesting; we are repurposing to wrap parsing the XML. /// - internal static void Parse(XmlDocumentWithLocation document, ProjectRootElement projectRootElement) + internal static void Parse(XmlDocumentWithLocation document, ProjectRootElement projectRootElement, ParserIgnoreConfiguration ParserIgnoreConfiguration) { MSBuildEventSource.Log.ParseStart(projectRootElement.ProjectFileLocation.File); { - ProjectParser parser = new ProjectParser(document, projectRootElement); + ProjectParser parser = new ProjectParser(document, projectRootElement, ParserIgnoreConfiguration); parser.Parse(); } MSBuildEventSource.Log.ParseStop(projectRootElement.ProjectFileLocation.File); @@ -204,7 +208,11 @@ private void Parse() break; default: - ProjectXmlUtilities.ThrowProjectInvalidChildElement(childElement.Name, childElement.ParentNode.Name, childElement.Location); + if (ProjectXmlUtilities.ThrowIfProjectInvalidChildElement(childElement.Name, childElement.ParentNode.Name, childElement.Location, _ParserIgnoreConfiguration)) + { + continue; + } + break; } } @@ -215,13 +223,13 @@ private void Parse() /// private ProjectPropertyGroupElement ParseProjectPropertyGroupElement(XmlElementWithLocation element, ProjectElementContainer parent) { - ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnlyConditionAndLabel); + ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnlyConditionAndLabel, _ParserIgnoreConfiguration); ProjectPropertyGroupElement propertyGroup = new ProjectPropertyGroupElement(element, parent, _project); foreach (XmlElementWithLocation childElement in ProjectXmlUtilities.GetVerifyThrowProjectChildElements(element)) { - ProjectXmlUtilities.VerifyThrowProjectAttributes(childElement, ValidAttributesOnlyConditionAndLabel); + ProjectXmlUtilities.VerifyThrowProjectAttributes(childElement, ValidAttributesOnlyConditionAndLabel, _ParserIgnoreConfiguration, ParserIgnoreConfiguration.GenericPropertyElement); XmlUtilities.VerifyThrowProjectValidElementName(childElement); ProjectErrorUtilities.VerifyThrowInvalidProject(!XMakeElements.ReservedItemNames.Contains(childElement.Name) && !ReservedPropertyNames.IsReservedProperty(childElement.Name), childElement.Location, "CannotModifyReservedProperty", childElement.Name); @@ -239,7 +247,7 @@ private ProjectPropertyGroupElement ParseProjectPropertyGroupElement(XmlElementW /// private ProjectItemGroupElement ParseProjectItemGroupElement(XmlElementWithLocation element, ProjectElementContainer parent) { - ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnlyConditionAndLabel); + ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnlyConditionAndLabel, _ParserIgnoreConfiguration); ProjectItemGroupElement itemGroup = new ProjectItemGroupElement(element, parent, _project); @@ -320,7 +328,10 @@ private ProjectItemElement ParseProjectItemElement(XmlElementWithLocation elemen if (!isKnownAttribute && !isValidMetadataNameInAttribute) { - ProjectXmlUtilities.ThrowProjectInvalidAttribute(attribute); + if (_ParserIgnoreConfiguration?.CheckSkipAttribute(ParserIgnoreConfiguration.GenericItemElement, attribute.Name) != true) + { + ProjectXmlUtilities.ThrowProjectInvalidAttribute(attribute); + } } else if (isValidMetadataNameInAttribute) { @@ -392,7 +403,7 @@ internal static void CheckMetadataAsAttributeName(string name, out bool isReserv /// private ProjectMetadataElement ParseProjectMetadataElement(XmlElementWithLocation element, ProjectElementContainer parent) { - ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnlyConditionAndLabel); + ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnlyConditionAndLabel, _ParserIgnoreConfiguration, ParserIgnoreConfiguration.GenericMetadataElement); XmlUtilities.VerifyThrowProjectValidElementName(element); @@ -420,18 +431,25 @@ private ProjectMetadataElement ParseProjectMetadataElement(XmlElementWithLocatio /// A ProjectImportGroupElement derived from the XML element passed in private ProjectImportGroupElement ParseProjectImportGroupElement(XmlElementWithLocation element, ProjectRootElement parent) { - ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnlyConditionAndLabel); + ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnlyConditionAndLabel, _ParserIgnoreConfiguration); ProjectImportGroupElement importGroup = new ProjectImportGroupElement(element, parent, _project); foreach (XmlElementWithLocation childElement in ProjectXmlUtilities.GetVerifyThrowProjectChildElements(element)) { - ProjectErrorUtilities.VerifyThrowInvalidProject( - childElement.Name == XMakeElements.import, - childElement.Location, - "UnrecognizedChildElement", - childElement.Name, - element.Name); + if (childElement.Name != XMakeElements.import) + { + if (_ParserIgnoreConfiguration?.CheckSkipElement(element.Name, childElement.Name) == true) + { + continue; + } + + ProjectErrorUtilities.ThrowInvalidProject( + childElement.Location, + "UnrecognizedChildElement", + childElement.Name, + element.Name); + } ProjectImportElement item = ParseProjectImportElement(childElement, importGroup); @@ -453,9 +471,9 @@ private ProjectImportElement ParseProjectImportElement(XmlElementWithLocation el parent, element); - ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnImport); + ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnImport, _ParserIgnoreConfiguration); ProjectXmlUtilities.VerifyThrowProjectRequiredAttribute(element, XMakeAttributes.project); - ProjectXmlUtilities.VerifyThrowProjectNoChildElements(element); + ProjectXmlUtilities.VerifyThrowProjectNoChildElements(element, _ParserIgnoreConfiguration); SdkReference sdk = null; if (element.HasAttribute(XMakeAttributes.sdk)) @@ -475,7 +493,7 @@ private ProjectImportElement ParseProjectImportElement(XmlElementWithLocation el private UsingTaskParameterGroupElement ParseUsingTaskParameterGroupElement(XmlElementWithLocation element, ProjectElementContainer parent) { // There should be no attributes - ProjectXmlUtilities.VerifyThrowProjectNoAttributes(element); + ProjectXmlUtilities.VerifyThrowProjectNoAttributes(element, _ParserIgnoreConfiguration); UsingTaskParameterGroupElement parameterGroup = new UsingTaskParameterGroupElement(element, parent, _project); @@ -489,7 +507,7 @@ private UsingTaskParameterGroupElement ParseUsingTaskParameterGroupElement(XmlEl } else { - ProjectXmlUtilities.VerifyThrowProjectAttributes(childElement, ValidAttributesOnUsingTaskParameter); + ProjectXmlUtilities.VerifyThrowProjectAttributes(childElement, ValidAttributesOnUsingTaskParameter, _ParserIgnoreConfiguration, XMakeElements.usingTaskParameter); XmlUtilities.VerifyThrowProjectValidElementName(childElement); ProjectUsingTaskParameterElement parameter = new ProjectUsingTaskParameterElement(childElement, parameterGroup, _project); parameterGroup.AppendParentedChildNoChecks(parameter); @@ -507,7 +525,7 @@ private UsingTaskParameterGroupElement ParseUsingTaskParameterGroupElement(XmlEl /// private ProjectUsingTaskElement ParseProjectUsingTaskElement(XmlElementWithLocation element) { - ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnUsingTask); + ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnUsingTask, _ParserIgnoreConfiguration); ProjectErrorUtilities.VerifyThrowInvalidProject(element.GetAttribute(XMakeAttributes.taskName).Length > 0, element.Location, "ProjectTaskNameEmpty"); string assemblyName = element.GetAttribute(XMakeAttributes.assemblyName); @@ -550,13 +568,17 @@ private ProjectUsingTaskElement ParseProjectUsingTaskElement(XmlElementWithLocat ProjectXmlUtilities.ThrowProjectInvalidChildElementDueToDuplicate(childElement); } - ProjectXmlUtilities.VerifyThrowProjectAttributes(childElement, ValidAttributesOnUsingTaskBody); + ProjectXmlUtilities.VerifyThrowProjectAttributes(childElement, ValidAttributesOnUsingTaskBody, _ParserIgnoreConfiguration, ParserIgnoreConfiguration.GenericUsingTaskBodyElement); child = new ProjectUsingTaskBodyElement(childElement, usingTask, _project); foundTaskElement = true; break; default: - ProjectXmlUtilities.ThrowProjectInvalidChildElement(childElement.Name, element.Name, element.Location); + if (ProjectXmlUtilities.ThrowIfProjectInvalidChildElement(childElement.Name, element.Name, element.Location, _ParserIgnoreConfiguration)) + { + continue; + } + break; } @@ -571,7 +593,7 @@ private ProjectUsingTaskElement ParseProjectUsingTaskElement(XmlElementWithLocat /// private ProjectTargetElement ParseProjectTargetElement(XmlElementWithLocation element) { - ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnTarget); + ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnTarget, _ParserIgnoreConfiguration); ProjectXmlUtilities.VerifyThrowProjectRequiredAttribute(element, XMakeAttributes.name); // Orcas compat: all target names are automatically unescaped @@ -613,9 +635,9 @@ private ProjectTargetElement ParseProjectTargetElement(XmlElementWithLocation el case XMakeElements.onError: // Previous OM accidentally didn't verify ExecuteTargets on parse, // but we do, as it makes no sense - ProjectXmlUtilities.VerifyThrowProjectAttributes(childElement, ValidAttributesOnOnError); + ProjectXmlUtilities.VerifyThrowProjectAttributes(childElement, ValidAttributesOnOnError, _ParserIgnoreConfiguration); ProjectXmlUtilities.VerifyThrowProjectRequiredAttribute(childElement, XMakeAttributes.executeTargets); - ProjectXmlUtilities.VerifyThrowProjectNoChildElements(childElement); + ProjectXmlUtilities.VerifyThrowProjectNoChildElements(childElement, _ParserIgnoreConfiguration); child = onError = new ProjectOnErrorElement(childElement, target, _project); break; @@ -665,7 +687,15 @@ private ProjectTaskElement ParseProjectTaskElement(XmlElementWithLocation elemen foreach (XmlElementWithLocation childElement in ProjectXmlUtilities.GetVerifyThrowProjectChildElements(element)) { - ProjectErrorUtilities.VerifyThrowInvalidProject(childElement.Name == XMakeElements.output, childElement.Location, "UnrecognizedChildElement", childElement.Name, task.Name); + if (childElement.Name != XMakeElements.output) + { + if (_ParserIgnoreConfiguration?.CheckSkipElement(XMakeElements.usingTaskBody, childElement.Name) == true) + { + continue; + } + + ProjectErrorUtilities.ThrowInvalidProject(childElement.Location, "UnrecognizedChildElement", childElement.Name, task.Name); + } ProjectOutputElement output = ParseProjectOutputElement(childElement, task); @@ -680,9 +710,9 @@ private ProjectTaskElement ParseProjectTaskElement(XmlElementWithLocation elemen /// private ProjectOutputElement ParseProjectOutputElement(XmlElementWithLocation element, ProjectTaskElement parent) { - ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnOutput); + ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnOutput, _ParserIgnoreConfiguration); ProjectXmlUtilities.VerifyThrowProjectRequiredAttribute(element, XMakeAttributes.taskParameter); - ProjectXmlUtilities.VerifyThrowProjectNoChildElements(element); + ProjectXmlUtilities.VerifyThrowProjectNoChildElements(element, _ParserIgnoreConfiguration); XmlAttributeWithLocation itemNameAttribute = element.GetAttributeWithLocation(XMakeAttributes.itemName); XmlAttributeWithLocation propertyNameAttribute = element.GetAttributeWithLocation(XMakeAttributes.propertyName); @@ -706,7 +736,7 @@ private ProjectOutputElement ParseProjectOutputElement(XmlElementWithLocation el /// private ProjectItemDefinitionGroupElement ParseProjectItemDefinitionGroupElement(XmlElementWithLocation element, ProjectElementContainer parent) { - ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnlyConditionAndLabel); + ProjectXmlUtilities.VerifyThrowProjectAttributes(element, ValidAttributesOnlyConditionAndLabel, _ParserIgnoreConfiguration); ProjectItemDefinitionGroupElement itemDefinitionGroup = new ProjectItemDefinitionGroupElement(element, parent, _project); @@ -737,7 +767,10 @@ private ProjectItemDefinitionElement ParseProjectItemDefinitionXml(XmlElementWit if (!isKnownAttribute && !isValidMetadataNameInAttribute) { - ProjectXmlUtilities.ThrowProjectInvalidAttribute(attribute); + if (_ParserIgnoreConfiguration?.CheckSkipAttribute(ParserIgnoreConfiguration.GenericItemDefinitionElement, attribute.Name) != true) + { + ProjectXmlUtilities.ThrowProjectInvalidAttribute(attribute); + } } else if (isValidMetadataNameInAttribute) { @@ -749,7 +782,10 @@ private ProjectItemDefinitionElement ParseProjectItemDefinitionXml(XmlElementWit } else if (!ValidAttributesOnlyConditionAndLabel.Contains(attribute.Name)) { - ProjectXmlUtilities.ThrowProjectInvalidAttribute(attribute); + if (_ParserIgnoreConfiguration?.CheckSkipAttribute(ParserIgnoreConfiguration.GenericItemDefinitionElement, attribute.Name) != true) + { + ProjectXmlUtilities.ThrowProjectInvalidAttribute(attribute); + } } } @@ -768,7 +804,7 @@ private ProjectItemDefinitionElement ParseProjectItemDefinitionXml(XmlElementWit /// private ProjectChooseElement ParseProjectChooseElement(XmlElementWithLocation element, ProjectElementContainer parent, int nestingDepth) { - ProjectXmlUtilities.VerifyThrowProjectNoAttributes(element); + ProjectXmlUtilities.VerifyThrowProjectNoAttributes(element, _ParserIgnoreConfiguration); ProjectChooseElement choose = new ProjectChooseElement(element, parent, _project); @@ -797,7 +833,11 @@ private ProjectChooseElement ParseProjectChooseElement(XmlElementWithLocation el break; default: - ProjectXmlUtilities.ThrowProjectInvalidChildElement(childElement.Name, element.Name, element.Location); + if (ProjectXmlUtilities.ThrowIfProjectInvalidChildElement(childElement.Name, element.Name, element.Location, _ParserIgnoreConfiguration)) + { + continue; + } + break; } @@ -828,7 +868,7 @@ private ProjectWhenElement ParseProjectWhenElement(XmlElementWithLocation elemen /// private ProjectOtherwiseElement ParseProjectOtherwiseElement(XmlElementWithLocation element, ProjectChooseElement parent, int nestingDepth) { - ProjectXmlUtilities.VerifyThrowProjectNoAttributes(element); + ProjectXmlUtilities.VerifyThrowProjectNoAttributes(element, _ParserIgnoreConfiguration); ProjectOtherwiseElement otherwise = new ProjectOtherwiseElement(element, parent, _project); @@ -865,7 +905,11 @@ private void ParseWhenOtherwiseChildren(XmlElementWithLocation element, ProjectE break; default: - ProjectXmlUtilities.ThrowProjectInvalidChildElement(childElement.Name, element.Name, element.Location); + if (ProjectXmlUtilities.ThrowIfProjectInvalidChildElement(childElement.Name, element.Name, element.Location, _ParserIgnoreConfiguration)) + { + continue; + } + break; } @@ -880,7 +924,7 @@ private ProjectExtensionsElement ParseProjectExtensionsElement(XmlElementWithLoc { // ProjectExtensions are only found in the main project file - in fact, the code used to ignore them in imported // files. We don't. - ProjectXmlUtilities.VerifyThrowProjectNoAttributes(element); + ProjectXmlUtilities.VerifyThrowProjectNoAttributes(element, _ParserIgnoreConfiguration); ProjectErrorUtilities.VerifyThrowInvalidProject(!_seenProjectExtensions, element.Location, "DuplicateProjectExtensions"); _seenProjectExtensions = true; diff --git a/src/Build/Evaluation/ProjectRootElementCacheBase.cs b/src/Build/Evaluation/ProjectRootElementCacheBase.cs index fc556c1eec9..05a2065a2ba 100644 --- a/src/Build/Evaluation/ProjectRootElementCacheBase.cs +++ b/src/Build/Evaluation/ProjectRootElementCacheBase.cs @@ -12,6 +12,15 @@ internal abstract class ProjectRootElementCacheBase { public bool LoadProjectsReadOnly { get; protected set; } + public ParserIgnoreConfiguration ParserIgnoreConfiguration { get; protected set; } + + /// + /// Whether has already been applied to this cache. A null + /// is a legitimate configuration - it is what the escape hatch + /// produces - so it cannot itself stand for "never set". + /// + private bool _parserIgnoreConfigurationInitialized; + /// /// Handler for which project root element just got added to the cache /// @@ -70,6 +79,26 @@ internal abstract ProjectRootElement Get(string projectFile, OpenProjectRootElem internal abstract void DiscardAnyWeakReference(ProjectRootElement projectRootElement); + internal void SetParserIgnoreConfiguration(ParserIgnoreConfiguration configuration) + { + if (ParserIgnoreConfiguration.Equals(ParserIgnoreConfiguration, configuration)) + { + return; + } + + // Entries parsed under the previous configuration may have skipped - or refused to skip - unknown + // attributes and elements that the new one treats differently, so they must be reparsed. A cache that + // has not had a configuration applied yet holds nothing parsed under a different one, so on that first + // initialization - which happens once per newly created cache - there is nothing to invalidate. + if (_parserIgnoreConfigurationInitialized) + { + DiscardImplicitReferences(); + } + + _parserIgnoreConfigurationInitialized = true; + ParserIgnoreConfiguration = configuration; + } + /// /// Raises the event. /// diff --git a/src/Build/Evaluation/SimpleProjectRootElementCache.cs b/src/Build/Evaluation/SimpleProjectRootElementCache.cs index 646bcdcae40..1b3083756de 100644 --- a/src/Build/Evaluation/SimpleProjectRootElementCache.cs +++ b/src/Build/Evaluation/SimpleProjectRootElementCache.cs @@ -111,7 +111,9 @@ internal override void Clear() internal override void DiscardImplicitReferences() { - throw new NotImplementedException(); + // This cache does not track whether an entry was explicitly loaded - Get ignores isExplicitlyLoaded - + // so every entry it holds is an implicit reference and discarding all of them is the correct behavior. + Clear(); } internal override void DiscardAnyWeakReference(ProjectRootElement projectRootElement) diff --git a/src/Build/Instance/TaskRegistry.cs b/src/Build/Instance/TaskRegistry.cs index d3114ec80bf..32e35cebca5 100644 --- a/src/Build/Instance/TaskRegistry.cs +++ b/src/Build/Instance/TaskRegistry.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -320,7 +320,7 @@ private static void RegisterTasksFromUsingTaskElement if (String.IsNullOrEmpty(taskFactory) || taskFactory.Equals(RegisteredTaskRecord.AssemblyTaskFactory, StringComparison.OrdinalIgnoreCase) || taskFactory.Equals(RegisteredTaskRecord.TaskHostFactory, StringComparison.OrdinalIgnoreCase)) { - ProjectXmlUtilities.VerifyThrowProjectNoChildElements(projectUsingTaskXml.XmlElement); + ProjectXmlUtilities.VerifyThrowProjectNoChildElements(projectUsingTaskXml.XmlElement, projectUsingTaskXml.ContainingProject.ProjectRootElementCache.ParserIgnoreConfiguration); } if (projectUsingTaskXml.AssemblyFile.Length > 0) diff --git a/src/Build/Logging/BinaryLogger/BinaryLogger.cs b/src/Build/Logging/BinaryLogger/BinaryLogger.cs index e5d73a3bf3f..3dd6cc52663 100644 --- a/src/Build/Logging/BinaryLogger/BinaryLogger.cs +++ b/src/Build/Logging/BinaryLogger/BinaryLogger.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -511,6 +511,14 @@ public void Shutdown() projectImportsCollector.AddFile(filePath); } EditorConfigParser.ClearEditorConfigFilePaths(); + + // Write the Directory.Parse.config file paths to the log + foreach (var filePath in Evaluation.ParserIgnoreConfiguration.BinlogEmbedPaths) + { + projectImportsCollector.AddFile(filePath); + } + Evaluation.ParserIgnoreConfiguration.ClearBinlogEmbedPaths(); + projectImportsCollector.Close(); if (CollectProjectImports == ProjectImportsCollectionMode.Embed) diff --git a/src/Build/Microsoft.Build.csproj b/src/Build/Microsoft.Build.csproj index 2edd2f2474e..1aa8eef4824 100644 --- a/src/Build/Microsoft.Build.csproj +++ b/src/Build/Microsoft.Build.csproj @@ -544,6 +544,7 @@ + diff --git a/src/Build/Xml/ProjectXmlUtilities.XmlElementChildIterator.cs b/src/Build/Xml/ProjectXmlUtilities.XmlElementChildIterator.cs index 8fc5e75a7b5..2543fbff0e3 100644 --- a/src/Build/Xml/ProjectXmlUtilities.XmlElementChildIterator.cs +++ b/src/Build/Xml/ProjectXmlUtilities.XmlElementChildIterator.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -97,7 +97,7 @@ private readonly XmlElementWithLocation GetNextNode(XmlNode child) } if (_throwForInvalidNodeTypes) { - ThrowProjectInvalidChildElement(child.Name, _element.Name, _element.Location); + ThrowIfProjectInvalidChildElement(child.Name, _element.Name, _element.Location, config: null); } break; } diff --git a/src/Build/Xml/ProjectXmlUtilities.cs b/src/Build/Xml/ProjectXmlUtilities.cs index 43672a07329..c397b26f0f1 100644 --- a/src/Build/Xml/ProjectXmlUtilities.cs +++ b/src/Build/Xml/ProjectXmlUtilities.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; @@ -6,6 +6,7 @@ using System.IO; using System.Xml; using Microsoft.Build.Construction; +using Microsoft.Build.Evaluation; using Microsoft.Build.Framework.BuildException; using Microsoft.Build.Shared; @@ -55,11 +56,11 @@ private static XmlElementChildIterator GetChildElements(XmlElementWithLocation e /// /// Throw an invalid project exception if there are any child elements at all /// - internal static void VerifyThrowProjectNoChildElements(XmlElementWithLocation element) + internal static void VerifyThrowProjectNoChildElements(XmlElementWithLocation element, ParserIgnoreConfiguration config) { foreach (var child in GetVerifyThrowProjectChildElements(element)) { - ThrowProjectInvalidChildElement(child.Name, element.Name, element.Location); + ThrowIfProjectInvalidChildElement(child.Name, element.Name, element.Location, config); } } @@ -72,14 +73,6 @@ internal static void ThrowProjectInvalidChildElementDueToDuplicate(XmlElementWit ProjectErrorUtilities.ThrowInvalidProject(child.Location, "InvalidChildElementDueToDuplication", child.Name, child.ParentNode.Name); } - /// - /// Throw an invalid project exception indicating that the child is not valid beneath the element - /// - internal static void ThrowProjectInvalidChildElement(string name, string parentName, ElementLocation location) - { - ProjectErrorUtilities.ThrowInvalidProject(location, "UnrecognizedChildElement", name, parentName); - } - /// /// Verify that if a namespace is specified it matches the default MSBuild namespace. /// @@ -145,12 +138,17 @@ internal static void VerifyThrowProjectAttributeEitherMissingOrNotEmpty(XmlEleme /// /// If there are any attributes on the element, throws an InvalidProjectFileException complaining that the attribute is not valid on this element. /// - internal static void VerifyThrowProjectNoAttributes(XmlElementWithLocation element) + internal static void VerifyThrowProjectNoAttributes(XmlElementWithLocation element, ParserIgnoreConfiguration config) { if (element.HasAttributes) { foreach (XmlAttributeWithLocation attribute in element.Attributes) { + if (config?.CheckSkipAttribute(element.Name, attribute.Name) == true) + { + continue; + } + ThrowProjectInvalidAttribute(attribute); } } @@ -177,14 +175,40 @@ internal static void VerifyThrowProjectRequiredAttribute(XmlElementWithLocation } /// - /// Verify that all attributes on the element are on the list of legal attributes + /// Verify that all attributes on the element are on the list of legal attributes. + /// When genericElementName is provided, only that name is checked in the config (not the actual element name). /// - internal static void VerifyThrowProjectAttributes(XmlElementWithLocation element, HashSet validAttributes) + internal static void VerifyThrowProjectAttributes(XmlElementWithLocation element, HashSet validAttributes, ParserIgnoreConfiguration config, string genericElementName = null) { foreach (XmlAttributeWithLocation attribute in element.Attributes) { - ProjectXmlUtilities.VerifyThrowProjectInvalidAttribute(validAttributes.Contains(attribute.Name), attribute); + if (!validAttributes.Contains(attribute.Name)) + { + string configElementName = genericElementName ?? element.Name; + if (config?.CheckSkipAttribute(configElementName, attribute.Name) == true) + { + continue; + } + + ThrowProjectInvalidAttribute(attribute); + } + } + } + + /// + /// Throws an InvalidProjectFileException if the child element is not allowed by configuration. + /// If the element is allowed, it is silently skipped. + /// + /// True if the element was skipped (caller should continue), false if the element was not in config and an exception was thrown. + internal static bool ThrowIfProjectInvalidChildElement(string name, string parentName, ElementLocation location, ParserIgnoreConfiguration config) + { + if (config?.CheckSkipElement(parentName, name) == true) + { + return true; } + + ProjectErrorUtilities.ThrowInvalidProject(location, "UnrecognizedChildElement", name, parentName); + return false; // Unreachable, but needed for compiler } /// diff --git a/src/Framework/Traits.cs b/src/Framework/Traits.cs index 8c6b2fe8846..1bea24823e6 100644 --- a/src/Framework/Traits.cs +++ b/src/Framework/Traits.cs @@ -275,6 +275,13 @@ internal class EscapeHatches /// public readonly bool AlwaysEvaluateDangerousGlobs = Environment.GetEnvironmentVariable("MSBuildAlwaysEvaluateDangerousGlobs") == "1"; + /// + /// Disables automatic loading of Directory.Parse.config from global locations + /// (MSBuild exe directory, user profile, MSBUILD_PARSE_CONFIG env var). + /// When set, configuration must be explicitly provided via ProjectCollection or BuildParameters. + /// + public readonly bool DisableParseConfig = Environment.GetEnvironmentVariable("MSBUILD_DISABLE_PARSE_CONFIG") == "1"; + /// /// Disables skipping full up to date check for immutable files. See FileClassifier class. /// diff --git a/src/Framework/Utilities/CollectionHelpers.cs b/src/Framework/Utilities/CollectionHelpers.cs index 624502e3196..01a2d332306 100644 --- a/src/Framework/Utilities/CollectionHelpers.cs +++ b/src/Framework/Utilities/CollectionHelpers.cs @@ -54,7 +54,7 @@ internal static bool SetEquivalent(IEnumerable a, IEnumerable b) return a.ToHashSet().SetEquals(b); } - internal static bool DictionaryEquals(IReadOnlyDictionary a, IReadOnlyDictionary b) + internal static bool DictionaryEquals(IReadOnlyDictionary a, IReadOnlyDictionary b, IEqualityComparer valueComparer = null) { if (a.Count != b.Count) { @@ -68,9 +68,19 @@ internal static bool DictionaryEquals(IReadOnlyDictionary a, IReadOn return false; } - if (!Equals(aKvp.Value, bValue)) + if (valueComparer != null) { - return false; + if (!valueComparer.Equals(aKvp.Value, bValue)) + { + return false; + } + } + else + { + if (!Equals(aKvp.Value, bValue)) + { + return false; + } } } diff --git a/src/MSBuild/XMake.cs b/src/MSBuild/XMake.cs index 989fbda0b6d..b4596c068a9 100644 --- a/src/MSBuild/XMake.cs +++ b/src/MSBuild/XMake.cs @@ -1703,7 +1703,8 @@ .. distributedLoggerRecords.Select(d => d.CentralLogger).Where(l => l is not nul enableTargetOutputLogging: isTaskAndTargetItemLoggingRequired, loadProjectsReadOnly: !isPreprocess, useAsynchronousLogging: true, - reuseProjectRootElementCache: s_isServerNode); + reuseProjectRootElementCache: s_isServerNode, + parseConfigDirectory: Path.GetDirectoryName(Path.GetFullPath(projectFile))); // globalProperties collection contains values only from CommandLine at this stage populated by ProcessCommandLineSwitches projectCollection.PropertiesFromCommandLine = [.. globalProperties.Keys];