5

I recently noticed some strange behaviour in Visual Studio. If I edit my appsettings.json file then run in debug F5, the changed config is not picked up by the compiler.

AndyS
  • 750
  • 5
  • 14

5 Answers5

5

The reason appears to be due to a new feature, called Build Acceleration, introduced into Visual Studio v17.5 (Feb 2023)

Release notes: https://learn.microsoft.com/en-us/visualstudio/releases/2022/release-notes
Build Acceleration: https://github.com/dotnet/project-system/blob/main/docs/build-acceleration.md

enter image description here

Build Acceleration will only recompile projects that have code changes. It won't pick up changes to settings files that have Build Action: None, which is the default value. Previously VS would run MSBuild, which would update changed files and run a build. Now VS handles the file changes itself, and only calls MSBuild when needed. The result is that edits on the settings file don't get picked up, unless you also edit a code file, or explicitly build (Ctrl+B or Ctrl+Shift+B) before running debug.

Test project on GitHub

EDIT: After a bit of testing, Build Action: Embedded Resource seems to work best. I tried Content but still got the same issue and Resource threw a runtime error. Grab the test project above if you want to experiment.

AndyS
  • 750
  • 5
  • 14
  • Based on the highlighted text it should still copy new content files. Sound like a bug to me. – Guru Stron Mar 21 '23 at 12:01
  • @GuruStron good point. It probably _is_ copying the new content, but it's not passing it to the compile - maybe the compiler is using a cached copy? – AndyS Mar 21 '23 at 12:06
  • There is no compilation involved in case of Build Acceleration (as per docs and other things like explicit build). I don't have VS on my machine (because Ubuntu) so I will not be able to repro anyway but I would recommend still to create a [mre] and possible create a VS bug. – Guru Stron Mar 21 '23 at 12:10
  • I tried to create new project to check the default build action of appsettings.json, and it is `Content` not `None`. – tia Mar 21 '23 at 12:22
  • Hi @tia, thanks for the feedback. I just created a new test project Console app (VS 2022 - v17.5.2), added a settings,json file and the default was `None`, `Do Not Copy`. I changed it to `Content`, `Copy always` and I still have the issue described. After testing several settings I found that `Build Action: Embedded Resource` is the only one that works. Will drop a test project on Github and add the link here. – AndyS Mar 21 '23 at 17:13
  • @AndyS You are right for new file, but if you create new ASP.NET project it comes with `Content` and `Copy If Newer`. Anyway, I also can reproduce the issue you have found. One thing I'd like to add is that if there are any changes in Program.cs, the new settings will be picked up. – tia Mar 22 '23 at 06:50
  • I can reproduce your problem and I will report this finding. In general, I tend to clean and rebuild programs after modifying files. – Jiale Xue - MSFT Mar 22 '23 at 09:28
  • @JamesSkemp the bug here is not with Build Acceleration. If you hit _Build_ before pressing F5 everything works correctly. It's being tracked in this ticket. Please vote on it if it's impacting you: https://developercommunity.visualstudio.com/t/Copy-if-newer---Issue-when-only-non-sour/10308822 – Drew Noakes Apr 18 '23 at 02:54
2

This is a known bug in VS and is being tracked by:

https://developercommunity.visualstudio.com/t/Copy-if-newer---Issue-when-only-non-sour/10308822

Please vote on the ticket if its impacting you.

A workaround is to hit Build before pressing F5. The problem only occurs when launching the debugger in this way, as the build is skipped.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
0

Faced the same issue there is one solution to it but yes, that is not convenient for all but until they provide a fix, this can be done.

  1. Run cleanup.
  2. Run rebuild solution.

Then, when you run the code it will start picking up changes in appsettings.json file. Still searching for better solution will update if I find one.

XouDo
  • 945
  • 10
  • 19
0

My workaround is:

  1. Open appsettings.json properties
  2. Change 'Copy to output directory' for some another value than you have
  3. Ctrl + S
  4. Return 'Copy to output directory' to the original value
  5. Ctrl + S
  6. Build and run.

Terrible solution tho...

0

Fixed on Visual Studio 17.7 release August 8, 2023:

https://learn.microsoft.com/en-us/visualstudio/releases/2022/release-notes#17.7.0

DevRacker
  • 246
  • 1
  • 6