Changes to appsettings.json config files not picked up?
Asked Answered
T

5

7

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.

Trek answered 21/3, 2023 at 11:55 Comment(1)
This was fixed in VS 17.7 via github.com/dotnet/project-system/pull/9130Fleabag
T
7

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.

Trek answered 21/3, 2023 at 11:55 Comment(8)
Based on the highlighted text it should still copy new content files. Sound like a bug to me.Floe
@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?Trek
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 minimal reproducible example and possible create a VS bug.Floe
I tried to create new project to check the default build action of appsettings.json, and it is Content not None.Spitz
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.Trek
@Trek 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.Spitz
I can reproduce your problem and I will report this finding. In general, I tend to clean and rebuild programs after modifying files.Gleesome
@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: developercommunity.visualstudio.com/t/…Fleabag
F
4

EDIT This issue was fixed in VS 17.7 via this PR.


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.

Fleabag answered 18/4, 2023 at 2:57 Comment(6)
According to the link you mentioned, this issue has been fixed in the meantime (Aug 09 2023)Undecagon
You're right! I fixed this in github.com/dotnet/project-system/pull/9130Fleabag
Strange I still have this issue now (VS 17.10, July 2024)Summersault
@LukeVo please file a new issue on that repo with steps showing how we can reproduce the bug ourselves. Thanks!Fleabag
@DrewNoakes thanks, just to be sure, the issue is that changes to appsettings.json is not picked up by MSBuild right (unless I make another change to a code file)? The issue linked seem to be a different issue.Summersault
@LukeVo it's the same thing but with a different symptom. Most people hit it with appSettings.json. Please create an issue and include as much information as possible. If you can enable verbose fast up-to-date check logging (in Tools | Options) and include that output when a build doesn't go as expected, that'd be very helpful. Thanks.Fleabag
A
1

Fixed on Visual Studio 17.7 release August 8, 2023:

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

Anastrophe answered 10/8, 2023 at 0:46 Comment(0)
E
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.

Exeunt answered 28/4, 2023 at 17:53 Comment(0)
T
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...

Travesty answered 8/6, 2023 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.