-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add support for include() statements in Bazel MODULE.bazel files #13701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
markhallen
wants to merge
3
commits into
main
Choose a base branch
from
bazel-add-include-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+667
−15
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
bazel/lib/dependabot/bazel/file_fetcher/include_extractor.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # typed: strict | ||
| # frozen_string_literal: true | ||
|
|
||
| require "dependabot/bazel/file_fetcher" | ||
| require "dependabot/bazel/file_fetcher/path_converter" | ||
| require "sorbet-runtime" | ||
|
|
||
| module Dependabot | ||
| module Bazel | ||
| class FileFetcher < Dependabot::FileFetchers::Base | ||
| # Extracts include() statements from MODULE.bazel files and fetches the included files. | ||
| # Bazel's include() directive allows splitting MODULE.bazel content across multiple files. | ||
| # The include() statement uses Bazel label syntax: include("//path:file.MODULE.bazel") | ||
| # See https://bazel.build/rules/lib/globals/module#include | ||
| class IncludeExtractor | ||
| extend T::Sig | ||
|
|
||
| sig do | ||
| params( | ||
| module_file: DependencyFile, | ||
| fetcher: FileFetcher | ||
| ).void | ||
| end | ||
| def initialize(module_file:, fetcher:) | ||
| @module_file = module_file | ||
| @fetcher = fetcher | ||
| @visited_files = T.let(Set.new, T::Set[String]) | ||
| end | ||
|
|
||
| # Fetches all files included via include() statements, recursively. | ||
| sig { returns([T::Array[DependencyFile], T::Set[String]]) } | ||
| def fetch_included_files | ||
| files = T.let([], T::Array[DependencyFile]) | ||
| directories = T.let(Set.new, T::Set[String]) | ||
|
|
||
| content = T.must(@module_file.content) | ||
| include_paths = extract_include_paths(content) | ||
|
|
||
| include_paths.each do |path| | ||
| next if @visited_files.include?(path) | ||
|
|
||
| @visited_files.add(path) | ||
|
|
||
| fetched_file = @fetcher.send(:fetch_file_if_present, path) | ||
| next unless fetched_file | ||
|
|
||
| files << fetched_file | ||
|
|
||
| dir = File.dirname(path) | ||
| directories.add(dir) unless dir == "." | ||
|
|
||
| nested_files, nested_dirs = fetch_nested_includes(fetched_file) | ||
| files.concat(nested_files) | ||
| nested_dirs.each { |d| directories.add(d) } | ||
| end | ||
|
|
||
| [files, directories] | ||
| end | ||
|
|
||
| private | ||
|
|
||
| sig { returns(DependencyFile) } | ||
| attr_reader :module_file | ||
|
|
||
| sig { returns(FileFetcher) } | ||
| attr_reader :fetcher | ||
|
|
||
| sig { returns(T::Set[String]) } | ||
| attr_reader :visited_files | ||
|
|
||
| # Extracts file paths from include() statements. | ||
| # Only extracts workspace-relative paths (//...) and filters out external repositories. | ||
| sig { params(content: String).returns(T::Array[String]) } | ||
| def extract_include_paths(content) | ||
| paths = [] | ||
|
|
||
| # Match include("//path:file") and include("//path/to:file.MODULE.bazel") | ||
| content.scan(%r{include\s*\(\s*"(//[^"]+)"}) do |match| | ||
| label = match[0] | ||
| path = PathConverter.label_to_path(label) | ||
| paths << path unless path.empty? | ||
| end | ||
|
|
||
| # Match include(":file") for same-directory includes | ||
| content.scan(/include\s*\(\s*"(:[^"]+)"/) do |match| | ||
| label = match[0] | ||
| context_dir = File.dirname(@module_file.name) | ||
| context_dir = nil if context_dir == "." | ||
| path = PathConverter.label_to_path(label, context_dir: context_dir) | ||
| paths << path unless path.empty? | ||
| end | ||
|
|
||
| paths.uniq | ||
| end | ||
|
|
||
| sig { params(included_file: DependencyFile).returns([T::Array[DependencyFile], T::Set[String]]) } | ||
| def fetch_nested_includes(included_file) | ||
| nested_extractor = IncludeExtractor.new(module_file: included_file, fetcher: @fetcher) | ||
| nested_extractor.instance_variable_set(:@visited_files, @visited_files) | ||
| nested_extractor.fetch_included_files | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.