Skip to content
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

Make Bazel itself build under an output base with Unicode characters #24457

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bazel_dep(name = "zstd-jni", version = "1.5.2-3.bcr.1")
bazel_dep(name = "blake3", version = "1.5.1.bcr.1")
bazel_dep(name = "zlib", version = "1.3.1.bcr.3")
bazel_dep(name = "rules_cc", version = "0.0.17")
bazel_dep(name = "rules_java", version = "8.8.0")
bazel_dep(name = "rules_java", version = "8.9.0")
bazel_dep(name = "rules_graalvm", version = "0.11.1")
bazel_dep(name = "rules_proto", version = "7.0.2")
bazel_dep(name = "rules_jvm_external", version = "6.5")
Expand Down
4 changes: 2 additions & 2 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/MODULE.tools
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use_repo(buildozer_binary, "buildozer_binary")
# Dependencies used to auto-load removed symbols and rules from Bazel (due to Starlarkification)
# See also: --incompatible_autoload_externally, AutoloadSymbols
bazel_dep(name = "protobuf", version = "29.0", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_java", version = "8.8.0")
bazel_dep(name = "rules_java", version = "8.9.0")
bazel_dep(name = "rules_cc", version = "0.0.17")
bazel_dep(name = "rules_python", version = "0.40.0")
bazel_dep(name = "rules_shell", version = "0.2.0")
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis:frontier_serializer",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis:frontier_violation_checker",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis:options",
"//src/main/java/com/google/devtools/build/lib/unsafe:string",
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/build/lib/util:TestType",
"//src/main/java/com/google/devtools/build/lib/util:abrupt_exit_exception",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.google.devtools.build.lib.bazel.repository.DecompressorValue.Decompressor;
import com.google.devtools.build.lib.vfs.Path;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -37,12 +36,6 @@ public class ArFunction implements Decompressor {
// This is the same value as picked for .tar files, which appears to have worked well.
private static final int BUFFER_SIZE = 32 * 1024;

private InputStream getDecompressorStream(DecompressorDescriptor descriptor) throws IOException {
return new BufferedInputStream(
new FileInputStream(descriptor.archivePath().getPathFile()), BUFFER_SIZE);
}
;

@Override
public Path decompress(DecompressorDescriptor descriptor)
throws InterruptedException, IOException {
Expand All @@ -52,7 +45,8 @@ public Path decompress(DecompressorDescriptor descriptor)

Map<String, String> renameFiles = descriptor.renameFiles();

try (InputStream decompressorStream = getDecompressorStream(descriptor)) {
try (InputStream decompressorStream =
new BufferedInputStream(descriptor.archivePath().getInputStream(), BUFFER_SIZE)) {
ArArchiveInputStream arStream = new ArArchiveInputStream(decompressorStream);
ArArchiveEntry entry;
while ((entry = arStream.getNextArEntry()) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -56,7 +57,9 @@
* representation of raw bytes stored as latin-1 strings.
*/
public abstract class CompressedTarFunction implements Decompressor {
protected abstract InputStream getDecompressorStream(DecompressorDescriptor descriptor)
private static final int BUFFER_SIZE = 32 * 1024;

protected abstract InputStream getDecompressorStream(BufferedInputStream compressedInputStream)
throws IOException;

@Override
Expand All @@ -72,7 +75,9 @@ public Path decompress(DecompressorDescriptor descriptor)
// Store link, target info of symlinks, we create them after regular files are extracted.
Map<Path, PathFragment> symlinks = new HashMap<>();

try (InputStream decompressorStream = getDecompressorStream(descriptor)) {
try (InputStream compressedInputStream = descriptor.archivePath().getInputStream();
InputStream decompressorStream =
getDecompressorStream(new BufferedInputStream(compressedInputStream, BUFFER_SIZE))) {
// USTAR tar headers use an unspecified encoding whereas PAX tar headers always use UTF-8.
// We can specify the encoding to use for USTAR headers, but the Charset used for PAX headers
// is fixed to UTF-8. We thus specify a custom Charset for the former so that we can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import com.google.devtools.build.lib.bazel.repository.DecompressorValue.Decompressor;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
Expand All @@ -26,17 +25,12 @@
*/
public class TarBz2Function extends CompressedTarFunction {
public static final Decompressor INSTANCE = new TarBz2Function();
private static final int BUFFER_SIZE = 32 * 1024;

private TarBz2Function() {
}
private TarBz2Function() {}

@Override
protected InputStream getDecompressorStream(DecompressorDescriptor descriptor)
protected InputStream getDecompressorStream(BufferedInputStream compressedInputStream)
throws IOException {
return new BZip2CompressorInputStream(
new BufferedInputStream(
new FileInputStream(descriptor.archivePath().getPathFile()), BUFFER_SIZE),
true);
return new BZip2CompressorInputStream(compressedInputStream, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,16 @@

import com.google.devtools.build.lib.bazel.repository.DecompressorValue.Decompressor;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/** Creates a repository by unarchiving a plain .tar file. */
public class TarFunction extends CompressedTarFunction {
public static final Decompressor INSTANCE = new TarFunction();
private static final int BUFFER_SIZE = 32 * 1024;

private TarFunction() {}

@Override
protected InputStream getDecompressorStream(DecompressorDescriptor descriptor)
throws IOException {
return new BufferedInputStream(
new FileInputStream(descriptor.archivePath().getPathFile()), BUFFER_SIZE);
protected InputStream getDecompressorStream(BufferedInputStream compressedInputStream) {
return compressedInputStream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import com.google.devtools.build.lib.bazel.repository.DecompressorValue.Decompressor;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
Expand All @@ -26,17 +25,12 @@
*/
public class TarGzFunction extends CompressedTarFunction {
public static final Decompressor INSTANCE = new TarGzFunction();
private static final int BUFFER_SIZE = 32 * 1024;

private TarGzFunction() {
}
private TarGzFunction() {}

@Override
protected InputStream getDecompressorStream(DecompressorDescriptor descriptor)
protected InputStream getDecompressorStream(BufferedInputStream compressedInputStream)
throws IOException {
return new GzipCompressorInputStream(
new BufferedInputStream(
new FileInputStream(descriptor.archivePath().getPathFile()), BUFFER_SIZE),
true);
return new GzipCompressorInputStream(compressedInputStream, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import com.google.devtools.build.lib.bazel.repository.DecompressorValue.Decompressor;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.tukaani.xz.XZInputStream;
Expand All @@ -26,16 +25,12 @@
*/
class TarXzFunction extends CompressedTarFunction {
public static final Decompressor INSTANCE = new TarXzFunction();
private static final int BUFFER_SIZE = 32 * 1024;

private TarXzFunction() {
}
private TarXzFunction() {}

@Override
protected InputStream getDecompressorStream(DecompressorDescriptor descriptor)
protected InputStream getDecompressorStream(BufferedInputStream compressedInputStream)
throws IOException {
return new XZInputStream(
new BufferedInputStream(
new FileInputStream(descriptor.archivePath().getPathFile()), BUFFER_SIZE));
return new XZInputStream(compressedInputStream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@
import com.github.luben.zstd.ZstdInputStreamNoFinalizer;
import com.google.devtools.build.lib.bazel.repository.DecompressorValue.Decompressor;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/** Creates a repository by unarchiving a zstandard-compressed .tar file. */
final class TarZstFunction extends CompressedTarFunction {
static final Decompressor INSTANCE = new TarZstFunction();
private static final int BUFFER_SIZE = 32 * 1024;

private TarZstFunction() {}

@Override
protected InputStream getDecompressorStream(DecompressorDescriptor descriptor)
protected InputStream getDecompressorStream(BufferedInputStream compressedInputStream)
throws IOException {
return new ZstdInputStreamNoFinalizer(
new BufferedInputStream(
new FileInputStream(descriptor.archivePath().getPathFile()), BUFFER_SIZE));
return new ZstdInputStreamNoFinalizer(compressedInputStream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/unsafe:string",
"//src/main/java/com/google/devtools/build/lib/util",
"//src/main/java/com/google/devtools/build/lib/util:string",
"//src/main/java/com/google/devtools/build/lib/util:string_encoding",
"//src/main/java/com/google/devtools/build/lib/util/io:out-err",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
Expand Down Expand Up @@ -1051,10 +1049,7 @@ public StructImpl downloadAndExtract(
Path downloadDirectory;
try {
// Download to temp directory inside the outputDirectory and delete it after extraction
java.nio.file.Path tempDirectory =
Files.createTempDirectory(Paths.get(outputPath.toString()), "temp");
downloadDirectory =
workingDirectory.getFileSystem().getPath(tempDirectory.toFile().getAbsolutePath());
downloadDirectory = outputPath.getPath().createTempDirectory("temp");

Phaser downloadPhaser = new Phaser();
Future<Path> pendingDownload =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
import java.io.File;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.nio.file.Files;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
Expand Down Expand Up @@ -181,11 +180,7 @@ public boolean handlesCaching() {
}

protected Path createActionTemp(Path execRoot) throws IOException {
return execRoot.getRelative(
Files.createTempDirectory(
java.nio.file.Paths.get(execRoot.getPathString()), "local-spawn-runner.")
.getFileName()
.toString());
return execRoot.createTempDirectory("local-spawn-runner.");
}

private final class SubprocessHandler {
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/com/google/devtools/build/lib/runtime/InfoItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@

package com.google.devtools.build.lib.runtime;


import com.google.common.base.Supplier;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.unsafe.StringUnsafe;
import com.google.devtools.build.lib.util.AbruptExitException;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/** An item that is returned by <code>blaze info</code>. */
public abstract class InfoItem {
Expand Down Expand Up @@ -77,11 +75,6 @@ protected static byte[] print(Object value) {
if (value instanceof byte[]) {
return (byte[]) value;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintWriter writer =
new PrintWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
writer.print(value + "\n");
writer.flush();
return outputStream.toByteArray();
return StringUnsafe.getInstance().getInternalStringBytes(String.valueOf(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.google.devtools.build.lib.runtime.commands;

import static com.google.devtools.build.lib.runtime.Command.BuildPhase.NONE;
import static java.nio.charset.StandardCharsets.ISO_8859_1;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/pkgcache",
"//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster",
"//src/main/java/com/google/devtools/build/lib/skyframe:starlark_builtins_value",
"//src/main/java/com/google/devtools/build/lib/unsafe:string",
"//src/main/java/com/google/devtools/build/lib/util:abrupt_exit_exception",
"//src/main/java/com/google/devtools/build/lib/util:debug-logger-configurator",
"//src/main/java/com/google/devtools/build/lib/util:detailed_exit_code",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
// limitations under the License.
package com.google.devtools.build.lib.runtime.commands.info;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.devtools.build.lib.runtime.InfoItem;
import com.google.devtools.build.lib.unsafe.StringUnsafe;
import com.google.devtools.build.lib.util.io.OutErr;
import java.io.IOException;

Expand All @@ -30,9 +29,12 @@ class StdoutInfoItemHandler implements InfoItemHandler {
@Override
public void addInfoItem(String key, byte[] value, boolean printKey) throws IOException {
if (printKey) {
outErr.getOutputStream().write((key + ": ").getBytes(UTF_8));
outErr.getOutputStream().write(StringUnsafe.getInstance().getInternalStringBytes(key));
outErr.getOutputStream().write(':');
outErr.getOutputStream().write(' ');
}
outErr.getOutputStream().write(value);
outErr.getOutputStream().write('\n');
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ protected SandboxedSpawn prepareSpawn(Spawn spawn, SpawnExecutionContext context
}

private static Path createActionTemp(Path execRoot) throws IOException {
return execRoot.getRelative(
java.nio.file.Files.createTempDirectory(
java.nio.file.Paths.get(execRoot.getPathString()), "windows-sandbox.")
.getFileName()
.toString());
return execRoot.createTempDirectory("windows-sandbox.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.jni.JniLoader;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.util.StringEncoding;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -110,7 +111,7 @@ public ImmutableList<String> getArgv() {
public SubprocessBuilder setArgv(ImmutableList<String> argv) {
this.argv = Preconditions.checkNotNull(argv);
Preconditions.checkArgument(!this.argv.isEmpty());
File argv0 = new File(this.argv.get(0));
File argv0 = new File(StringEncoding.internalToPlatform(this.argv.get(0)));
Preconditions.checkArgument(
argv0.isAbsolute() || argv0.getParent() == null,
"argv[0] = '%s'; it should be either absolute or just a single file name"
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/util/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ java_library(
deps = [
":os",
":single_line_formatter",
":string_encoding",
":util",
"//third_party:error_prone_annotations",
"//third_party:guava",
Expand Down
Loading