Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix package restore happening on every script recompilation in projects #424

Conversation

NoTuxNoBux
Copy link
Contributor

@NoTuxNoBux NoTuxNoBux commented Nov 10, 2021

When NuGetForUnity is included in a user project, and they make changes to their scripts, Unity also reloads the editor scripts for NuGetForUnity, which will then retrigger the static NugetHelper constructor because it is marked as InitializeOnLoad. Because this constructor restores packages, a package restore is done every script reload. Usually this amounts to no actual change because the packages in question are already installed, but just checking one or two packages this way can already slow down the script recompilation process by several seconds, which quickly becomes tiresome.

This changes the restore to only happen once on project start-up, fixing the issue. There was also little benefit to the restore happening, as it didn't happen automatically when packages.config was modified, which would have been the most useful and already requires a manual reload.

Solution was proposed by @Shii2 in #407. I just formalized the change in a PR, also skipped doing I/O to check if the packages folder exists every time, and tested in a Unity 2020.3.20f1 project that it works as intended. Checks done:

  • Starting up a project still restores packages.
  • Making a change to a script no longer restores packages, even multiple times in succession.

When NuGetForUnity is included in a user project, and they make changes
to their scripts, Unity also reloads the editor scripts for
NuGetForUnity, which will then retrigger the static NugetHelper
constructor because it is marked as InitializeOnLoad. Because this
constructor restores packages, a package restore is done every script
reload. Usually this amounts to no actual change because the packages
in question are already installed, but just checking one or two
packages this way can already slow down the script recompilation
process by several seconds, which quickly becomes tiresome.

This changes the restore to only happen once on project start-up, fixing
the issue. There was also little benefit to the restore happening, as it
didn't happen automatically when packages.config was modified, which
would have been the most useful and already requires a manual reload.
Copy link
Collaborator

@jwittner jwittner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small change request, otherwise this looks like a decent solution.

Assets/NuGet/Editor/NugetHelper.cs Outdated Show resolved Hide resolved
@jwittner jwittner merged commit 16406e7 into GlitchEnzo:master Nov 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants