|
| 1 | +// Copyright 2024 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.bzlmod; |
| 16 | + |
| 17 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import com.google.devtools.build.lib.actions.FileValue; |
| 21 | +import com.google.devtools.build.lib.cmdline.LabelConstants; |
| 22 | +import com.google.devtools.build.lib.events.Event; |
| 23 | +import com.google.devtools.build.lib.packages.BazelStarlarkEnvironment; |
| 24 | +import com.google.devtools.build.lib.packages.DotBazelFileSyntaxChecker; |
| 25 | +import com.google.devtools.build.lib.packages.VendorThreadContext; |
| 26 | +import com.google.devtools.build.lib.skyframe.PrecomputedValue; |
| 27 | +import com.google.devtools.build.lib.skyframe.PrecomputedValue.Precomputed; |
| 28 | +import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 29 | +import com.google.devtools.build.lib.vfs.Path; |
| 30 | +import com.google.devtools.build.lib.vfs.Root; |
| 31 | +import com.google.devtools.build.lib.vfs.RootedPath; |
| 32 | +import com.google.devtools.build.skyframe.SkyFunction; |
| 33 | +import com.google.devtools.build.skyframe.SkyFunctionException; |
| 34 | +import com.google.devtools.build.skyframe.SkyFunctionException.Transience; |
| 35 | +import com.google.devtools.build.skyframe.SkyKey; |
| 36 | +import com.google.devtools.build.skyframe.SkyValue; |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.Optional; |
| 39 | +import javax.annotation.Nullable; |
| 40 | +import net.starlark.java.eval.EvalException; |
| 41 | +import net.starlark.java.eval.Mutability; |
| 42 | +import net.starlark.java.eval.Starlark; |
| 43 | +import net.starlark.java.eval.StarlarkSemantics; |
| 44 | +import net.starlark.java.eval.StarlarkThread; |
| 45 | +import net.starlark.java.eval.SymbolGenerator; |
| 46 | +import net.starlark.java.syntax.ParserInput; |
| 47 | +import net.starlark.java.syntax.Program; |
| 48 | +import net.starlark.java.syntax.StarlarkFile; |
| 49 | +import net.starlark.java.syntax.SyntaxError; |
| 50 | + |
| 51 | +/** |
| 52 | + * The function to evaluate the VENDOR.bazel file under the vendor directory specified by the flag: |
| 53 | + * --vendor_dir. |
| 54 | + */ |
| 55 | +public class VendorFileFunction implements SkyFunction { |
| 56 | + |
| 57 | + public static final Precomputed<Optional<Path>> VENDOR_DIRECTORY = |
| 58 | + new Precomputed<>("vendor_directory"); |
| 59 | + |
| 60 | + private static final String VENDOR_FILE_HEADER = |
| 61 | + """ |
| 62 | +############################################################################### |
| 63 | +# This file is used to configure how external repositories are handled in vendor mode. |
| 64 | +# ONLY the two following functions can be used: |
| 65 | +# |
| 66 | +# ignore('@@<canonical repo name>', ...) is used to completely ignore this repo from vendoring. |
| 67 | +# Bazel will use the normal external cache and fetch process for this repo. |
| 68 | +# |
| 69 | +# pin('@@<canonical repo name>', ...) is used to pin the contents of this repo under the vendor |
| 70 | +# directory as if there is a --override_repository flag for this repo. |
| 71 | +# Note that Bazel will NOT update the vendored source for this repo while running vendor command |
| 72 | +# unless it's unpinned. The user can modify and maintain the vendored source for this repo manually. |
| 73 | +############################################################################### |
| 74 | +"""; |
| 75 | + |
| 76 | + private final BazelStarlarkEnvironment starlarkEnv; |
| 77 | + |
| 78 | + public VendorFileFunction(BazelStarlarkEnvironment starlarkEnv) { |
| 79 | + this.starlarkEnv = starlarkEnv; |
| 80 | + } |
| 81 | + |
| 82 | + @Nullable |
| 83 | + @Override |
| 84 | + public SkyValue compute(SkyKey skyKey, Environment env) |
| 85 | + throws SkyFunctionException, InterruptedException { |
| 86 | + if (VENDOR_DIRECTORY.get(env).isEmpty()) { |
| 87 | + throw new VendorFileFunctionException( |
| 88 | + new IllegalStateException( |
| 89 | + "VENDOR.bazel file is not accessible with vendor mode off (without --vendor_dir" |
| 90 | + + " flag)"), |
| 91 | + Transience.PERSISTENT); |
| 92 | + } |
| 93 | + |
| 94 | + Path vendorPath = VENDOR_DIRECTORY.get(env).get(); |
| 95 | + RootedPath vendorFilePath = |
| 96 | + RootedPath.toRootedPath(Root.fromPath(vendorPath), LabelConstants.VENDOR_FILE_NAME); |
| 97 | + |
| 98 | + FileValue vendorFileValue = (FileValue) env.getValue(FileValue.key(vendorFilePath)); |
| 99 | + if (vendorFileValue == null) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + if (!vendorFileValue.exists()) { |
| 103 | + createVendorFile(vendorPath, vendorFilePath.asPath()); |
| 104 | + return VendorFileValue.create(ImmutableList.of(), ImmutableList.of()); |
| 105 | + } |
| 106 | + |
| 107 | + StarlarkSemantics starlarkSemantics = PrecomputedValue.STARLARK_SEMANTICS.get(env); |
| 108 | + if (starlarkSemantics == null) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + VendorThreadContext context = |
| 112 | + getVendorFileContext(env, skyKey, vendorFilePath.asPath(), starlarkSemantics); |
| 113 | + return VendorFileValue.create(context.getIgnoredRepos(), context.getPinnedRepos()); |
| 114 | + } |
| 115 | + |
| 116 | + private VendorThreadContext getVendorFileContext( |
| 117 | + Environment env, SkyKey skyKey, Path vendorFilePath, StarlarkSemantics starlarkSemantics) |
| 118 | + throws VendorFileFunctionException, InterruptedException { |
| 119 | + try (Mutability mu = Mutability.create("vendor file")) { |
| 120 | + StarlarkFile vendorFile = readAndParseVendorFile(vendorFilePath, env); |
| 121 | + new DotBazelFileSyntaxChecker("VENDOR.bazel files", /* canLoadBzl= */ false) |
| 122 | + .check(vendorFile); |
| 123 | + net.starlark.java.eval.Module predeclaredEnv = |
| 124 | + net.starlark.java.eval.Module.withPredeclared( |
| 125 | + starlarkSemantics, starlarkEnv.getStarlarkGlobals().getVendorToplevels()); |
| 126 | + Program program = Program.compileFile(vendorFile, predeclaredEnv); |
| 127 | + StarlarkThread thread = |
| 128 | + StarlarkThread.create( |
| 129 | + mu, starlarkSemantics, /* contextDescription= */ "", SymbolGenerator.create(skyKey)); |
| 130 | + VendorThreadContext context = new VendorThreadContext(); |
| 131 | + context.storeInThread(thread); |
| 132 | + Starlark.execFileProgram(program, predeclaredEnv, thread); |
| 133 | + return context; |
| 134 | + } catch (SyntaxError.Exception | EvalException e) { |
| 135 | + throw new VendorFileFunctionException( |
| 136 | + new BadVendorFileException("error parsing VENDOR.bazel file: " + e.getMessage()), |
| 137 | + Transience.PERSISTENT); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private void createVendorFile(Path vendorPath, Path vendorFilePath) |
| 142 | + throws VendorFileFunctionException { |
| 143 | + try { |
| 144 | + vendorPath.createDirectoryAndParents(); |
| 145 | + byte[] vendorFileContents = VENDOR_FILE_HEADER.getBytes(UTF_8); |
| 146 | + FileSystemUtils.writeContent(vendorFilePath, vendorFileContents); |
| 147 | + } catch (IOException e) { |
| 148 | + throw new VendorFileFunctionException( |
| 149 | + new IOException("error creating VENDOR.bazel file", e), Transience.TRANSIENT); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private static StarlarkFile readAndParseVendorFile(Path path, Environment env) |
| 154 | + throws VendorFileFunctionException { |
| 155 | + byte[] contents; |
| 156 | + try { |
| 157 | + contents = FileSystemUtils.readWithKnownFileSize(path, path.getFileSize()); |
| 158 | + } catch (IOException e) { |
| 159 | + throw new VendorFileFunctionException( |
| 160 | + new IOException("error reading VENDOR.bazel file", e), Transience.TRANSIENT); |
| 161 | + } |
| 162 | + StarlarkFile starlarkFile = |
| 163 | + StarlarkFile.parse(ParserInput.fromUTF8(contents, path.getPathString())); |
| 164 | + if (!starlarkFile.ok()) { |
| 165 | + Event.replayEventsOn(env.getListener(), starlarkFile.errors()); |
| 166 | + throw new VendorFileFunctionException( |
| 167 | + new BadVendorFileException("error parsing VENDOR.bazel file"), Transience.PERSISTENT); |
| 168 | + } |
| 169 | + return starlarkFile; |
| 170 | + } |
| 171 | + |
| 172 | + /** Thrown when something is wrong with the contents of the VENDOR.bazel file. */ |
| 173 | + public static class BadVendorFileException extends Exception { |
| 174 | + public BadVendorFileException(String message) { |
| 175 | + super(message); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + static class VendorFileFunctionException extends SkyFunctionException { |
| 180 | + private VendorFileFunctionException(Exception e, Transience transience) { |
| 181 | + super(e, transience); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments