|
| 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 | + |
| 16 | +package com.google.devtools.build.lib.bazel.bzlmod; |
| 17 | + |
| 18 | +import static com.google.common.collect.ImmutableSet.toImmutableSet; |
| 19 | +import static com.google.devtools.build.lib.bazel.bzlmod.BazelLockFileFunction.LOCKFILE_MODE; |
| 20 | +import static com.google.devtools.build.lib.bazel.bzlmod.ModuleFileFunction.IGNORE_DEV_DEPS; |
| 21 | +import static com.google.devtools.build.lib.bazel.bzlmod.ModuleFileFunction.MODULE_OVERRIDES; |
| 22 | +import static com.google.devtools.build.lib.skyframe.PrecomputedValue.STARLARK_SEMANTICS; |
| 23 | + |
| 24 | +import com.google.common.collect.ImmutableSet; |
| 25 | +import com.google.devtools.build.lib.cmdline.Label; |
| 26 | +import com.google.devtools.build.lib.cmdline.LabelSyntaxException; |
| 27 | +import com.google.devtools.build.lib.cmdline.RepositoryName; |
| 28 | +import com.google.devtools.build.lib.rules.repository.NeedsSkyframeRestartException; |
| 29 | +import com.google.devtools.build.lib.rules.repository.RepositoryFunction; |
| 30 | +import com.google.devtools.build.lib.server.FailureDetails; |
| 31 | +import com.google.devtools.build.lib.skyframe.RepositoryMappingValue; |
| 32 | +import com.google.devtools.build.lib.vfs.RootedPath; |
| 33 | +import com.google.devtools.build.skyframe.SkyFunction; |
| 34 | +import com.google.devtools.build.skyframe.SkyFunctionException; |
| 35 | +import com.google.devtools.build.skyframe.SkyKey; |
| 36 | +import com.google.devtools.build.skyframe.SkyValue; |
| 37 | +import com.google.devtools.build.skyframe.SkyframeLookupResult; |
| 38 | +import javax.annotation.Nullable; |
| 39 | +import net.starlark.java.eval.EvalException; |
| 40 | + |
| 41 | +/** |
| 42 | + * Computes all information required for the {@code bazel mod tidy} command. The evaluation of all |
| 43 | + * module extensions used by the root module is triggered to, as a side effect, emit any {@link |
| 44 | + * RootModuleFileFixupEvent}s. |
| 45 | + */ |
| 46 | +public class BazelModTidyFunction implements SkyFunction { |
| 47 | + |
| 48 | + @Override |
| 49 | + @Nullable |
| 50 | + public SkyValue compute(SkyKey skyKey, Environment env) |
| 51 | + throws InterruptedException, SkyFunctionException { |
| 52 | + BazelDepGraphValue depGraphValue = (BazelDepGraphValue) env.getValue(BazelDepGraphValue.KEY); |
| 53 | + if (depGraphValue == null) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + RepositoryMappingValue bazelToolsRepoMapping = |
| 57 | + (RepositoryMappingValue) |
| 58 | + env.getValue(RepositoryMappingValue.key(RepositoryName.BAZEL_TOOLS)); |
| 59 | + if (bazelToolsRepoMapping == null) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + Label buildozerLabel; |
| 63 | + try { |
| 64 | + buildozerLabel = |
| 65 | + Label.parseWithRepoContext( |
| 66 | + // This label always has the ".exe" extension, even on Unix, to get a single static |
| 67 | + // label that works on all platforms. |
| 68 | + "@buildozer_binary//:buildozer.exe", |
| 69 | + Label.RepoContext.of( |
| 70 | + RepositoryName.BAZEL_TOOLS, bazelToolsRepoMapping.getRepositoryMapping())); |
| 71 | + } catch (LabelSyntaxException e) { |
| 72 | + throw new IllegalStateException(e); |
| 73 | + } |
| 74 | + RootedPath buildozer; |
| 75 | + try { |
| 76 | + buildozer = RepositoryFunction.getRootedPathFromLabel(buildozerLabel, env); |
| 77 | + } catch (NeedsSkyframeRestartException e) { |
| 78 | + return null; |
| 79 | + } catch (EvalException e) { |
| 80 | + throw new IllegalStateException(e); |
| 81 | + } |
| 82 | + |
| 83 | + ImmutableSet<SkyKey> extensionsUsedByRootModule = |
| 84 | + depGraphValue.getExtensionUsagesTable().columnMap().get(ModuleKey.ROOT).keySet().stream() |
| 85 | + .map(SingleExtensionEvalValue::key) |
| 86 | + .collect(toImmutableSet()); |
| 87 | + SkyframeLookupResult result = env.getValuesAndExceptions(extensionsUsedByRootModule); |
| 88 | + if (env.valuesMissing()) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + for (SkyKey extension : extensionsUsedByRootModule) { |
| 92 | + try { |
| 93 | + result.getOrThrow(extension, ExternalDepsException.class); |
| 94 | + } catch (ExternalDepsException e) { |
| 95 | + if (e.getDetailedExitCode().getFailureDetail() == null |
| 96 | + || !e.getDetailedExitCode() |
| 97 | + .getFailureDetail() |
| 98 | + .getExternalDeps() |
| 99 | + .getCode() |
| 100 | + .equals(FailureDetails.ExternalDeps.Code.INVALID_EXTENSION_IMPORT)) { |
| 101 | + throw new BazelModTidyFunctionException(e, SkyFunctionException.Transience.PERSISTENT); |
| 102 | + } |
| 103 | + // This is an error bazel mod tidy can fix, so don't fail. |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return BazelModTidyValue.create( |
| 108 | + buildozer.asPath(), |
| 109 | + MODULE_OVERRIDES.get(env), |
| 110 | + IGNORE_DEV_DEPS.get(env), |
| 111 | + LOCKFILE_MODE.get(env), |
| 112 | + STARLARK_SEMANTICS.get(env)); |
| 113 | + } |
| 114 | + |
| 115 | + static final class BazelModTidyFunctionException extends SkyFunctionException { |
| 116 | + |
| 117 | + BazelModTidyFunctionException(ExternalDepsException cause, Transience transience) { |
| 118 | + super(cause, transience); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments