'Hugo rebuild loop when using Visual Studio Code

When I open a Hugo project in Visual Studio Code and run hugo server, the Hugo server goes into a constant rebuild cycle, even though I'm not making any changes to the content. For example:

Change detected, rebuilding site.
2022-02-01 10:35:45.916 +0000
Rebuilt in 856 ms

Change detected, rebuilding site.
2022-02-01 10:35:52.415 +0000
Rebuilt in 861 ms

If I'm viewing a page in a web browser, this in turn triggers a page reload every few seconds.

If VSCode is closed this is not an issue; similarly, I don't get this if I edit in TextMate.

I'm assuming that VSCode must be modifying some files, and that the Hugo server is detecting those changes.

How can I stop this? For example, is there a Hugo config element that I could change to ignore changes to certain files that are only touched by VSCode?

For additional information such as versions, this is the output from starting Hugo:

Start building sites … 
hugo v0.89.4+extended darwin/amd64 BuildDate=unknown

                   | EN  
-------------------+-----
  Pages            | 43  
  Paginator pages  |  0  
  Non-page files   |  4  
  Static files     | 43  
  Processed images | 10  
  Aliases          |  3  
  Sitemaps         |  1  
  Cleaned          |  0  

Built in 688 ms
Watching for changes in /Users/stevie/dev/gowork/src/github.com/stephen-masters/hugo-test/{assets,content,layouts,package.json,themes}
Watching for config changes in /Users/stevie/dev/gowork/src/github.com/stephen-masters/hugo-test/config.toml, /Users/stevie/dev/gowork/src/github.com/stephen-masters/hugo-test/themes/docsy/config.toml
Environment: "development"
Serving pages from memory
Running in Fast Render Mode.


Solution 1:[1]

I ran into this problem today and I guess I have what may not be a very satisfying solution for others.

I had cloned a repo and started making some small changes and noticed that Hugo was going into a constant rebuild loop as you described. If I did a hugo server outside of VS Code, it did not loop.

I had only made two small changes to the original project. I loaded that project up in VSCode and it did NOT loop. The changes were to some node modules that npm noted had vulnerabilities and to some code that used a js slider module. I figured it was one of these so I backed everything out and pulled the original repo down. I verified that it did not loop and then started re-doing the changes I had made.

After getting everything back in, hugo sever was NOT looping.

I can't explain it. It seems like something got confused inside VS Code and caused this problem and some sequence of re-starting or re-loading cleared it up.

I know there are other SO posts about this problem and other people have done a lot of troubleshooting to try and solve it.

I guess my suggested solution is to either shut down and restart VSC or try and back-out and re-do your recent changes.

Solution 2:[2]

I had faced the same issue. Only difference was I was using atom IDE to edit the code.

I had duplicated an existing project folder and imported it in atom. The issue started only when I duplicated this folder and started hugo server. And the issue was only with this new hugo project only. All old projects were running fine even now.

After some debugging, I removed the .git directory from the new project and it stopped rebuilding the hugo server. Don't know why this was happening but the code editors like VS code and atom etc reference the .git directory when you duplicate the project folder that is already version controlled.

I hope this helps someone.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tmhudg
Solution 2 Sandeep_Patil