Skip to content

Commit

Permalink
Ensure that lockfiles end in newlines. (#1774)
Browse files Browse the repository at this point in the history
I encountered a repo whose precommit checks choked on our lockfiles because they didn't end in newlines.
Since this is a reasonable expectation from a text file (indeed POSIX requires it, so various CLI tools assume it),
we oblige.
  • Loading branch information
benjyw committed May 25, 2022
1 parent ddd3bd8 commit 6cb2826
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions pex/cli/commands/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from __future__ import absolute_import, print_function

import functools
import sys
from argparse import ArgumentParser, _ActionsContainer
from collections import OrderedDict, defaultdict
Expand Down Expand Up @@ -288,17 +287,24 @@ def _dump_lockfile(
):
# type: (...) -> None
path_mappings = resolver_options.get_path_mappings(self.options)
dump = functools.partial(
self.dump_json,
self.options,
json_codec.as_json_data(lockfile=lock_file, path_mappings=path_mappings),
sort_keys=True,
)

def dump_with_terminating_newline(out):
# json.dump() does not write the newline terminating the last line, but some
# JSON linters, and line-based tools in general, expect it, and since these
# files are intended to be checked in to repos that may enforce this, we oblige.
self.dump_json(
self.options,
json_codec.as_json_data(lockfile=lock_file, path_mappings=path_mappings),
out=out,
sort_keys=True,
)
out.write("\n")

if output:
dump(out=output)
dump_with_terminating_newline(out=output)
else:
with self.output(self.options) as output:
dump(out=output)
dump_with_terminating_newline(out=output)

def _export(self):
# type: () -> Result
Expand Down

0 comments on commit 6cb2826

Please sign in to comment.