Skip to content

Improve whitespace in ExitCodeException Show instance #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

9999years
Copy link
Contributor

Split off of #83.

Before, ProcessConfig's Show output would include a trailing newline. This has been fixed, so that derived Show output does not include newlines in weird places.

Before:

ghci> data Foo = Foo { a :: Int, b :: ProcessConfig () () (), c :: String } deriving Show
ghci> Foo 1 (proc "echo" ["puppy"]) "doggy"
Foo {a = 1, b = Raw command: echo puppy
, c = "doggy"}

After

ghci> Foo 1 (proc "echo" ["puppy"]) "doggy"
Foo {a = 1, b = Raw command: echo puppy, c = "doggy"}

Whitespace for the ExitCodeException Show instance has also been adjusted, to place the output closer to the relevant headers.

Before:

ghci> readProcess_ $ proc "sh" ["-c", "echo this is stdout; echo this is stderr >&2; false"]
*** Exception: Received ExitFailure 1 when running
Raw command: sh -c "echo this is stdout; echo this is stderr >&2; false"
Standard output:

this is stdout
Standard error:

this is stderr

After:

*** Exception: Received ExitFailure 1 when running
Raw command: sh -c "echo this is stdout; echo this is stderr >&2; false"

Standard output:
this is stdout

Standard error:
this is stderr

Note that because trailing whitespace is not accounted for, it is still possible to get unintuitive results depending on what exactly the subprocess prints:

ghci> readProcess_ $ proc "sh" ["-c", "echo -n this is stdout; echo -n this is stderr >&2; false"]
*** Exception: Received ExitFailure 1 when running
Raw command: sh -c "echo -n this is stdout; echo -n this is stderr >&2; false"

Standard output:
this is stdout
Standard error:
this is stderr

Split off of fpco#83.

Before, `ProcessConfig`'s `Show` output would include a trailing
newline. This has been fixed, so that derived `Show` output does not
include newlines in weird places.

Before:

    ghci> data Foo = Foo { a :: Int, b :: ProcessConfig () () (), c :: String } deriving Show
    ghci> Foo 1 (proc "echo" ["puppy"]) "doggy"
    Foo {a = 1, b = Raw command: echo puppy
    , c = "doggy"}

After

    ghci> Foo 1 (proc "echo" ["puppy"]) "doggy"
    Foo {a = 1, b = Raw command: echo puppy, c = "doggy"}

Whitespace for the `ExitCodeException` `Show` instance has also been
adjusted, to place the output closer to the relevant headers.

Before:

    ghci> readProcess_ $ proc "sh" ["-c", "echo this is stdout; echo this is stderr >&2; false"]
    *** Exception: Received ExitFailure 1 when running
    Raw command: sh -c "echo this is stdout; echo this is stderr >&2; false"
    Standard output:

    this is stdout
    Standard error:

    this is stderr

After:

    *** Exception: Received ExitFailure 1 when running
    Raw command: sh -c "echo this is stdout; echo this is stderr >&2; false"

    Standard output:
    this is stdout

    Standard error:
    this is stderr

Note that because trailing whitespace is not accounted for, it is still
possible to get unintuitive results depending on what exactly the
subprocess prints:

    ghci> readProcess_ $ proc "sh" ["-c", "echo -n this is stdout; echo -n this is stderr >&2; false"]
    *** Exception: Received ExitFailure 1 when running
    Raw command: sh -c "echo -n this is stdout; echo -n this is stderr >&2; false"

    Standard output:
    this is stdout
    Standard error:
    this is stderr
@9999years 9999years force-pushed the exitcodeexception-whitespace branch from 3ce8c32 to 685a67a Compare April 8, 2025 19:26
9999years added a commit to 9999years/typed-process that referenced this pull request Apr 8, 2025
Needs fpco#88.

Previously, output was assumed to end with a newline, leading to poor
`ExitCodeException` rendering (see the "Standard error" header below):

    ghci> readProcess_ $ proc "sh" ["-c", "nix path-info --json nixpkgs#agda && false"]
    *** Exception: Received ExitFailure 1 when running
    Raw command: sh -c "nix path-info --json nixpkgs#agda && false"
    Standard output:

    [{"path":"/nix/store/sj2z0h5ywlflqv50dfphwia6p0ij0mlj-agdaWithPackages-2.6.4.3","valid":false}]Standard error:

    these 5 paths will be fetched (18.30 MiB download, 133.19 MiB unpacked):
      /nix/store/5q0kb0nqnqcfs7a0ncsjq4fdppwirpxa-Agda-2.6.4.3-bin
        /nix/store/xmximjjnkn0hm4gw7akc9f20ydz6msmk-Agda-2.6.4.3-data
          /nix/store/sj2z0h5ywlflqv50dfphwia6p0ij0mlj-agdaWithPackages-2.6.4.3
            /nix/store/b49sa2q0yb3fd14ppzh6j6rm8vvgr9n6-ghc-9.6.6-with-packages
              /nix/store/vharimf7f2glj4fyhiglzws0qyv4xrry-libraries

