Skip to content

Commit 963af2b

Browse files
authored
Switch CsWin32RunAsBuildTask to EmitSingleFile by default (for VS incremental scenario) (#1565)
1 parent 3cf50ce commit 963af2b

5 files changed

Lines changed: 11 additions & 14 deletions

File tree

src/CsWin32Generator/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ private Task<bool> GenerateCode(
264264
options.ComInterop.UseComSourceGenerators = true;
265265
}
266266

267+
// Set EmitSingleFile to true by default if unspecified.
268+
// We put everything in a single file to avoid needing to kick design-time-build to re-run to pick up the newly generated files.
269+
// We _could_ make this happen using the AdditionalDesignTimeBuildInput item group but that makes design time build re-run each time
270+
// NativeMethods.txt changes and we want to avoid triggering DTBs if possible.
271+
options.EmitSingleFile ??= true;
272+
267273
this.VerboseWriteLine($"Loaded generator options. AllowMarshaling: {options.AllowMarshaling}, ClassName: {options.ClassName}");
268274

269275
// Validate metadata files exist

src/Microsoft.Windows.CsWin32/Generator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ nsContents.Key is object
775775
: nsContents.ToArray());
776776
}
777777

778-
if (this.options.EmitSingleFile)
778+
if (this.options.EmitSingleFile == true)
779779
{
780780
CompilationUnitSyntax file = CompilationUnit()
781781
.AddMembers(starterNamespace.AddMembers(GroupMembersByNamespace(this.NamespaceMembers).ToArray()))
@@ -862,7 +862,7 @@ nsContents.Key is object
862862

863863
if (this.compilation?.GetTypeByMetadataName("System.Reflection.AssemblyMetadataAttribute") is not null)
864864
{
865-
if (this.options.EmitSingleFile)
865+
if (this.options.EmitSingleFile == true)
866866
{
867867
KeyValuePair<string, CompilationUnitSyntax> originalEntry = normalizedResults.Single();
868868
normalizedResults[originalEntry.Key] = originalEntry.Value.WithLeadingTrivia().AddAttributeLists(CsWin32StampAttribute).WithLeadingTrivia(originalEntry.Value.GetLeadingTrivia());

src/Microsoft.Windows.CsWin32/GeneratorOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public record GeneratorOptions
2020
public string ClassName { get; set; } = "PInvoke";
2121

2222
/// <summary>
23-
/// Gets or sets a value indicating whether to emit a single source file as opposed to types spread across many files.
23+
/// Gets or sets a value indicating whether to emit a single source file as opposed to types spread across many files, 'null' indicates to use the recommended default for the environment.
2424
/// </summary>
25-
/// <value>The default value is <see langword="false" />.</value>
26-
public bool EmitSingleFile { get; set; }
25+
/// <value>The default value is <see langword="null" />.</value>
26+
public bool? EmitSingleFile { get; set; }
2727

2828
/// <summary>
2929
/// Gets or sets a value indicating whether to expose the generated APIs publicly (as opposed to internally).

src/Microsoft.Windows.CsWin32/build/Microsoft.Windows.CsWin32.targets

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,4 @@
9595
</ItemGroup>
9696

9797
</Target>
98-
99-
<!-- Add the items to AdditionalDesignTimeBuildInput as well so that DesignTimeBuilds re-run when NativeMethods.txt contents change.
100-
VS seems to evaluate this directly (not looking at the DesignTimeBuild outputs) and so we can't do it in a Target.
101-
And VS doesn't support conditions on FileName or things like that in a top-level ItemGroup so just include all AdditionalFiles
102-
as ContentSensitive. Worst case the DesignTimeBuild runs on files other than NativeMethods.txt. -->
103-
<ItemGroup Condition="'$(CsWin32RunAsBuildTask)'=='true'">
104-
<AdditionalDesignTimeBuildInput Include="@(AdditionalFiles)" ContentSensitive="true" />
105-
</ItemGroup>
10698
</Project>

test/GenerationSandbox.BuildTask.Tests/NativeMethods.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"$schema": "..\\..\\src\\Microsoft.Windows.CsWin32\\settings.schema.json",
3-
"emitSingleFile": true,
43
"allowMarshaling": true,
54
"public": false,
65
"comInterop": {

0 commit comments

Comments
 (0)