cedar-policy / cedar

Implementation of the Cedar Policy Language
https://www.cedarpolicy.com
Apache License 2.0
873 stars 79 forks source link

CLI cedar format does not tolerate policy files with trailing newline #1217

Closed adamrothman closed 1 month ago

adamrothman commented 1 month ago

Before opening, please confirm:

Bug Category

Other

Describe the bug

Given a file containing the following policy (taken from the Cedar docs), with a trailing newline:

// Jane's friends can view all photos in her janeTrips album
permit (
  principal in Group::"janeFriends",
  action in [Action::"view", Action::"comment"],
  resource in Album::"janeTrips"
);

The cedar format --check command exits with a non-0 status:

$ cedar format --policies ./test.cedar --line-width 80 --indent-width 2 --check --error-format human
// Jane's friends can view all photos in her janeTrips album
permit (
  principal in Group::"janeFriends",
  action in [Action::"view", Action::"comment"],
  resource in Album::"janeTrips"
);

adam.rothman at adamrot-ltmnvzm in ~/Desktop 
$ echo $?
1

Expected behavior

The format command should not treat a policy file that ends with a newline as malformed or in need of formatting. It's very common for editors to automatically add a trailing newline to files on save, and quite a pain to try to disable this feature for only .cedar files.

When invoked with --check, policy files that end with a trailing newline should pass (i.e. exit status 0).

When invoked with --write, the CLI should not remove trailing newlines from policy files where they are present.

Reproduction steps

  1. Install cedar-policy-cli crate.
  2. Save the example policy above into a file called test.cedar, ensuring that a trailing newline is added.
  3. Invoke cedar format --check.
  4. Check the exit code of the previous command.
$ cargo install cedar-policy-cli  
    Updating crates.io index
  Downloaded cedar-policy-cli v4.0.0
  Downloaded 1 crate (43.5 KB) in 0.22s
  Installing cedar-policy-cli v4.0.0
<snip>
   Compiling cedar-policy-cli v4.0.0
    Finished `release` profile [optimized] target(s) in 34.42s
  Installing /Users/adam.rothman/.cargo/bin/cedar
   Installed package `cedar-policy-cli v4.0.0` (executable `cedar`)

$ cedar -V
cedar-policy-cli 4.0.0

$ cat test.cedar 
// Jane's friends can view all photos in her janeTrips album
permit (
  principal in Group::"janeFriends",
  action in [Action::"view", Action::"comment"],
  resource in Album::"janeTrips"
);

$ cat -e test.cedar 
// Jane's friends can view all photos in her janeTrips album$
permit ($
  principal in Group::"janeFriends",$
  action in [Action::"view", Action::"comment"],$
  resource in Album::"janeTrips"$
);$

$ cedar format --policies ./test.cedar --line-width 80 --indent-width 2 --check --error-format human
// Jane's friends can view all photos in her janeTrips album
permit (
  principal in Group::"janeFriends",
  action in [Action::"view", Action::"comment"],
  resource in Album::"janeTrips"
);

$ echo $?
1

Code Snippet

No response

Log output

No response

Additional configuration

No response

Operating System

macOS Sonoma 14.7

Additional information and screenshots

No response

khieta commented 1 month ago

Thanks for opening this issue! That behavior does indeed sound annoying. Should be a quick fix -- I'll take a look now.

khieta commented 1 month ago

Fixed by #1220. This change will naturally be included in the next minor version (4.1.0). But let us know if you need it asap and we can release a patch version 4.0.1.