Repository navigation
Description
Describe the bug
When running a C# file-based app via a stable path that is a symlink, the file-based app build cache under may be incorrectly reused after the symlink target changes.
The cache directory is keyed by the entrypoint path string (the symlink path), so dotnet run l.cs continues to use the same cache folder even if l.cs is repointed from y.cs to x.cs. Whether a rebuild is needed is then decided by comparing timestamps: the resolved target file’s LastWriteTimeUtc is compared against the time of build-success.cache. If the new target’s mtime is not newer than the previous build time (common on Nix/NixOS where store mtimes are reproducible and may not reflect updates, but also reproducible with ordinary files), dotnet can decide “up to date” and run the stale previously-built output.
In other words, changing the symlink target is not reliably detected as an input change because the stale check is only mtime-based and does not persist/compare the previously resolved target identity (path/inode).
Steps to reproduce
cd "$(mktemp -d -p ./)"
echo 'Console.WriteLine("x");' > x.cs
echo 'Console.WriteLine("y");' > y.cs
ln -s y.cs l.cs
dotnet run l.cs # prints "y" -- it's expected
rm -f l.cs
ln -s x.cs l.cs
dotnet run l.cs # still prints "y"!!!
cd ..Expected behavior
I expect it to print x on the second run.
Actual behavior
It prints y.
Is this a regression?
No response
Are there any workarounds?
Explicitly specifying no-cache can avoid this. But it's generally not desirable to do so.
dotnet --info output
IDE version
No response
Other details
A “perfect” solution would be content-hash based: compute a hash over the entry file (and all build inputs it transitively depends on) and use that to validate/reuse the cache. However, the current design appears intentionally lightweight and avoids hashing large files / many inputs for performance reasons.
Given that constraint, there are at least two pragmatic alternatives:
- Key the cache by the resolved symlink target (real path / file identity), not by the symlink path.
Ifl.csis a symlink, resolve it and use the resolved target to derive the cache directory key (or store the resolved target identity in the cache entry and invalidate when it changes). This makesl.cs -> x.csandl.cs -> y.csnaturally use different caches. - Keep the cache keyed by the symlink path, but track changes to the symlink itself.
Ifl.csis a symlink, treat changes in the link (or link metadata) as an input change. Concretely: record the previously resolved target path (and/or file ID/inode) inbuild-success.cache, and invalidate if the current resolution differs, even if the new target’s mtime is not newer than the previous build time.
Either approach should address this scenario while keeping the design lightweight. I really hope this counter-intuitive (and practically problematic on Nix) behavior can be addressed.
Activity
jjonescz commentedon Sep 1, 2026
Thank you for reporting this issue.
What is your .NET SDK version?
yueyinqiu commentedon Sep 1, 2026
Tested on version
10.0.302, but the code being checked is the latest version:Update: Also reproducible with
11.0.100-preview.6.26359.118:jjonescz commentedon Sep 1, 2026
This doesn't seem to be an issue with file-based apps only. If I create a project which has
<Compile Include="linked.cs" />wherelinked.csis a symlinked file which changes its target, MSBuild's up-to-date checks also ignore that change.That also means that just changing
dotnet run file.csto detect the symlink target change won't have any effect when MSBuild is used under the hood, since MSBuild's up-to-date checks would then skip Csc anyway.We could make
dotnet run file.csforce rebuild or resolve the symlink and produce a project under the hood with<Compile Include="target.cs" />but those wouldn't solve additional files included through#:includeor implicit build files.So I think this needs to be fixed in MSBuild, too. Here seems to be the corresponding tracking issue: dotnet/msbuild#13465
I will fix the
dotnet run file.cspart (until msbuild is fixed, this change will have an effect only for the csc-only optimized runs).