StyraInc / regal

Regal is a linter and language server for Rego, bringing your policy development experience to the next level!
https://docs.styra.com/regal
Apache License 2.0
261 stars 35 forks source link

feature: lint stdin #1118

Closed srenatus closed 1 month ago

srenatus commented 1 month ago

It would be nice to be able to pipe some Rego into Regal, and have it output anything it could complain about:

magic-command-outputting-rego | regal lint -

Now, anything that depends on file names would automatically be disabled: we have no file name.

Not a pressing issue in any way. I suppose the user base for this feature is imperceptibly small.

anderseknert commented 1 month ago

Not sure what you mean... how is this not ergonomic? 😄

ln -s /dev/stdin p.rego
cat lab/p.rego | regal lint p.rego

Rule:           directory-package-mismatch
Description:    Directory structure should mirror package
Category:       idiomatic
Location:       p.rego:2:9
Text:           package lab
Documentation:  https://docs.styra.com/regal/rules/idiomatic/directory-package-mismatch

Rule:           file-missing-test-suffix
Description:    Files containing tests should have a _test.rego suffix
Category:       testing
Location:       p.rego
Documentation:  https://docs.styra.com/regal/rules/testing/file-missing-test-suffix

Rule:           test-outside-test-package
Description:    Test outside of test package
Category:       testing
Location:       p.rego:10:1
Text:           test_fool if {
Documentation:  https://docs.styra.com/regal/rules/testing/test-outside-test-package

1 file linted. 3 violations found.

This actually killed the language server though, lol

2024/09/19 15:44:24 jsonrpc2: protocol error: failed to read from stdin: read /dev/stdin: resource temporarily unavailable
Connection closed
[Error - 3:44:24 PM] Connection to server got closed. Server will not be restarted.
[Error - 3:44:24 PM] Server process exited with code 0.

In seriousness though, yeah, we should allow - in any place where we currently expect a file... so i.e. --config-file - should work too.

In the case of regal lint -, regal lint /dev/stdin would work today already but reports 0 files linted since no .rego file is provided.. which is why the above symlink hack was needed :P

srenatus commented 1 month ago

If it only assumed .rego when there's no file extension...? That would perhaps help a bit already.

anderseknert commented 1 month ago

Indeed. Sort of related to #328, although it's not clear to me how "any file with no extension" would be expressed in config. We could just make an exception for /dev/stdin, and - like you originally suggested.

srenatus commented 1 month ago

Re: "What others are doing": prettier is reading from STDIN and allows providing a filename of that.

prettier --stdin-filepath foo.js

(But also for my purposes here, reading from - as implemented is enough.)

anderseknert commented 1 month ago

Yeah, if we have a good case for that later, we can add it. But as long as we can avoid flag pollution, let's do it :)