@@ -64,6 +64,13 @@ func Parse(filePath string, preprocessor Preprocessor, options *protocols.Execut
6464 newBase .TemplateInfo = tplCopy .Options .TemplateInfo
6565 newBase .TemplateVerifier = tplCopy .Options .TemplateVerifier
6666 newBase .RawTemplate = tplCopy .Options .RawTemplate
67+
68+ if tplCopy .Options .Variables .Len () > 0 {
69+ newBase .Variables = tplCopy .Options .Variables
70+ }
71+ if len (tplCopy .Options .Constants ) > 0 {
72+ newBase .Constants = tplCopy .Options .Constants
73+ }
6774 tplCopy .Options = newBase
6875
6976 tplCopy .Options .ApplyNewEngineOptions (options )
@@ -156,12 +163,16 @@ func Parse(filePath string, preprocessor Preprocessor, options *protocols.Execut
156163 // Compile the workflow request
157164 if len (template .Workflows ) > 0 {
158165 compiled := & template .Workflow
159- compileWorkflow (filePath , preprocessor , options , compiled , options .WorkflowLoader )
166+ compileWorkflow (filePath , preprocessor , tplCopy . Options , compiled , tplCopy . Options .WorkflowLoader )
160167 template .CompiledWorkflow = compiled
161- template .CompiledWorkflow .Options = options
168+ template .CompiledWorkflow .Options = tplCopy .Options
169+ }
170+
171+ if isCachedTemplateValid (template ) {
172+ // options.Logger.Error().Msgf("returning cached template %s after recompiling %d requests", tplCopy.Options.TemplateID, tplCopy.Requests())
173+ return template , nil
162174 }
163- // options.Logger.Error().Msgf("returning cached template %s after recompiling %d requests", tplCopy.Options.TemplateID, tplCopy.Requests())
164- return template , nil
175+ // else: fallthrough to re-parse template from scratch
165176 }
166177 }
167178
@@ -579,6 +590,50 @@ func parseTemplate(data []byte, srcOptions *protocols.ExecutorOptions) (*Templat
579590 return template , nil
580591}
581592
593+ // isCachedTemplateValid validates that a cached template is still usable after
594+ // option updates
595+ func isCachedTemplateValid (template * Template ) bool {
596+ // no requests or workflows
597+ if template .Requests () == 0 && len (template .Workflows ) == 0 {
598+ return false
599+ }
600+
601+ // options not initialized
602+ if template .Options == nil {
603+ return false
604+ }
605+
606+ // executer not available for non-workflow template
607+ if len (template .Workflows ) == 0 && template .Executer == nil {
608+ return false
609+ }
610+
611+ // compiled workflow not available
612+ if len (template .Workflows ) > 0 && template .CompiledWorkflow == nil {
613+ return false
614+ }
615+
616+ // template ID mismatch
617+ if template .Options .TemplateID != template .ID {
618+ return false
619+ }
620+
621+ // executer exists but no requests or flow available
622+ if template .Executer != nil {
623+ // NOTE(dwisiswant0): This is a basic sanity check since we can't access
624+ // private fields, but we can check requests tho
625+ if template .Requests () == 0 && template .Options .Flow == "" {
626+ return false
627+ }
628+ }
629+
630+ if template .Options .Options == nil {
631+ return false
632+ }
633+
634+ return true
635+ }
636+
582637var (
583638 jsCompiler * compiler.Compiler
584639 jsCompilerOnce = sync .OnceFunc (func () {
0 commit comments