Skip to content

Commit 00d74ff

Browse files
benjaminpcopybara-github
authored andcommitted
Support decompressing zstd tar archives for repository rules.
This is mostly a rebase of bazelbuild#11968 with a few tweaks of my own. Fixes bazelbuild#10342. Co-authored-by: Grzegorz Lukasik <[email protected]> RELNOTES[new]: `repository_ctx.extract`, and thus `http_archive`, can now decompress zstandard-compressed archives. Closes bazelbuild#15026. PiperOrigin-RevId: 436146779
1 parent a421288 commit 00d74ff

File tree

8 files changed

+72
-6
lines changed

8 files changed

+72
-6
lines changed

src/main/java/com/google/devtools/build/lib/bazel/repository/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ java_library(
5454
"//third_party:java-diff-utils",
5555
"//third_party:jsr305",
5656
"//third_party:xz",
57+
"@zstd-jni",
5758
],
5859
)
5960

src/main/java/com/google/devtools/build/lib/bazel/repository/DecompressorValue.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,15 @@ static Decompressor getDecompressor(Path archivePath)
103103
return TarGzFunction.INSTANCE;
104104
} else if (baseName.endsWith(".tar.xz") || baseName.endsWith(".txz")) {
105105
return TarXzFunction.INSTANCE;
106+
} else if (baseName.endsWith(".tar.zst") || baseName.endsWith(".tzst")) {
107+
return TarZstFunction.INSTANCE;
106108
} else if (baseName.endsWith(".tar.bz2")) {
107109
return TarBz2Function.INSTANCE;
108110
} else {
109111
throw new RepositoryFunctionException(
110112
Starlark.errorf(
111-
"Expected a file with a .zip, .jar, .war, .aar, .tar, .tar.gz, .tgz, .tar.xz, .txz, "
112-
+ "or .tar.bz2 suffix (got %s)",
113+
"Expected a file with a .zip, .jar, .war, .aar, .tar, .tar.gz, .tgz, .tar.xz, .txz,"
114+
+ " .tar.zst, .tzst, or .tar.bz2 suffix (got %s)",
113115
archivePath),
114116
Transience.PERSISTENT);
115117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2022 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.devtools.build.lib.bazel.repository;
16+
17+
import com.github.luben.zstd.ZstdInputStreamNoFinalizer;
18+
import com.google.devtools.build.lib.bazel.repository.DecompressorValue.Decompressor;
19+
import java.io.BufferedInputStream;
20+
import java.io.FileInputStream;
21+
import java.io.IOException;
22+
import java.io.InputStream;
23+
24+
/** Creates a repository by unarchiving a zstandard-compressed .tar file. */
25+
final class TarZstFunction extends CompressedTarFunction {
26+
static final Decompressor INSTANCE = new TarZstFunction();
27+
private static final int BUFFER_SIZE = 32 * 1024;
28+
29+
private TarZstFunction() {}
30+
31+
@Override
32+
protected InputStream getDecompressorStream(DecompressorDescriptor descriptor)
33+
throws IOException {
34+
return new ZstdInputStreamNoFinalizer(
35+
new BufferedInputStream(
36+
new FileInputStream(descriptor.archivePath().getPathFile()), BUFFER_SIZE));
37+
}
38+
}

src/test/java/com/google/devtools/build/lib/bazel/repository/DecompressorValueTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public void testKnownFileExtensionsDoNotThrow() throws Exception {
5050
DecompressorDescriptor.builder().setArchivePath(path).build();
5151
path = fs.getPath("/foo/.external-repositories/some-repo/bar.baz.txz");
5252
DecompressorDescriptor.builder().setArchivePath(path).build();
53+
path = fs.getPath("/foo/.external-repositories/some-repo/bar.baz.tar.zst");
54+
DecompressorDescriptor.builder().setArchivePath(path).build();
55+
path = fs.getPath("/foo/.external-repositories/some-repo/bar.baz.tzst");
56+
DecompressorDescriptor.builder().setArchivePath(path).build();
5357
path = fs.getPath("/foo/.external-repositories/some-repo/bar.baz.tar.bz2");
5458
DecompressorDescriptor.builder().setArchivePath(path).build();
5559
}

src/test/shell/bazel/BUILD

+4-1
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,10 @@ sh_test(
657657
name = "external_integration_test",
658658
size = "large",
659659
srcs = ["external_integration_test.sh"],
660-
data = [":test-deps"],
660+
data = [
661+
":test-deps",
662+
"//src/test/shell/bazel/testdata:zstd_test_archive.tar.zst",
663+
],
661664
shard_count = 15,
662665
tags = [
663666
"no_windows",

src/test/shell/bazel/external_integration_test.sh

+16
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ function test_http_archive_tar_xz() {
205205
http_archive_helper tar_xz_up
206206
}
207207

208+
function test_http_archive_tar_zstd() {
209+
cat >> $(create_workspace_with_default_repos WORKSPACE) <<EOF
210+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
211+
http_archive(
212+
name = 'test_zstd_repo',
213+
url = 'file://$(rlocation io_bazel/src/test/shell/bazel/testdata/zstd_test_archive.tar.zst)',
214+
sha256 = '12b0116f2a3c804859438e102a8a1d5f494c108d1b026da9f6ca55fb5107c7e9',
215+
build_file_content = 'filegroup(name="x", srcs=glob(["*"]))',
216+
)
217+
EOF
218+
bazel build @test_zstd_repo//...
219+
220+
base_external_path=bazel-out/../external/test_zstd_repo
221+
assert_contains "test content" "${base_external_path}/test_dir/test_file"
222+
}
223+
208224
function test_http_archive_no_server() {
209225
cat >> $(create_workspace_with_default_repos WORKSPACE) <<EOF
210226
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

src/test/shell/bazel/testdata/BUILD

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
load("//:distdir_deps.bzl", "gen_workspace_stanza")
22

3+
package(default_testonly = True)
4+
35
filegroup(
46
name = "srcs",
7+
testonly = False,
58
srcs = glob(["**"]),
69
visibility = ["//src/test/shell/bazel:__pkg__"],
710
)
811

912
filegroup(
1013
name = "git-repos",
11-
testonly = 1,
1214
srcs = [
1315
"outer-planets-repo.tar.gz",
1416
"pluto-repo.tar.gz",
@@ -20,7 +22,6 @@ filegroup(
2022

2123
filegroup(
2224
name = "embedded_tools_deps_test_data",
23-
testonly = 1,
2425
srcs = [
2526
"//src/test/shell/bazel/testdata:embedded_tools_srcs_deps",
2627
],
@@ -29,7 +30,6 @@ filegroup(
2930

3031
filegroup(
3132
name = "jdk_http_archives_filegroup",
32-
testonly = 1,
3333
srcs = [
3434
"//src/test/shell/bazel/testdata:jdk_http_archives",
3535
],
@@ -64,3 +64,5 @@ gen_workspace_stanza(
6464
template = "jdk_http_archives.tmpl",
6565
visibility = ["//:__pkg__"],
6666
)
67+
68+
exports_files(["zstd_test_archive.tar.zst"])
Binary file not shown.

0 commit comments

Comments
 (0)