Skip to content

Commit f7fb432

Browse files
committed
Never perform clean in a design-time build
1 parent 1f5c0e7 commit f7fb432

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

src/PackageReferenceCleaner/CleanPackageReferences.cs

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Immutable;
2-
using System.Diagnostics;
32
using System.IO;
43
using System.Linq;
54
using System.Xml.Linq;
@@ -29,31 +28,37 @@ public override void Initialize(AnalysisContext context)
2928
var projectFile = globalOptions.TryGetValue("build_property.MSBuildProjectFullPath", out var fullPath) ?
3029
fullPath : null;
3130

32-
if (!string.IsNullOrEmpty(projectFile) && File.Exists(projectFile))
31+
var designTimeBuild = globalOptions.TryGetValue("build_property.DesignTimeBuild", out var dtb) &&
32+
bool.TryParse(dtb, out var isDtb) && isDtb ? true : false;
33+
34+
// Don't clean during DTB since the project file might be edited in-memory and not yet saved.
35+
if (designTimeBuild ||
36+
string.IsNullOrEmpty(projectFile) ||
37+
!File.Exists(projectFile))
38+
return;
39+
40+
var doc = XDocument.Load(projectFile, LoadOptions.PreserveWhitespace);
41+
var references = doc.Descendants()
42+
.Where(e => e.Name.LocalName == "PackageReference" && e.Elements().Any())
43+
.Where(e => e.Attribute("PrivateAssets")?.Value == "all" || e.Element("PrivateAssets")?.Value == "all");
44+
45+
var cleaned = 0;
46+
47+
foreach (var item in references)
48+
{
49+
var attributes = item.Attributes().ToArray();
50+
item.RemoveAll();
51+
item.Add(attributes);
52+
if (item.Attribute("PrivateAssets") == null)
53+
item.Add(new XAttribute("PrivateAssets", "all"));
54+
55+
cleaned++;
56+
}
57+
58+
if (cleaned > 0)
3359
{
34-
var doc = XDocument.Load(projectFile, LoadOptions.PreserveWhitespace);
35-
var references = doc.Descendants()
36-
.Where(e => e.Name.LocalName == "PackageReference" && e.Elements().Any())
37-
.Where(e => e.Attribute("PrivateAssets")?.Value == "all" || e.Element("PrivateAssets")?.Value == "all");
38-
39-
var cleaned = 0;
40-
41-
foreach (var item in references)
42-
{
43-
var attributes = item.Attributes().ToArray();
44-
item.RemoveAll();
45-
item.Add(attributes);
46-
if (item.Attribute("PrivateAssets") == null)
47-
item.Add(new XAttribute("PrivateAssets", "all"));
48-
49-
cleaned++;
50-
}
51-
52-
if (cleaned > 0)
53-
{
54-
ctx.ReportDiagnostic(Diagnostic.Create(SupportedDiagnostics[0], null, cleaned));
55-
doc.Save(projectFile);
56-
}
60+
ctx.ReportDiagnostic(Diagnostic.Create(SupportedDiagnostics[0], null, cleaned));
61+
doc.Save(projectFile);
5762
}
5863
});
5964
}

src/PackageReferenceCleaner/PackageReferenceCleaner.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</ItemGroup>
2828

2929
<ItemGroup>
30-
<PackageFile Include="PackageReferenceCleaner.targets" PackagePath="buildTransitive/%(Filename)%(Extension)" />
30+
<PackageFile Include="PackageReferenceCleaner.targets" PackagePath="buildTransitive/%(Filename)%(Extension)" Visible="true" />
3131
</ItemGroup>
3232

3333
</Project>

src/PackageReferenceCleaner/PackageReferenceCleaner.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<Project>
22

33
<ItemGroup>
4+
<CompilerVisibleProperty Include="DesignTimeBuild" />
45
<CompilerVisibleProperty Include="MSBuildProjectFullPath" />
56
</ItemGroup>
67

0 commit comments

Comments
 (0)