Skip to content

Commit 526b33d

Browse files
dzbarskyUebelAndre
andauthored
Use ctx.watch properly for crate_universe (#3569)
I don't think rctx.read did what the comment was trying to do. https://bazel.build/rules/lib/builtins/repository_ctx#read says the watch parameter "**default is 'auto'**...Can be the string 'yes', 'no', or 'auto'. Passing 'yes' is equivalent to immediately invoking the [watch()](https://bazel.build/rules/lib/builtins/repository_ctx#watch) method; passing 'no' does not attempt to watch the file; **passing 'auto' will only attempt to watch the file when it is legal to do so** (see watch() docs for more information." https://bazel.build/rules/lib/builtins/repository_ctx#watch says "Note that attempting to watch paths inside the repo currently being fetched, or inside the working directory of the current module extension, will result in an error. **A module extension attempting to watch a path outside the current Bazel workspace will also result in an error.** " This means that paths outside the workspace were already not getting watched, so we can filter them out and then use `.watch` instead of `.read` Co-authored-by: UebelAndre <[email protected]>
1 parent 5851175 commit 526b33d

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

crate_universe/extensions.bzl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,7 @@ def _generate_hub_and_spokes(
684684

685685
paths_to_track = json.decode(module_ctx.read(paths_to_track_file))
686686
for path in paths_to_track:
687-
# This read triggers watching the file at this path and invalidates the repository_rule which will get re-run.
688-
# Ideally we'd use module_ctx.watch, but it doesn't support files outside of the workspace, and we need to support that.
689-
module_ctx.read(path)
687+
module_ctx.watch(path)
690688

691689
warnings_output_file = json.decode(module_ctx.read(warnings_output_file))
692690
for warning in warnings_output_file:

crate_universe/private/crates_repository.bzl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@ def _crates_repository_impl(repository_ctx):
119119

120120
paths_to_track = json.decode(repository_ctx.read(paths_to_track_file))
121121
for path in paths_to_track:
122-
# This read triggers watching the file at this path and invalidates the repository_rule which will get re-run.
123-
# Ideally we'd use repository_ctx.watch, but it doesn't support files outside of the workspace, and we need to support that.
124-
repository_ctx.read(path)
122+
repository_ctx.watch(path)
125123

126124
warnings_output_file = json.decode(repository_ctx.read(warnings_output_file))
127125
for warning in warnings_output_file:

crate_universe/src/cli/generate.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {
137137
.values()
138138
.filter_map(|crate_context| crate_context.repository.as_ref()),
139139
context.unused_patches.iter(),
140+
&opt.nonhermetic_root_bazel_workspace_dir,
140141
)?;
141142

142143
return Ok(());
@@ -193,6 +194,7 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {
193194
splicing_manifest.manifests.keys().cloned(),
194195
annotations.lockfile.crates.values(),
195196
cargo_lockfile.patch.unused.iter(),
197+
&opt.nonhermetic_root_bazel_workspace_dir,
196198
)?;
197199

198200
// Generate renderable contexts for each package
@@ -252,6 +254,7 @@ fn write_paths_to_track<
252254
manifests: Paths,
253255
source_annotations: SourceAnnotations,
254256
unused_patches: UnusedPatches,
257+
nonhermetic_root_bazel_workspace_dir: &Utf8PathBuf,
255258
) -> Result<()> {
256259
let source_annotation_manifests: BTreeSet<_> = source_annotations
257260
.filter_map(|v| {
@@ -266,6 +269,8 @@ fn write_paths_to_track<
266269
.iter()
267270
.cloned()
268271
.chain(manifests)
272+
// Paths outside the bazel workspace cannot be `.watch`-ed.
273+
.filter(|p| p.starts_with(nonhermetic_root_bazel_workspace_dir))
269274
.collect();
270275
std::fs::write(
271276
output_file,

0 commit comments

Comments
 (0)