@@ -164,6 +164,9 @@ type LanguageServer struct {
164164
165165 workspaceRootURI string
166166 workspaceDiagnosticsPoll time.Duration
167+
168+ // Flag used to suppress input.json prompt if user chooses to ignore it
169+ supressInputPrompt bool
167170}
168171
169172// lintFileJob is sent to the lintFileJobs channel to trigger a
@@ -2578,6 +2581,8 @@ func (l *LanguageServer) handleEvalCommand(ctx context.Context, args types.Comma
25782581
25792582 var inputMap map [string ]any
25802583
2584+ var inputPath string
2585+
25812586 // When the first comment in the file is `regal eval: use-as-input`, the AST of that module is
25822587 // used as the input rather than the contents of input.json/yaml. This is a development feature for
25832588 // working on rules (built-in or custom), allowing querying the AST of the module directly.
@@ -2593,7 +2598,33 @@ func (l *LanguageServer) handleEvalCommand(ctx context.Context, args types.Comma
25932598 // Normal mode — try to find the input.json/yaml file in the workspace and use as input
25942599 // NOTE that we don't break on missing input, as some rules don't depend on that, and should
25952600 // still be evaluable. We may consider returning some notice to the user though.
2596- _ , inputMap = rio .FindInput (uri .ToPath (args .Target ), l .workspacePath ())
2601+ inputPath , inputMap = rio .FindInput (uri .ToPath (args .Target ), l .workspacePath ())
2602+ if inputPath == "" && ! l .supressInputPrompt {
2603+ var action types.MessageActionItem
2604+
2605+ reqParams := types.ShowMessageRequestParams {
2606+ Type : 3 , // info
2607+ Message : "No input.json/yaml file was found. " +
2608+ "This file is used to provide input data to your rules. " +
2609+ "Would you like to create one?" ,
2610+ Actions : []types.MessageActionItem {
2611+ {Title : "Yes" },
2612+ {Title : "No" },
2613+ {Title : "Ignore" },
2614+ },
2615+ }
2616+
2617+ if err = l .conn .Call (ctx , "window/showMessageRequest" , reqParams , & action ); err != nil {
2618+ l .log .Message ("window/showMessageRequest failed: %v" , err )
2619+ } else if action .Title == "Yes" {
2620+ inputFile := filepath .Join (l .workspacePath (), "input.json" )
2621+ if err = os .WriteFile (inputFile , []byte ("{}\n " ), 0o600 ); err != nil {
2622+ l .log .Message ("failed to create input.json: %v" , err )
2623+ }
2624+ } else if action .Title == "Ignore" {
2625+ l .supressInputPrompt = true
2626+ }
2627+ }
25972628 }
25982629
25992630 var result EvalResult
0 commit comments