Skip to content

Make CollectDeclaredReferences target incremental#136

Closed
SergeyTeplyakov wants to merge 1 commit intodfederm:mainfrom
SergeyTeplyakov:fix/incremental-collect-declared-references
Closed

Make CollectDeclaredReferences target incremental#136
SergeyTeplyakov wants to merge 1 commit intodfederm:mainfrom
SergeyTeplyakov:fix/incremental-collect-declared-references

Conversation

@SergeyTeplyakov
Copy link
Copy Markdown

Add Inputs/Outputs to the CollectDeclaredReferences target so MSBuild can skip it on incremental builds when project file and assets file haven't changed.

Two changes were needed:

  1. Target Inputs/Outputs: Use \ for the Outputs path instead of GetFullPath(), since GetFullPath() resolves relative to the process working directory rather than the project directory.

  2. Touch output file on unchanged content: SaveDeclaredReferences() has a write-only-if-different optimization that prevents updating the file timestamp when content is identical. This causes MSBuild to always consider the target out of date. Now we touch the file timestamp even when content is unchanged.

Tested on a 305-project build (NeMo/Hardware in Azure-Compute-Move):

  • Before: CollectDeclaredReferences 26.8s (305 executions, 0 skipped)
  • After: CollectDeclaredReferences 0s (305 skipped)

Add Inputs/Outputs to the CollectDeclaredReferences target so MSBuild
can skip it on incremental builds when project file and assets file
haven't changed.

Two changes were needed:

1. Target Inputs/Outputs: Use \
   for the Outputs path instead of GetFullPath(), since GetFullPath()
   resolves relative to the process working directory rather than the
   project directory.

2. Touch output file on unchanged content: SaveDeclaredReferences()
   has a write-only-if-different optimization that prevents updating
   the file timestamp when content is identical. This causes MSBuild
   to always consider the target out of date. Now we touch the file
   timestamp even when content is unchanged.

Tested on a 305-project build (NeMo/Hardware in Azure-Compute-Move):
- Before: CollectDeclaredReferences 26.8s (305 executions, 0 skipped)
- After:  CollectDeclaredReferences 0s (305 skipped)
DependsOnTargets="ResolveAssemblyReferences;PrepareProjectReferences"
Condition="'$(EnableReferenceTrimmer)' != 'false'"
Inputs="$(MSBuildProjectFullPath);$(ProjectAssetsFile)"
Outputs="$(MSBuildProjectDirectory)\$(IntermediateOutputPath)_ReferenceTrimmer_DeclaredReferences.tsv">
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Outputs="$(MSBuildProjectDirectory)\$(IntermediateOutputPath)_ReferenceTrimmer_DeclaredReferences.tsv">
Outputs="$([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)_ReferenceTrimmer_DeclaredReferences.tsv'))">

{
// Touch the file so that MSBuild's Inputs/Outputs incrementality check
// sees a fresh timestamp and can skip the target on subsequent builds.
File.SetLastWriteTimeUtc(filePath, DateTime.UtcNow);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole point of the if check is not to touch the timestamp if the file is identical, so this line should be reverted.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then incrementality never works for this target

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, but with the touch then the compiler's inputs changed and it triggers a recompile....

<Target Name="CollectDeclaredReferences"
DependsOnTargets="ResolveAssemblyReferences;PrepareProjectReferences"
Condition="'$(EnableReferenceTrimmer)' != 'false'"
Inputs="$(MSBuildProjectFullPath);$(ProjectAssetsFile)"
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many more inputs than this

@dfederm
Copy link
Copy Markdown
Owner

dfederm commented Apr 24, 2026

Superseded by #139

@dfederm dfederm closed this Apr 24, 2026
@SergeyTeplyakov
Copy link
Copy Markdown
Author

David, thanks for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants