Skip to content

File-based apps: cache can be incorrectly reused when entrypoint symlink target changes (check is only mtime-based) #56065

Closed

Description

@yueyinqiu

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:

  1. Key the cache by the resolved symlink target (real path / file identity), not by the symlink path.
    If l.cs is 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 makes l.cs -> x.cs and l.cs -> y.cs naturally use different caches.
  2. Keep the cache keyed by the symlink path, but track changes to the symlink itself.
    If l.cs is 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) in build-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

  1. added
    untriaged
    on Sep 1, 2026
  2. locked and limited conversation to collaborators on Sep 1, 2026
  3. unlocked this conversation on Sep 1, 2026
  4. jjonescz commented on Sep 1, 2026

    @jjonescz
    Member

    Thank you for reporting this issue.

    What is your .NET SDK version?

  5. added
    Area-run-file
    on Sep 1, 2026
  6. yueyinqiu commented on Sep 1, 2026

    @yueyinqiu
    Author

    Tested on version 10.0.302, but the code being checked is the latest version:

    dotnet --info
    .NET SDK:
     Version:           10.0.302
     Commit:            35b593bebf
     Workload version:  10.0.300-manifests.1641d827
     MSBuild version:   18.6.11+35b593beb
    
    Runtime Environment:
     OS Name:     nixos
     OS Version:  26.11
     OS Platform: Linux
     RID:         linux-x64
     Base Path:   /nix/store/i0284n31fqnp52zd7kvpy5l3a6zk5dm0-dotnet-sdk-10.0.302/share/dotnet/sdk/10.0.302/
    
    .NET workloads installed:
    There are no installed workloads to display.
    Configured to use workload sets when installing new manifests.
    No workload sets are installed. Run "dotnet workload restore" to install a workload set.
    
    Host:
      Version:      10.0.10
      Architecture: x64
      Commit:       f7d90799ce
    
    .NET SDKs installed:
      10.0.302 [/nix/store/i0284n31fqnp52zd7kvpy5l3a6zk5dm0-dotnet-sdk-10.0.302/share/dotnet/sdk]
    
    .NET runtimes installed:
      Microsoft.AspNetCore.App 10.0.10 [/nix/store/i0284n31fqnp52zd7kvpy5l3a6zk5dm0-dotnet-sdk-10.0.302/share/dotnet/shared/Microsoft.AspNetCore.App]
      Microsoft.NETCore.App 10.0.10 [/nix/store/i0284n31fqnp52zd7kvpy5l3a6zk5dm0-dotnet-sdk-10.0.302/share/dotnet/shared/Microsoft.NETCore.App]
    
    Other architectures found:
      None
    
    Environment variables:
      Not set
    
    global.json file:
      Not found
    
    Learn more:
      https://aka.ms/dotnet/info
    
    Download .NET:
      https://aka.ms/dotnet/download
    

    Update: Also reproducible with 11.0.100-preview.6.26359.118:

    .NET SDK:
     Version:           11.0.100-preview.6.26359.118
     Commit:            ba53d0ed33
     Workload version:  11.0.100-manifests.f9248ba3
     MSBuild version:   18.9.0-preview-26359-118+ba53d0ed3
    
    Runtime Environment:
     OS Name:     nixos
     OS Version:  26.11
     OS Platform: Linux
     RID:         linux-x64
     Base Path:   /nix/store/i53mg866yd45wiw90lqx8vgjb9hbizb9-dotnet-sdk-11.0.100-preview.6.26359.118/share/dotnet/sdk/11.0.100-preview.6.26359.118/
    
    .NET workloads installed:
    There are no installed workloads to display.
    Configured to use workload sets when installing new manifests.
    No workload sets are installed. Run "dotnet workload restore" to install a workload set.
    
    Host:
      Version:      11.0.0-preview.6.26359.118
      Architecture: x64
      Commit:       ba53d0ed33
    
    .NET SDKs installed:
      11.0.100-preview.6.26359.118 [/nix/store/i53mg866yd45wiw90lqx8vgjb9hbizb9-dotnet-sdk-11.0.100-preview.6.26359.118/share/dotnet/sdk]
    
    .NET runtimes installed:
      Microsoft.AspNetCore.App 11.0.0-preview.6.26359.118 [/nix/store/i53mg866yd45wiw90lqx8vgjb9hbizb9-dotnet-sdk-11.0.100-preview.6.26359.118/share/dotnet/shared/Microsoft.AspNetCore.App]
      Microsoft.NETCore.App 11.0.0-preview.6.26359.118 [/nix/store/i53mg866yd45wiw90lqx8vgjb9hbizb9-dotnet-sdk-11.0.100-preview.6.26359.118/share/dotnet/shared/Microsoft.NETCore.App]
    
    Other architectures found:
      None
    
    Environment variables:
      Not set
    
    global.json file:
      Not found
    
    Learn more:
      https://aka.ms/dotnet/info
    
    Download .NET:
      https://aka.ms/dotnet/download
    
  7. added theissue type on Sep 1, 2026
  8. self-assigned this
    on Sep 1, 2026
  9. jjonescz commented on Sep 1, 2026

    @jjonescz
    Member

    This doesn't seem to be an issue with file-based apps only. If I create a project which has <Compile Include="linked.cs" /> where linked.cs is 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.cs to 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.cs force 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 #:include or 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.cs part (until msbuild is fixed, this change will have an effect only for the csc-only optimized runs).

  10. removed
    untriaged
    on Sep 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area-run-file

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions