Skip to content

Commit a5e96aa

Browse files
authored
Handle newlines in dependencies (#147)
1 parent 7066aa3 commit a5e96aa

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

internal/rego/rego.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rego
22

33
import (
4+
"bytes"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -228,11 +229,17 @@ func parseDirectory(directory string) ([]Rego, error) {
228229
return nil, fmt.Errorf("compile: %w", err)
229230
}
230231

231-
// Re-key the loaded rego file map based on the package path of the rego file.
232-
// This makes finding the source rego file from an import path much easier.
233232
files := make(map[string]*loader.RegoFile)
234-
for _, regoFile := range result.Modules {
235-
files[regoFile.Parsed.Package.Path.String()] = regoFile
233+
for m := range result.Modules {
234+
235+
// Many YAML parsers do not like rendering out CRLF when writing the YAML to disk.
236+
// This causes ConstraintTemplates to be rendered with the line breaks as text,
237+
// rather than the actual line break.
238+
result.Modules[m].Raw = bytes.ReplaceAll(result.Modules[m].Raw, []byte("\r"), []byte(""))
239+
240+
// Re-key the loaded rego file map based on the package path of the rego file.
241+
// This makes finding the source rego file from an import path much easier.
242+
files[result.Modules[m].Parsed.Package.Path.String()] = result.Modules[m]
236243
}
237244

238245
var regos []Rego
@@ -285,11 +292,6 @@ func parseDirectory(directory string) ([]Rego, error) {
285292
}
286293
}
287294

288-
// Many YAML parsers do not like rendering out CRLF when writing the YAML to disk.
289-
// This causes ConstraintTemplates to be rendered with the line breaks as text,
290-
// rather than the actual line break.
291-
raw := strings.ReplaceAll(string(file.Raw), "\r", "")
292-
293295
rego := Rego{
294296
id: getPolicyID(file.Parsed.Rules),
295297
path: file.Name,
@@ -298,7 +300,7 @@ func parseDirectory(directory string) ([]Rego, error) {
298300
parameters: headerParams,
299301
headerComments: headerComments,
300302
comments: comments,
301-
raw: raw,
303+
raw: string(file.Raw),
302304
skipConstraint: hasSkipConstraintTag(headerComments),
303305
}
304306

0 commit comments

Comments
 (0)