Skip to content

Commit

Permalink
Make sure Bazel patches files with line ending '\n' on all platforms
Browse files Browse the repository at this point in the history
This is to ensure all Bazel patched files have the exact same content and hash value. For example, for patched bzl files, their hash could end up in Bzlmod lockfile. Without this change, the lockfile will be different on Windows due to line ending being `\r\n`.

RELNOTES:
PiperOrigin-RevId: 564504118
Change-Id: Ia61493dca0c034341279d0d5e613354d4c6b9e19
  • Loading branch information
meteorcloudy committed Sep 12, 2023
1 parent 585d7bf commit ece4913
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private static ImmutableList<String> readFile(Path file) throws IOException {
}

private static void writeFile(Path file, List<String> content) throws IOException {
FileSystemUtils.writeLinesAs(file, UTF_8, content);
FileSystemUtils.writeLinesAs(file, UTF_8, content, "\n");
}

private static boolean getReadPermission(int permission) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,17 +708,17 @@ public static void writeContent(Path outputFile, byte[] content) throws IOExcept
}

/**
* Writes lines to file using the given encoding, ending every line with a line break '\n'
* character.
* Writes lines to file using the given encoding, ending every line with a system specific line
* break character.
*/
@ThreadSafe // but not atomic
public static void writeLinesAs(Path file, Charset charset, String... lines) throws IOException {
writeLinesAs(file, charset, Arrays.asList(lines));
}

/**
* Writes lines to file using the given encoding, ending every line with a line break '\n'
* character.
* Writes lines to file using the given encoding, ending every line with a system specific line
* break character.
*/
@ThreadSafe // but not atomic
public static void writeLinesAs(Path file, Charset charset, Iterable<String> lines)
Expand All @@ -728,17 +728,28 @@ public static void writeLinesAs(Path file, Charset charset, Iterable<String> lin
}

/**
* Appends lines to file using the given encoding, ending every line with a line break '\n'
* Writes lines to file using the given encoding, ending every line with a given line break
* character.
*/
@ThreadSafe // but not atomic
public static void writeLinesAs(
Path file, Charset charset, Iterable<String> lines, String lineBreak) throws IOException {
file.getParentDirectory().createDirectoryAndParents();
asByteSink(file).asCharSink(charset).writeLines(lines, lineBreak);
}

/**
* Appends lines to file using the given encoding, ending every line with a system specific line
* break character.
*/
@ThreadSafe // but not atomic
public static void appendLinesAs(Path file, Charset charset, String... lines) throws IOException {
appendLinesAs(file, charset, Arrays.asList(lines));
}

/**
* Appends lines to file using the given encoding, ending every line with a line break '\n'
* character.
* Appends lines to file using the given encoding, ending every line with a system specific line
* break character.
*/
@ThreadSafe // but not atomic
public static void appendLinesAs(Path file, Charset charset, Iterable<String> lines)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public void testPatch() throws Exception {
context.createFile(
context.path("my.patch"), "--- foo\n+++ foo\n" + ONE_LINE_PATCH, false, true, thread);
context.patch(patchFile, StarlarkInt.of(0), thread);
testOutputFile(foo.getPath(), String.format("line one%nline two%n"));
testOutputFile(foo.getPath(), "line one\nline two\n");
}

@Test
Expand Down

0 comments on commit ece4913

Please sign in to comment.