Now, trailing newlines are removed and the correct number of newlines
are inserted in order to make the `Show` instance display legibly:

    ghci> readProcess_ $ proc "sh" ["-c", "nix path-info --json nixpkgs#agda && false"]
    *** Exception: Received ExitFailure 1 when running
    Raw command: sh -c "nix path-info --json nixpkgs#agda && false"

    Standard output:
    [{"path":"/nix/store/sj2z0h5ywlflqv50dfphwia6p0ij0mlj-agdaWithPackages-2.6.4.3","valid":false}]

    Standard error:
    these 5 paths will be fetched (18.30 MiB download, 133.19 MiB unpacked):
      /nix/store/5q0kb0nqnqcfs7a0ncsjq4fdppwirpxa-Agda-2.6.4.3-bin
        /nix/store/xmximjjnkn0hm4gw7akc9f20ydz6msmk-Agda-2.6.4.3-data
          /nix/store/sj2z0h5ywlflqv50dfphwia6p0ij0mlj-agdaWithPackages-2.6.4.3
            /nix/store/b49sa2q0yb3fd14ppzh6j6rm8vvgr9n6-ghc-9.6.6-with-packages
              /nix/store/vharimf7f2glj4fyhiglzws0qyv4xrry-libraries

Also, derived `Show` instances will behave correctly now. Previously,
the `Show` instance would often end with a newline, leading to clumsy
output:

    ghci> e stdout stderr = ExitCodeException { ... }
    ghci> data Foo = Foo { a :: Int, b :: ExitCodeException, c :: String } deriving Show
    ghci> Foo 1 (e "<STDOUT>\n" "") "hello"
    Foo {a = 1, b = Received ExitFailure 1 when running
    Raw command: echo

    Standard output:
    <STDOUT>
    , c = "hello"}

Now:

    ghci> Foo 1 (e "<STDOUT>\n" "") "hello"
    Foo {a = 1, b = Received ExitFailure 1 when running
    Raw command: echo

    Standard output:
    <STDOUT>, c = "hello"}
9999years added a commit to 9999years/typed-process that referenced this pull request Apr 8, 2025
Needs fpco#88.

Previously, output was assumed to end with a newline, leading to poor
`ExitCodeException` rendering (see the "Standard error" header below):

    ghci> readProcess_ $ proc "sh" ["-c", "nix path-info --json nixpkgs#agda && false"]
    *** Exception: Received ExitFailure 1 when running
    Raw command: sh -c "nix path-info --json nixpkgs#agda && false"
    Standard output:

    [{"path":"/nix/store/sj2z0h5ywlflqv50dfphwia6p0ij0mlj-agdaWithPackages-2.6.4.3","valid":false}]Standard error:

    these 5 paths will be fetched (18.30 MiB download, 133.19 MiB unpacked):
      /nix/store/5q0kb0nqnqcfs7a0ncsjq4fdppwirpxa-Agda-2.6.4.3-bin
        /nix/store/xmximjjnkn0hm4gw7akc9f20ydz6msmk-Agda-2.6.4.3-data
          /nix/store/sj2z0h5ywlflqv50dfphwia6p0ij0mlj-agdaWithPackages-2.6.4.3
            /nix/store/b49sa2q0yb3fd14ppzh6j6rm8vvgr9n6-ghc-9.6.6-with-packages
              /nix/store/vharimf7f2glj4fyhiglzws0qyv4xrry-libraries

Now, trailing newlines are removed and the correct number of newlines
are inserted in order to make the `Show` instance display legibly:

    ghci> readProcess_ $ proc "sh" ["-c", "nix path-info --json nixpkgs#agda && false"]
    *** Exception: Received ExitFailure 1 when running
    Raw command: sh -c "nix path-info --json nixpkgs#agda && false"

    Standard output:
    [{"path":"/nix/store/sj2z0h5ywlflqv50dfphwia6p0ij0mlj-agdaWithPackages-2.6.4.3","valid":false}]

    Standard error:
    these 5 paths will be fetched (18.30 MiB download, 133.19 MiB unpacked):
      /nix/store/5q0kb0nqnqcfs7a0ncsjq4fdppwirpxa-Agda-2.6.4.3-bin
        /nix/store/xmximjjnkn0hm4gw7akc9f20ydz6msmk-Agda-2.6.4.3-data
          /nix/store/sj2z0h5ywlflqv50dfphwia6p0ij0mlj-agdaWithPackages-2.6.4.3
            /nix/store/b49sa2q0yb3fd14ppzh6j6rm8vvgr9n6-ghc-9.6.6-with-packages
              /nix/store/vharimf7f2glj4fyhiglzws0qyv4xrry-libraries

Also, derived `Show` instances will behave correctly now. Previously,
the `Show` instance would often end with a newline, leading to clumsy
output:

    ghci> e stdout stderr = ExitCodeException { ... }
    ghci> data Foo = Foo { a :: Int, b :: ExitCodeException, c :: String } deriving Show
    ghci> Foo 1 (e "<STDOUT>\n" "") "hello"
    Foo {a = 1, b = Received ExitFailure 1 when running
    Raw command: echo

    Standard output:
    <STDOUT>
    , c = "hello"}

Now:

    ghci> Foo 1 (e "<STDOUT>\n" "") "hello"
    Foo {a = 1, b = Received ExitFailure 1 when running
    Raw command: echo

    Standard output:
    <STDOUT>, c = "hello"}
@tomjaguarpaw
Copy link
Collaborator

Thanks for this.

Before tackling the issue of whitespace I would like to resolve the issue of (not) UTF-8 encoding (#86), since I expect that's causing more practical problems. I have put at an attempted resolution at #87. Could you confirm that you are content with that resolution? If so I can merge it and we can proceed to discuss whitespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants