Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,13 @@
// This is better handled by Regal's selection ranges implementation
"editor.smartSelect.selectLeadingAndTrailingWhitespace": false,
"editor.smartSelect.selectSubwords": false
}
},
// Enable to have values displayed inline during debugging
// The default value is "auto", which consults the language extension on whethher
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// The default value is "auto", which consults the language extension on whethher
// The default value is "auto", which consults the language extension on whether

// to have this enabled or not. And since the extension currently doesn't do anything
// on its own to support this, we need to explicitly enable it here if we want this.
// Long term, the extension + Regal should provide support for this, as it'll also
// result in a better experience when we can be more selective about what to show,
// and how.
"debug.inlineValues": "on"
}
4 changes: 2 additions & 2 deletions internal/io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ func WithCreateRecursive(path string, fn func(f *os.File) error) error {
return fn(file)
}

// FindInputPath consults the filesystem and returns the input.json or input.yaml closes to the
// file provided as arguments.
// FindInputPath consults the filesystem and returns the location of the input.json
// or input.yaml closest to the file provided.
func FindInputPath(file string, workspacePath string) string {
relative := strings.TrimPrefix(file, workspacePath)
components := strings.Split(filepath.Dir(relative), string(os.PathSeparator))
Expand Down
37 changes: 26 additions & 11 deletions internal/lsp/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,19 @@ func TestFindInputPath(t *testing.T) {
t.Fatalf("did not expect to find input.%s", tc.fileExt)
}

createWithContent(t, tmpDir+"/workspace/foo/bar/input."+tc.fileExt, tc.fileContent)
inputPath := filepath.Join(workspacePath, "foo", "bar", "input."+tc.fileExt)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

filepath.Join is quite clever, I think you should be able to do this, too:

Suggested change
inputPath := filepath.Join(workspacePath, "foo", "bar", "input."+tc.fileExt)
inputPath := filepath.Join(workspacePath, "foo/bar/input."+tc.fileExt)

Join also calls Clean, so it'll adjust your / and \ as needed, IIRC.

createWithContent(t, inputPath, tc.fileContent)

if path, exp := rio.FindInputPath(file, workspacePath), workspacePath+"/foo/bar/input."+tc.fileExt; path != exp {
if path, exp := rio.FindInputPath(file, workspacePath), inputPath; path != exp {
t.Errorf(`expected input at %s, got %s`, exp, path)
}

testutil.MustRemove(t, tmpDir+"/workspace/foo/bar/input."+tc.fileExt)
createWithContent(t, tmpDir+"/workspace/input."+tc.fileExt, tc.fileContent)
testutil.MustRemove(t, inputPath)

if path, exp := rio.FindInputPath(file, workspacePath), workspacePath+"/input."+tc.fileExt; path != exp {
workspaceInputPath := filepath.Join(workspacePath, "input."+tc.fileExt)
createWithContent(t, workspaceInputPath, tc.fileContent)

if path, exp := rio.FindInputPath(file, workspacePath), workspaceInputPath; path != exp {
t.Errorf(`expected input at %s, got %s`, exp, path)
}
})
Expand All @@ -141,18 +144,30 @@ func TestFindInput(t *testing.T) {
t.Fatalf("did not expect to find input.%s", tc.fileType)
}

createWithContent(t, tmpDir+"/workspace/foo/bar/input."+tc.fileType, tc.fileContent)
inputPath := filepath.Join(workspacePath, "foo", "bar", "input."+tc.fileType)

createWithContent(t, inputPath, tc.fileContent)

path, content := rio.FindInput(file, workspacePath)
if path != workspacePath+"/foo/bar/input."+tc.fileType || !maps.Equal(content, map[string]any{"x": true}) {
t.Errorf(`expected input {"x": true} at, got %s`, content)
if path != inputPath {
t.Errorf(`expected input at %s, got %s`, inputPath, path)
}

if !maps.Equal(content, map[string]any{"x": true}) {
t.Errorf(`expected input {"x": true}, got %s`, content)
}

testutil.MustRemove(t, tmpDir+"/workspace/foo/bar/input."+tc.fileType)
createWithContent(t, tmpDir+"/workspace/input."+tc.fileType, tc.fileContent)
testutil.MustRemove(t, inputPath)

workspaceInputPath := filepath.Join(workspacePath, "input."+tc.fileType)
createWithContent(t, workspaceInputPath, tc.fileContent)

path, content = rio.FindInput(file, workspacePath)
if path != workspacePath+"/input."+tc.fileType || !maps.Equal(content, map[string]any{"x": true}) {
if path != workspaceInputPath {
t.Errorf(`expected input at %s, got %s`, workspaceInputPath, path)
}

if !maps.Equal(content, map[string]any{"x": true}) {
t.Errorf(`expected input {"x": true} at, got %s`, content)
}
})
Expand Down
35 changes: 15 additions & 20 deletions pkg/linter/linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,27 +869,22 @@ func (l Linter) lintWithAggregateRules(
ctx, cancel := context.WithCancel(ctx)
defer cancel()

input := map[string]any{
// This will be replaced by the routing policy to provide each
// aggregate rule only the aggregated data from the same rule
"aggregates_internal": aggregates,
// There is no file provided in input here, but we'll provide *something* for
// consistency, and to avoid silently failing with undefined should someone
// refer to input.regal in an aggregate_report rule
"ignore_directives": ignoreDirectives,
"regal": map[string]any{
"operations": []string{"aggregate"},
"file": map[string]any{
"name": "__aggregate_report__",
"lines": []string{},
},
},
}
regal := ast.ObjectTerm(
ast.Item(ast.InternedTerm("operations"), ast.ArrayTerm(ast.InternedTerm("aggregate"))),
ast.Item(ast.InternedTerm("file"), ast.ObjectTerm(
ast.Item(ast.InternedTerm("name"), ast.InternedTerm("__aggregate_report__")),
ast.Item(ast.InternedTerm("lines"), ast.InternedEmptyArray),
)),
)
Comment on lines +872 to +878
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤔 I wonder if the compiler will raise this out of the function body, since it's constant... alternatively, you could do that, if this is called more than once, perhaps in the LSP scenario.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, definitely for the LSP scenario! Will update 👍


inputValue, err := transform.ToOPAInputValue(input)
if err != nil {
return report.Report{}, fmt.Errorf("failed to transform input value: %w", err)
}
aggParsed, _ := transform.ToOPAInputValue(aggregates)
dirParsed, _ := transform.ToOPAInputValue(ignoreDirectives)

inputValue := ast.NewObject(
ast.Item(ast.InternedTerm("aggregates_internal"), ast.NewTerm(aggParsed)),
ast.Item(ast.InternedTerm("ignore_directives"), ast.NewTerm(dirParsed)),
ast.Item(ast.InternedTerm("regal"), regal),
)

evalArgs := []rego.EvalOption{
rego.EvalParsedInput(inputValue),
Expand Down
2 changes: 1 addition & 1 deletion pkg/linter/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func TestLintWithPrintHook(t *testing.T) {
func TestLintWithAggregateRule(t *testing.T) {
t.Parallel()

policies := make(map[string]string)
policies := make(map[string]string, 2)
policies["foo.rego"] = `package foo
import data.bar

Expand Down
Loading