From 936ae08181f086333ad1da960c690592fd043801 Mon Sep 17 00:00:00 2001 From: Cameron Mulhern Date: Wed, 20 Dec 2023 13:22:06 -0500 Subject: [PATCH] Enables the disabling of automatic workspace edits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit While modifying the user’s workspace can help with the ergonomics of first time setup, it should be possible to disable these non-essential options. --- refresh.template.py | 8 ++++++-- refresh_compile_commands.bzl | 22 +++++++++++++++++++++- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/refresh.template.py b/refresh.template.py index b146b97..7f6829c 100644 --- a/refresh.template.py +++ b/refresh.template.py @@ -1052,8 +1052,12 @@ def _ensure_cwd_is_workspace_root(): def main(): _ensure_cwd_is_workspace_root() - _ensure_gitignore_entries_exist() - _ensure_external_workspaces_link_exists() + + if {update_gitignore}: + _ensure_gitignore_entries_exist() + + if {link_external}: + _ensure_external_workspaces_link_exists() target_flag_pairs = [ # Begin: template filled by Bazel diff --git a/refresh_compile_commands.bzl b/refresh_compile_commands.bzl index 537c9fc..9fdbbfd 100644 --- a/refresh_compile_commands.bzl +++ b/refresh_compile_commands.bzl @@ -49,6 +49,12 @@ refresh_compile_commands( # exclude_headers = "external", # Still not fast enough? # Make sure you're specifying just the targets you care about by setting `targets`, above. + + # refresh_compile_commands will automatically update .git/info/exclude by default. + # You can disable this behavior with update_gitignore = False. + + # refresh_compile_commands will automatically create a symlink for external workspaces at /external. + # You can disable this behavior with link_external = False. ``` """ @@ -63,6 +69,8 @@ def refresh_compile_commands( targets = None, exclude_headers = None, exclude_external_sources = False, + update_gitignore = True, + link_external = True, **kwargs): # For the other common attributes. Tags, compatible_with, etc. https://docs.bazel.build/versions/main/be/common-definitions.html#common-attributes. # Convert the various, acceptable target shorthands into the dictionary format # In Python, `type(x) == y` is an antipattern, but [Starlark doesn't support inheritance](https://bazel.build/rules/language), so `isinstance` doesn't exist, and this is the correct way to switch on type. @@ -88,7 +96,15 @@ def refresh_compile_commands( # Generate the core, runnable python script from refresh.template.py script_name = name + ".py" - _expand_template(name = script_name, labels_to_flags = targets, exclude_headers = exclude_headers, exclude_external_sources = exclude_external_sources, **kwargs) + _expand_template( + name = script_name, + labels_to_flags = targets, + exclude_headers = exclude_headers, + exclude_external_sources = exclude_external_sources, + update_gitignore = update_gitignore, + link_external = link_external, + **kwargs + ) # Combine them so the wrapper calls the main script native.py_binary( @@ -115,6 +131,8 @@ def _expand_template_impl(ctx): " {windows_default_include_paths}": "\n".join([" %r," % path for path in find_cpp_toolchain(ctx).built_in_include_directories]), # find_cpp_toolchain is from https://docs.bazel.build/versions/main/integrating-with-rules-cc.html "{exclude_headers}": repr(ctx.attr.exclude_headers), "{exclude_external_sources}": repr(ctx.attr.exclude_external_sources), + "{update_gitignore}": repr(ctx.attr.update_gitignore), + "{link_external}": repr(ctx.attr.link_external), }, ) return DefaultInfo(files = depset([script])) @@ -124,6 +142,8 @@ _expand_template = rule( "labels_to_flags": attr.string_dict(mandatory = True), # string keys instead of label_keyed because Bazel doesn't support parsing wildcard target patterns (..., *, :all) in BUILD attributes. "exclude_external_sources": attr.bool(default = False), "exclude_headers": attr.string(values = ["all", "external", ""]), # "" needed only for compatibility with Bazel < 3.6.0 + "update_gitignore": attr.bool(default = True), + "link_external": attr.bool(default = True), "_script_template": attr.label(allow_single_file = True, default = "refresh.template.py"), # For Windows INCLUDE. If this were eliminated, for example by the resolution of https://github.com/clangd/clangd/issues/123, we'd be able to just use a macro and skylib's expand_template rule: https://github.com/bazelbuild/bazel-skylib/pull/330 # Once https://github.com/bazelbuild/bazel/pull/17108 is widely released, we should be able to eliminate this and get INCLUDE directly. Perhaps for 7.0? Should be released in the sucessor to 6.0