@@ -35,7 +35,7 @@ public class NodeFunctionInvoker : FunctionInvokerBase
3535 private readonly string _entryPoint ;
3636
3737 private Func < object , Task < object > > _scriptFunc ;
38- private Func < object , Task < object > > _clearRequireCache ;
38+ private static Func < object , Task < object > > _clearRequireCache ;
3939 private static Func < object , Task < object > > _globalInitializationFunc ;
4040 private static string _functionTemplate ;
4141 private static string _clearRequireCacheScript ;
@@ -98,7 +98,7 @@ private Func<object, Task<object>> ScriptFunc
9898 }
9999 }
100100
101- private Func < object , Task < object > > ClearRequireCacheFunc
101+ private static Func < object , Task < object > > ClearRequireCacheFunc
102102 {
103103 get
104104 {
@@ -238,6 +238,11 @@ protected override void OnScriptFileChanged(object sender, FileSystemEventArgs e
238238 return ;
239239 }
240240
241+ // clear the node module cache
242+ // This is done for any files to ensure that, if a file change triggers
243+ // a host restart, we leave the cache clean.
244+ ClearRequireCacheFunc ( null ) . GetAwaiter ( ) . GetResult ( ) ;
245+
241246 // The ScriptHost is already monitoring for changes to function.json, so we skip those
242247 string fileName = Path . GetFileName ( e . Name ) ;
243248 if ( string . Compare ( fileName , ScriptConstants . FunctionMetadataFileName , StringComparison . OrdinalIgnoreCase ) != 0 )
@@ -246,9 +251,6 @@ protected override void OnScriptFileChanged(object sender, FileSystemEventArgs e
246251 // force a reload on next execution
247252 _scriptFunc = null ;
248253
249- // clear the node module cache
250- ClearRequireCacheFunc ( null ) . GetAwaiter ( ) . GetResult ( ) ;
251-
252254 TraceOnPrimaryHost ( string . Format ( CultureInfo . InvariantCulture , "Script for function '{0}' changed. Reloading." , Metadata . Name ) , System . Diagnostics . TraceLevel . Info ) ;
253255 }
254256 }
0 commit comments