Skip to content

Commit

Permalink
Correct and narrow the exception types used in bzlmod skyfunctions.
Browse files Browse the repository at this point in the history
This is a first step towards bazelbuild#18629.
  • Loading branch information
katre committed Jun 9, 2023
1 parent 7a85e5f commit c82c0f2
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public BazelDepGraphFunction(Path rootDirectory) {
@Override
@Nullable
public SkyValue compute(SkyKey skyKey, Environment env)
throws SkyFunctionException, InterruptedException {
throws BazelDepGraphFunctionException, InterruptedException {
RootModuleFileValue root =
(RootModuleFileValue) env.getValue(ModuleFileValue.KEY_FOR_ROOT_MODULE);
if (root == null) {
Expand Down Expand Up @@ -115,8 +115,12 @@ public SkyValue compute(SkyKey skyKey, Environment env)
}
depGraph = selectionResult.getResolvedDepGraph();
if (lockfileMode.equals(LockfileMode.UPDATE)) {
BazelLockFileFunction.updateLockedModule(
rootDirectory, root.getModuleFileHash(), flags, localOverrideHashes, depGraph);
try {
BazelLockFileFunction.updateLockedModule(
rootDirectory, root.getModuleFileHash(), flags, localOverrideHashes, depGraph);
} catch (ExternalDepsException e) {
throw new BazelDepGraphFunctionException(e, Transience.PERSISTENT);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public BazelLockFileFunction(Path rootDirectory) {
@Override
@Nullable
public SkyValue compute(SkyKey skyKey, Environment env)
throws SkyFunctionException, InterruptedException {
throws BazelLockfileFunctionException, InterruptedException {
RootedPath lockfilePath =
RootedPath.toRootedPath(Root.fromPath(rootDirectory), LabelConstants.MODULE_LOCKFILE_NAME);

Expand Down Expand Up @@ -104,7 +104,7 @@ public static void updateLockedModule(
BzlmodFlagsAndEnvVars flags,
ImmutableMap<String, String> localOverrideHashes,
ImmutableMap<ModuleKey, Module> resolvedDepGraph)
throws BazelLockfileFunctionException {
throws ExternalDepsException {
RootedPath lockfilePath =
RootedPath.toRootedPath(Root.fromPath(rootDirectory), LabelConstants.MODULE_LOCKFILE_NAME);

Expand All @@ -118,16 +118,14 @@ public static void updateLockedModule(
try {
FileSystemUtils.writeContent(lockfilePath.asPath(), UTF_8, LOCKFILE_GSON.toJson(value));
} catch (IOException e) {
throw new BazelLockfileFunctionException(
ExternalDepsException.withCauseAndMessage(
Code.BAD_MODULE, e, "Unable to update the MODULE.bazel.lock file"),
Transience.PERSISTENT);
throw ExternalDepsException.withCauseAndMessage(
Code.BAD_MODULE, e, "Unable to update the MODULE.bazel.lock file");
}
}

static final class BazelLockfileFunctionException extends SkyFunctionException {

BazelLockfileFunctionException(Exception cause, Transience transience) {
BazelLockfileFunctionException(ExternalDepsException cause, Transience transience) {
super(cause, transience);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleInspectorValue.AugmentedModule.ResolutionReason;
import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileValue.RootModuleFileValue;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionException;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
import java.util.HashMap;
Expand All @@ -42,8 +41,7 @@ public class BazelModuleInspectorFunction implements SkyFunction {

@Override
@Nullable
public SkyValue compute(SkyKey skyKey, Environment env)
throws SkyFunctionException, InterruptedException {
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException {
RootModuleFileValue root =
(RootModuleFileValue) env.getValue(ModuleFileValue.KEY_FOR_ROOT_MODULE);
if (root == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class BazelModuleResolutionFunction implements SkyFunction {
@Override
@Nullable
public SkyValue compute(SkyKey skyKey, Environment env)
throws SkyFunctionException, InterruptedException {
throws BazelModuleResolutionFunctionException, InterruptedException {

ClientEnvironmentValue allowedYankedVersionsFromEnv =
(ClientEnvironmentValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ExternalDepsException extends Exception implements DetailedExceptio

private final DetailedExitCode detailedExitCode;

public ExternalDepsException(String message, @Nullable Throwable cause, ExternalDeps.Code code) {
private ExternalDepsException(String message, @Nullable Throwable cause, ExternalDeps.Code code) {
super(message, cause);
detailedExitCode =
DetailedExitCode.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public ModuleFileFunction(
@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env)
throws SkyFunctionException, InterruptedException {
throws ModuleFileFunctionException, InterruptedException {
StarlarkSemantics starlarkSemantics = PrecomputedValue.STARLARK_SEMANTICS.get(env);
if (starlarkSemantics == null) {
return null;
Expand Down Expand Up @@ -144,7 +144,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)

@Nullable
private SkyValue computeForRootModule(StarlarkSemantics starlarkSemantics, Environment env)
throws SkyFunctionException, InterruptedException {
throws ModuleFileFunctionException, InterruptedException {
RootedPath moduleFilePath =
RootedPath.toRootedPath(
Root.fromPath(workspaceRoot), LabelConstants.MODULE_DOT_BAZEL_FILE_NAME);
Expand Down Expand Up @@ -331,7 +331,7 @@ private static ModuleFileFunctionException errorf(

static final class ModuleFileFunctionException extends SkyFunctionException {

ModuleFileFunctionException(Exception cause) {
ModuleFileFunctionException(ExternalDepsException cause) {
super(cause, Transience.TRANSIENT);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void setRepositoryRemoteExecutor(RepositoryRemoteExecutor repositoryRemot
@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env)
throws SkyFunctionException, InterruptedException {
throws SingleExtensionEvalFunctionException, InterruptedException {
StarlarkSemantics starlarkSemantics = PrecomputedValue.STARLARK_SEMANTICS.get(env);
if (starlarkSemantics == null) {
return null;
Expand Down Expand Up @@ -297,7 +297,11 @@ private ModuleExtensionContext createContext(

static final class SingleExtensionEvalFunctionException extends SkyFunctionException {

SingleExtensionEvalFunctionException(Exception cause, Transience transience) {
SingleExtensionEvalFunctionException(ExternalDepsException cause, Transience transience) {
super(cause, transience);
}

SingleExtensionEvalFunctionException(IOException cause, Transience transience) {
super(cause, transience);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.google.common.collect.ImmutableTable;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionException;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
import javax.annotation.Nullable;
Expand All @@ -42,8 +41,7 @@ public class SingleExtensionUsagesFunction implements SkyFunction {

@Override
@Nullable
public SkyValue compute(SkyKey skyKey, Environment env)
throws SkyFunctionException, InterruptedException {
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException {
BazelDepGraphValue bazelDepGraphValue =
(BazelDepGraphValue) env.getValue(BazelDepGraphValue.KEY);
if (bazelDepGraphValue == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
import com.google.devtools.build.lib.analysis.ServerDirectories;
import com.google.devtools.build.lib.analysis.util.AnalysisMock;
import com.google.devtools.build.lib.bazel.bzlmod.BazelLockFileFunction.BazelLockfileFunctionException;
import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileValue.RootModuleFileValue;
import com.google.devtools.build.lib.bazel.repository.RepositoryOptions.BazelCompatibilityMode;
import com.google.devtools.build.lib.bazel.repository.RepositoryOptions.CheckDirectDepsMode;
Expand Down Expand Up @@ -63,7 +64,7 @@
import com.google.devtools.build.skyframe.RecordingDifferencer;
import com.google.devtools.build.skyframe.SequencedRecordingDifferencer;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionException;
import com.google.devtools.build.skyframe.SkyFunctionException.Transience;
import com.google.devtools.build.skyframe.SkyFunctionName;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
Expand Down Expand Up @@ -164,7 +165,7 @@ public void setup() throws Exception {
@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env)
throws SkyFunctionException, InterruptedException {
throws BazelLockfileFunctionException, InterruptedException {

UpdateLockFileKey key = (UpdateLockFileKey) skyKey;
BzlmodFlagsAndEnvVars flags = BazelDepGraphFunction.getFlagsAndEnvVars(env);
Expand All @@ -177,12 +178,16 @@ public SkyValue compute(SkyKey skyKey, Environment env)
if (localOverrideHashes == null) {
return null;
}
BazelLockFileFunction.updateLockedModule(
rootDirectory,
key.moduleHash(),
flags,
localOverrideHashes,
key.depGraph());
try {
BazelLockFileFunction.updateLockedModule(
rootDirectory,
key.moduleHash(),
flags,
localOverrideHashes,
key.depGraph());
} catch (ExternalDepsException e) {
throw new BazelLockfileFunctionException(e, Transience.PERSISTENT);
}
return new SkyValue() {};
}
})
Expand Down

0 comments on commit c82c0f2

Please sign in to comment.