bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.98k stars 4.03k forks source link

use_repo_rule crashes Bazel when loading a non-existent repo rule #23138

Open meteorcloudy opened 1 month ago

meteorcloudy commented 1 month ago

Description of the bug:

I accidentally load new_git_repository from http.bzl

new_git_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "new_git_repository")

and Bazel crashes like this

Starting local Bazel server and connecting to it...
Computing main repo mapping: 
Computing main repo mapping: 
Computing main repo mapping: 
Loading: 
Loading: 0 packages loaded
Analyzing: target //:foo (1 packages loaded, 0 targets configured)
Analyzing: target //:foo (1 packages loaded, 0 targets configured)
[0 / 1] [Prepa] BazelWorkspaceStatusAction stable-status.txt
FATAL: bazel crashed due to an internal error. Printing stack trace:
java.lang.IllegalStateException: Unexpected analysis error: ConfiguredTargetKey{label=//:foo, config=BuildConfigurationKey[5ce19e8228b5c7a746c76c3e50053c1002c2d8b472fc30897311a7ce4b8fb15c]} -> ErrorInfo{exception=com.google.devtools.build.lib.bazel.bzlmod.ExternalDepsException: @@bazel_tools//tools/build_defs/repo:http.bzl does not export a repository_rule called new_git_repository, yet its use is requested at /usr/local/google/home/pcloudy/.cache/bazel/_bazel_pcloudy/4c5ccabacef1ddac6600d71a1b8bfa58/sandbox/linux-sandbox/2917/execroot/_main/_tmp/f1972a83c536d7254318d49f3ac97cd4/workspace/main/MODULE.bazel:2:19, cycles=[], isCatastrophic=false, isDirectlyTransient=false, isTransitivelyTransient=false}, ([ConfiguredTargetKey{label=//:foo, config=BuildConfigurationKey[5ce19e8228b5c7a746c76c3e50053c1002c2d8b472fc30897311a7ce4b8fb15c]}])
        at com.google.devtools.build.lib.bugreport.BugReport.sendBugReport(BugReport.java:196)
        at com.google.devtools.build.lib.bugreport.BugReport.logUnexpected(BugReport.java:167)
        at com.google.devtools.build.lib.skyframe.SkyframeErrorProcessor.logUnexpectedException(SkyframeErrorProcessor.java:778)
        at com.google.devtools.build.lib.skyframe.SkyframeErrorProcessor.logUnexpectedExceptionOrigin(SkyframeErrorProcessor.java:773)
        at com.google.devtools.build.lib.skyframe.SkyframeErrorProcessor.assertValidAnalysisOrExecutionException(SkyframeErrorProcessor.java:729)
        at com.google.devtools.build.lib.skyframe.SkyframeErrorProcessor.processErrors(SkyframeErrorProcessor.java:282)
        at com.google.devtools.build.lib.skyframe.SkyframeBuildView.analyzeAndExecuteTargets(SkyframeBuildView.java:820)
        at com.google.devtools.build.lib.analysis.BuildView.update(BuildView.java:346)
        at com.google.devtools.build.lib.buildtool.AnalysisAndExecutionPhaseRunner.runAnalysisAndExecutionPhase(AnalysisAndExecutionPhaseRunner.java:227)
        at com.google.devtools.build.lib.buildtool.AnalysisAndExecutionPhaseRunner.execute(AnalysisAndExecutionPhaseRunner.java:125)
        at com.google.devtools.build.lib.buildtool.BuildTool.buildTargetsWithMergedAnalysisExecution(BuildTool.java:363)
        at com.google.devtools.build.lib.buildtool.BuildTool.buildTargets(BuildTool.java:198)
        at com.google.devtools.build.lib.buildtool.BuildTool.processRequest(BuildTool.java:548)
        at com.google.devtools.build.lib.buildtool.BuildTool.processRequest(BuildTool.java:516)
        at com.google.devtools.build.lib.runtime.commands.BuildCommand.exec(BuildCommand.java:107)
        at com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.execExclusively(BlazeCommandDispatcher.java:679)
        at com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.exec(BlazeCommandDispatcher.java:252)
        at com.google.devtools.build.lib.server.GrpcServerImpl.executeCommand(GrpcServerImpl.java:607)
        at com.google.devtools.build.lib.server.GrpcServerImpl.lambda$run$1(GrpcServerImpl.java:676)
        at io.grpc.Context$1.run(Context.java:566)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.base/java.lang.Thread.run(Unknown Source)

Which category does this issue belong to?

External Dependency

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

No response

Which operating system are you running Bazel on?

No response

What is the output of bazel info release?

No response

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse HEAD ?

No response

If this is a regression, please try to identify the Bazel commit where the bug was introduced with bazelisk --bisect.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

fmeum commented 1 month ago

I think that we should add ExternalDepsException to https://github.com/bazelbuild/bazel/blob/3beaaaf23e4f6e9071ef7eabced7d64513573e80/src/main/java/com/google/devtools/build/lib/skyframe/SkyframeErrorProcessor.java#L785-L787

This isn't needed for repo rule errors as those are translated into a RepositoryFetchException (and thus a NoSuchPackageException) at https://github.com/bazelbuild/bazel/blob/239682bfaf9f479499a6e8b41aadde808b3a5831/src/main/java/com/google/devtools/build/lib/skyframe/PackageLookupFunction.java#L394

CC @katre