rigortype 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +154 -33
- data/lib/rigor/analysis/check_rules.rb +10 -18
- data/lib/rigor/analysis/dependency_source_inference/boundary_cross_reporter.rb +75 -0
- data/lib/rigor/analysis/dependency_source_inference/builder.rb +47 -21
- data/lib/rigor/analysis/dependency_source_inference/gem_resolver.rb +1 -1
- data/lib/rigor/analysis/dependency_source_inference/index.rb +32 -3
- data/lib/rigor/analysis/dependency_source_inference/walker.rb +1 -1
- data/lib/rigor/analysis/dependency_source_inference.rb +1 -0
- data/lib/rigor/analysis/diagnostic.rb +0 -2
- data/lib/rigor/analysis/fact_store.rb +26 -6
- data/lib/rigor/analysis/result.rb +11 -3
- data/lib/rigor/analysis/rule_catalog.rb +2 -2
- data/lib/rigor/analysis/run_stats.rb +193 -0
- data/lib/rigor/analysis/runner.rb +498 -12
- data/lib/rigor/analysis/worker_session.rb +327 -0
- data/lib/rigor/builtins/imported_refinements.rb +364 -55
- data/lib/rigor/builtins/regex_refinement.rb +17 -12
- data/lib/rigor/cache/descriptor.rb +1 -1
- data/lib/rigor/cache/rbs_descriptor.rb +3 -1
- data/lib/rigor/cache/store.rb +39 -6
- data/lib/rigor/cli/diff_command.rb +1 -1
- data/lib/rigor/cli/sig_gen_command.rb +173 -0
- data/lib/rigor/cli/type_of_command.rb +1 -1
- data/lib/rigor/cli/type_scan_renderer.rb +1 -1
- data/lib/rigor/cli/type_scan_report.rb +2 -2
- data/lib/rigor/cli.rb +61 -3
- data/lib/rigor/configuration/dependencies.rb +2 -2
- data/lib/rigor/configuration.rb +131 -6
- data/lib/rigor/environment/bundle_sig_discovery.rb +198 -0
- data/lib/rigor/environment/class_registry.rb +12 -3
- data/lib/rigor/environment/lockfile_resolver.rb +125 -0
- data/lib/rigor/environment/rbs_collection_discovery.rb +126 -0
- data/lib/rigor/environment/rbs_coverage_report.rb +112 -0
- data/lib/rigor/environment/rbs_loader.rb +194 -6
- data/lib/rigor/environment/reflection.rb +152 -0
- data/lib/rigor/environment.rb +109 -6
- data/lib/rigor/flow_contribution/conflict.rb +2 -2
- data/lib/rigor/flow_contribution/element.rb +1 -1
- data/lib/rigor/flow_contribution/fact.rb +1 -1
- data/lib/rigor/flow_contribution/merge_result.rb +1 -1
- data/lib/rigor/flow_contribution/merger.rb +3 -3
- data/lib/rigor/flow_contribution.rb +2 -2
- data/lib/rigor/inference/acceptance.rb +35 -1
- data/lib/rigor/inference/block_parameter_binder.rb +0 -2
- data/lib/rigor/inference/builtins/method_catalog.rb +12 -5
- data/lib/rigor/inference/builtins/numeric_catalog.rb +15 -4
- data/lib/rigor/inference/coverage_scanner.rb +1 -1
- data/lib/rigor/inference/expression_typer.rb +77 -11
- data/lib/rigor/inference/fallback.rb +1 -1
- data/lib/rigor/inference/macro_block_self_type.rb +96 -0
- data/lib/rigor/inference/method_dispatcher/block_folding.rb +3 -5
- data/lib/rigor/inference/method_dispatcher/constant_folding.rb +29 -41
- data/lib/rigor/inference/method_dispatcher/iterator_dispatch.rb +1 -3
- data/lib/rigor/inference/method_dispatcher/kernel_dispatch.rb +4 -4
- data/lib/rigor/inference/method_dispatcher/literal_string_folding.rb +1 -1
- data/lib/rigor/inference/method_dispatcher/method_folding.rb +135 -0
- data/lib/rigor/inference/method_dispatcher/overload_selector.rb +7 -12
- data/lib/rigor/inference/method_dispatcher/rbs_dispatch.rb +27 -11
- data/lib/rigor/inference/method_dispatcher/shape_dispatch.rb +46 -44
- data/lib/rigor/inference/method_dispatcher.rb +274 -5
- data/lib/rigor/inference/method_parameter_binder.rb +22 -14
- data/lib/rigor/inference/narrowing.rb +129 -12
- data/lib/rigor/inference/rbs_type_translator.rb +0 -2
- data/lib/rigor/inference/scope_indexer.rb +14 -9
- data/lib/rigor/inference/statement_evaluator.rb +7 -7
- data/lib/rigor/inference/synthetic_method.rb +86 -0
- data/lib/rigor/inference/synthetic_method_index.rb +82 -0
- data/lib/rigor/inference/synthetic_method_scanner.rb +521 -0
- data/lib/rigor/plugin/blueprint.rb +60 -0
- data/lib/rigor/plugin/io_boundary.rb +0 -2
- data/lib/rigor/plugin/loader.rb +5 -3
- data/lib/rigor/plugin/macro/block_as_method.rb +131 -0
- data/lib/rigor/plugin/macro/external_file.rb +143 -0
- data/lib/rigor/plugin/macro/heredoc_template.rb +201 -0
- data/lib/rigor/plugin/macro/trait_registry.rb +198 -0
- data/lib/rigor/plugin/macro.rb +31 -0
- data/lib/rigor/plugin/manifest.rb +102 -10
- data/lib/rigor/plugin/registry.rb +43 -2
- data/lib/rigor/plugin/services.rb +1 -1
- data/lib/rigor/plugin/type_node_resolver.rb +52 -0
- data/lib/rigor/plugin.rb +2 -0
- data/lib/rigor/rbs_extended/reporter.rb +91 -0
- data/lib/rigor/rbs_extended.rb +131 -32
- data/lib/rigor/scope.rb +25 -8
- data/lib/rigor/sig_gen/classification.rb +36 -0
- data/lib/rigor/sig_gen/generator.rb +1048 -0
- data/lib/rigor/sig_gen/layout_index.rb +108 -0
- data/lib/rigor/sig_gen/method_candidate.rb +62 -0
- data/lib/rigor/sig_gen/observation_collector.rb +391 -0
- data/lib/rigor/sig_gen/observed_call.rb +62 -0
- data/lib/rigor/sig_gen/path_mapper.rb +116 -0
- data/lib/rigor/sig_gen/renderer.rb +157 -0
- data/lib/rigor/sig_gen/type_elaborator.rb +92 -0
- data/lib/rigor/sig_gen/write_result.rb +48 -0
- data/lib/rigor/sig_gen/writer.rb +530 -0
- data/lib/rigor/sig_gen.rb +25 -0
- data/lib/rigor/trinary.rb +15 -11
- data/lib/rigor/type/bot.rb +6 -3
- data/lib/rigor/type/bound_method.rb +79 -0
- data/lib/rigor/type/combinator.rb +207 -3
- data/lib/rigor/type/constant.rb +13 -0
- data/lib/rigor/type/hash_shape.rb +0 -2
- data/lib/rigor/type/integer_range.rb +7 -7
- data/lib/rigor/type/refined.rb +18 -12
- data/lib/rigor/type/top.rb +4 -3
- data/lib/rigor/type/union.rb +20 -1
- data/lib/rigor/type.rb +1 -0
- data/lib/rigor/type_node/generic.rb +68 -0
- data/lib/rigor/type_node/identifier.rb +38 -0
- data/lib/rigor/type_node/indexed_access.rb +41 -0
- data/lib/rigor/type_node/integer_literal.rb +29 -0
- data/lib/rigor/type_node/name_scope.rb +52 -0
- data/lib/rigor/type_node/resolver_chain.rb +56 -0
- data/lib/rigor/type_node/string_literal.rb +32 -0
- data/lib/rigor/type_node/symbol_literal.rb +28 -0
- data/lib/rigor/type_node/union.rb +42 -0
- data/lib/rigor/type_node.rb +29 -0
- data/lib/rigor/version.rb +1 -1
- data/lib/rigor.rb +2 -0
- data/sig/rigor/analysis/check_rules/always_truthy_condition_collector.rbs +10 -0
- data/sig/rigor/analysis/check_rules/dead_assignment_collector.rbs +10 -0
- data/sig/rigor/analysis/dependency_source_inference/gem_resolver.rbs +25 -0
- data/sig/rigor/analysis/dependency_source_inference/index.rbs +9 -0
- data/sig/rigor/cli/diff_command.rbs +4 -0
- data/sig/rigor/cli/explain_command.rbs +4 -0
- data/sig/rigor/cli/sig_gen_command.rbs +4 -0
- data/sig/rigor/cli/type_scan_command.rbs +3 -0
- data/sig/rigor/environment.rbs +8 -2
- data/sig/rigor/inference/builtins/method_catalog.rbs +4 -0
- data/sig/rigor/inference/builtins/numeric_catalog.rbs +3 -0
- data/sig/rigor/inference/builtins.rbs +2 -0
- data/sig/rigor/plugin/access_denied_error.rbs +3 -0
- data/sig/rigor/plugin/base.rbs +6 -0
- data/sig/rigor/plugin/blueprint.rbs +7 -0
- data/sig/rigor/plugin/fact_store.rbs +11 -0
- data/sig/rigor/plugin/io_boundary.rbs +4 -0
- data/sig/rigor/plugin/load_error.rbs +6 -0
- data/sig/rigor/plugin/loader.rbs +20 -0
- data/sig/rigor/plugin/manifest.rbs +9 -0
- data/sig/rigor/plugin/registry.rbs +16 -0
- data/sig/rigor/plugin/services.rbs +3 -0
- data/sig/rigor/plugin/trust_policy.rbs +4 -0
- data/sig/rigor/plugin/type_node_resolver.rbs +3 -0
- data/sig/rigor/plugin.rbs +8 -0
- data/sig/rigor/scope.rbs +4 -2
- data/sig/rigor/type.rbs +28 -6
- data/sig/rigor.rbs +35 -2
- metadata +90 -1
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "yaml"
|
|
4
|
+
|
|
5
|
+
module Rigor
|
|
6
|
+
class Environment
|
|
7
|
+
# Open item O4 — target-project Bundler awareness.
|
|
8
|
+
#
|
|
9
|
+
# Walks a Bundler-installed gem tree (e.g., the project's
|
|
10
|
+
# `vendor/bundle` or a Docker-mounted bundle root) and
|
|
11
|
+
# returns the per-gem `sig/` directories to feed into
|
|
12
|
+
# `RbsLoader`'s `signature_paths:`. Of the ~3% of gems that
|
|
13
|
+
# ship `sig/` in their gem package today (per the four-project
|
|
14
|
+
# Mastodon Docker bundle-install measurement on 2026-05-15:
|
|
15
|
+
# 10 of 343 gems shipped sig — `prism`, `aws-sdk-s3`,
|
|
16
|
+
# `aws-sdk-kms`, `aws-sdk-core`, `playwright-ruby-client`,
|
|
17
|
+
# `mutex_m`, `webrick`, `base64`, `stoplight`, `ffi`), this
|
|
18
|
+
# discovery surfaces the typed contract the gem author
|
|
19
|
+
# explicitly published.
|
|
20
|
+
#
|
|
21
|
+
# Conflicts with rigor's bundled stdlib RBS (the prism case
|
|
22
|
+
# was the motivating example) degrade gracefully via O7's
|
|
23
|
+
# failure-memo in `RbsLoader#env`: a single warning naming
|
|
24
|
+
# the offending file is emitted and analysis continues with
|
|
25
|
+
# `Dynamic[top]` everywhere rather than hanging.
|
|
26
|
+
#
|
|
27
|
+
# The discovery is intentionally a pure file-system walk —
|
|
28
|
+
# no `Bundler` API call, no `Gemfile.lock` parse — so rigor
|
|
29
|
+
# doesn't need the target project's Bundler context.
|
|
30
|
+
module BundleSigDiscovery
|
|
31
|
+
# Gems already covered by rigor's `DEFAULT_LIBRARIES`
|
|
32
|
+
# (stdlib RBS) plus the `data/vendored_gem_sigs/` bundle.
|
|
33
|
+
# Skipping these from bundle discovery prevents
|
|
34
|
+
# `RBS::DuplicatedDeclarationError` (the prism case was the
|
|
35
|
+
# motivating example — Ruby 4.0 ships prism's RBS in
|
|
36
|
+
# stdlib, and the gem also ships its own `sig/`, so loading
|
|
37
|
+
# both raises on `Prism::BACKEND` etc.).
|
|
38
|
+
#
|
|
39
|
+
# The list is hard-coded for the MVP because it tracks
|
|
40
|
+
# rigor's bundled coverage 1:1. When a new gem is vendored
|
|
41
|
+
# under `data/vendored_gem_sigs/` or added to
|
|
42
|
+
# `DEFAULT_LIBRARIES`, add its name here.
|
|
43
|
+
SKIPPED_GEMS_BY_DEFAULT = Set[
|
|
44
|
+
# DEFAULT_LIBRARIES (lib/rigor/environment.rb)
|
|
45
|
+
"pathname", "optparse", "json", "yaml", "fileutils",
|
|
46
|
+
"tempfile", "tmpdir", "stringio", "forwardable",
|
|
47
|
+
"digest", "securerandom", "uri", "logger", "date",
|
|
48
|
+
"pp", "delegate", "singleton", "observable", "abbrev",
|
|
49
|
+
"find", "tsort", "shellwords", "benchmark", "base64",
|
|
50
|
+
"did_you_mean", "monitor", "mutex_m", "timeout",
|
|
51
|
+
"open3", "erb", "etc", "ipaddr", "bigdecimal",
|
|
52
|
+
"bigdecimal-math", "prettyprint",
|
|
53
|
+
"random-formatter", "time", "open-uri", "resolv",
|
|
54
|
+
"csv", "pstore", "objspace", "io-console", "cgi", "cgi-escape",
|
|
55
|
+
"strscan",
|
|
56
|
+
"prism", "rbs",
|
|
57
|
+
# data/vendored_gem_sigs/
|
|
58
|
+
"pg", "mysql2", "nokogiri", "bcrypt", "redis", "idn-ruby"
|
|
59
|
+
].freeze
|
|
60
|
+
|
|
61
|
+
# @param bundle_path [String, Pathname, nil] explicit path
|
|
62
|
+
# to the bundler install root. When `nil`, falls back to
|
|
63
|
+
# `auto_detect` if `auto_detect:` is true.
|
|
64
|
+
# @param project_root [String] resolution base for relative
|
|
65
|
+
# `bundle_path:` and the auto-detect search.
|
|
66
|
+
# @param auto_detect [Boolean] when true and `bundle_path:`
|
|
67
|
+
# is nil, try `.bundle/config`'s `BUNDLE_PATH:` and
|
|
68
|
+
# `vendor/bundle/` under `project_root`.
|
|
69
|
+
# @param skip_gems [Set<String>] gem names to exclude from
|
|
70
|
+
# discovery. Defaults to {SKIPPED_GEMS_BY_DEFAULT}.
|
|
71
|
+
# @param locked_gems [Hash{String => LockfileResolver::LockedGem}, nil]
|
|
72
|
+
# Optional O4-Layer-3 filter. When non-nil and non-empty,
|
|
73
|
+
# only `sig/` directories whose gem `(name, version,
|
|
74
|
+
# platform)` tuple matches a lockfile entry are returned.
|
|
75
|
+
# Bundle entries absent from the lockfile (or at a drifted
|
|
76
|
+
# version) are silently dropped — the lockfile is treated
|
|
77
|
+
# as the source of truth for "what gems this project
|
|
78
|
+
# actually declares". Pass `nil` (the default) to keep
|
|
79
|
+
# the pre-Layer-3 behaviour of returning every non-skipped
|
|
80
|
+
# `sig/` under the bundle.
|
|
81
|
+
# @return [Array<Pathname>] every `<gem-dir>/sig` directory
|
|
82
|
+
# under the resolved bundle path, minus any whose gem
|
|
83
|
+
# name is in `skip_gems` and (when `locked_gems` is
|
|
84
|
+
# supplied) minus any whose `(name, version, platform)`
|
|
85
|
+
# does not match a lockfile entry.
|
|
86
|
+
def self.discover(bundle_path:, project_root: Dir.pwd, auto_detect: true,
|
|
87
|
+
skip_gems: SKIPPED_GEMS_BY_DEFAULT, locked_gems: nil)
|
|
88
|
+
resolved = resolve_bundle_path(
|
|
89
|
+
bundle_path: bundle_path,
|
|
90
|
+
project_root: project_root,
|
|
91
|
+
auto_detect: auto_detect
|
|
92
|
+
)
|
|
93
|
+
return [] if resolved.nil?
|
|
94
|
+
|
|
95
|
+
# `<bundle>/ruby/X.Y.Z/gems/<name>-<ver>/sig/` is the
|
|
96
|
+
# canonical bundler layout. `*` on the ruby version dir
|
|
97
|
+
# picks up whichever Ruby the bundle was installed for.
|
|
98
|
+
all = Dir.glob(resolved.join("ruby", "*", "gems", "*", "sig")).map { |d| Pathname.new(d) }
|
|
99
|
+
filtered = all.reject { |sig_dir| skip_gems.include?(gem_name_from_sig_path(sig_dir)) }
|
|
100
|
+
return filtered if locked_gems.nil? || locked_gems.empty?
|
|
101
|
+
|
|
102
|
+
expected_dirs = expected_gem_dirs(locked_gems)
|
|
103
|
+
filtered.select { |sig_dir| expected_dirs.include?(sig_dir.parent.basename.to_s) }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# `{name => LockedGem}` → set of canonical bundler gem
|
|
107
|
+
# directory basenames. Pure-Ruby gems install as
|
|
108
|
+
# `<name>-<version>`; platform-specific gems install as
|
|
109
|
+
# `<name>-<version>-<platform>` (e.g. `ffi-1.17.4-aarch64-linux-gnu`).
|
|
110
|
+
# Lockfile platform `"ruby"` is the pure-Ruby case; any
|
|
111
|
+
# other value is treated as a platform tag.
|
|
112
|
+
def self.expected_gem_dirs(locked_gems)
|
|
113
|
+
locked_gems.each_value.with_object(Set.new) do |locked, set|
|
|
114
|
+
base = "#{locked.name}-#{locked.version}"
|
|
115
|
+
set << if locked.platform == "ruby" || locked.platform.empty?
|
|
116
|
+
base
|
|
117
|
+
else
|
|
118
|
+
"#{base}-#{locked.platform}"
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
private_class_method :expected_gem_dirs
|
|
123
|
+
|
|
124
|
+
# `<bundle>/ruby/X.Y.Z/gems/<name>-<ver>/sig` → `<name>`.
|
|
125
|
+
# The gem directory follows the canonical
|
|
126
|
+
# `<name>-<version>` pattern; we strip everything from the
|
|
127
|
+
# last hyphen onwards to recover the name. (Platform-tagged
|
|
128
|
+
# variants like `ffi-1.17.4-aarch64-linux-gnu/` keep their
|
|
129
|
+
# platform suffix in the version part, so the first hyphen
|
|
130
|
+
# from the right is still the name boundary.)
|
|
131
|
+
#
|
|
132
|
+
# Public so the O4 Layer 3 slice-3 coverage report
|
|
133
|
+
# (`RbsCoverageReport`) can classify discovered bundle sigs
|
|
134
|
+
# against locked gem names without re-running discovery.
|
|
135
|
+
def self.gem_name_from_sig_path(sig_dir)
|
|
136
|
+
gem_dir = sig_dir.parent.basename.to_s
|
|
137
|
+
# Strip `-<version>` and any platform suffix. The version
|
|
138
|
+
# always starts with a digit, so split at the first
|
|
139
|
+
# `-` followed by a digit.
|
|
140
|
+
gem_dir.sub(/-\d.*\z/, "")
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# Returns `Pathname` resolved bundle path, or `nil` when
|
|
144
|
+
# neither explicit nor auto-detected. Public for the stats
|
|
145
|
+
# banner so end users can see what rigor picked up.
|
|
146
|
+
def self.resolve_bundle_path(bundle_path:, project_root: Dir.pwd, auto_detect: true)
|
|
147
|
+
if bundle_path
|
|
148
|
+
path = Pathname.new(File.expand_path(bundle_path.to_s, project_root))
|
|
149
|
+
return path if path.directory?
|
|
150
|
+
|
|
151
|
+
return nil
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
return nil unless auto_detect
|
|
155
|
+
|
|
156
|
+
detected = auto_detect(project_root: project_root)
|
|
157
|
+
Pathname.new(detected) if detected
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Auto-detection order:
|
|
161
|
+
# 1. `<project_root>/.bundle/config` carries `BUNDLE_PATH:`
|
|
162
|
+
# set by `bundle config set --local path <dir>`.
|
|
163
|
+
# 2. `<project_root>/vendor/bundle/` — the conventional
|
|
164
|
+
# in-tree install location when a developer ran
|
|
165
|
+
# `bundle install --path vendor/bundle`.
|
|
166
|
+
# 3. `nil` — let the caller proceed without bundle sig
|
|
167
|
+
# discovery (rigor's vendored RBS still loads).
|
|
168
|
+
def self.auto_detect(project_root:)
|
|
169
|
+
from_config = read_bundle_config_path(project_root)
|
|
170
|
+
return File.expand_path(from_config, project_root) if from_config
|
|
171
|
+
|
|
172
|
+
vendor = File.join(project_root, "vendor", "bundle")
|
|
173
|
+
return vendor if File.directory?(vendor)
|
|
174
|
+
|
|
175
|
+
nil
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def self.read_bundle_config_path(project_root)
|
|
179
|
+
config_path = File.join(project_root, ".bundle", "config")
|
|
180
|
+
return nil unless File.exist?(config_path)
|
|
181
|
+
|
|
182
|
+
# `.bundle/config` is YAML with all-caps env-style keys.
|
|
183
|
+
# `BUNDLE_PATH:` is the canonical key (Bundler 2.x); the
|
|
184
|
+
# `--path` flag sets it.
|
|
185
|
+
data = YAML.safe_load_file(config_path)
|
|
186
|
+
return nil unless data.is_a?(Hash)
|
|
187
|
+
|
|
188
|
+
data["BUNDLE_PATH"]
|
|
189
|
+
rescue StandardError
|
|
190
|
+
# Malformed `.bundle/config` should not break analysis;
|
|
191
|
+
# silently skip auto-detection.
|
|
192
|
+
nil
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
private_class_method :read_bundle_config_path
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
@@ -67,10 +67,19 @@ module Rigor
|
|
|
67
67
|
|
|
68
68
|
private
|
|
69
69
|
|
|
70
|
+
# ADR-15 Phase 4b — the default registry MUST be
|
|
71
|
+
# `Ractor.shareable?` so worker Ractors that consult
|
|
72
|
+
# `Environment.for_project`'s default `class_registry:`
|
|
73
|
+
# don't trip `Ractor::IsolationError`. The internal
|
|
74
|
+
# `@nominals` / `@class_objects` Hashes are populated
|
|
75
|
+
# via `register`, then `Ractor.make_shareable`
|
|
76
|
+
# recursively freezes the registry, the two Hashes,
|
|
77
|
+
# and confirms every entry (Type::Nominal carriers +
|
|
78
|
+
# core Ruby classes) is itself shareable.
|
|
70
79
|
def build_default
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
80
|
+
registry = new
|
|
81
|
+
CORE_BUILT_INS.each { |klass| registry.register(klass) }
|
|
82
|
+
Ractor.make_shareable(registry)
|
|
74
83
|
end
|
|
75
84
|
end
|
|
76
85
|
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rigor
|
|
4
|
+
class Environment
|
|
5
|
+
# Open item O4 Layer 3 — Gemfile.lock parse.
|
|
6
|
+
#
|
|
7
|
+
# Parses a target project's `Gemfile.lock` via Bundler's
|
|
8
|
+
# `LockfileParser` and exposes the locked gem set as a frozen
|
|
9
|
+
# `Hash[String, LockfileResolver::LockedGem]` keyed by gem
|
|
10
|
+
# name. Used by {Rigor::Environment::BundleSigDiscovery} as a
|
|
11
|
+
# filter so the discovered `sig/` directories under the
|
|
12
|
+
# bundler install root are limited to gems the project
|
|
13
|
+
# actually declares (and at the version it declared them).
|
|
14
|
+
#
|
|
15
|
+
# The resolver is intentionally read-only. It does NOT load
|
|
16
|
+
# the project's `Gemfile`, does NOT resolve dependencies,
|
|
17
|
+
# does NOT touch the network, and does NOT require the
|
|
18
|
+
# target project's Bundler context. It only reads bytes from
|
|
19
|
+
# the lockfile.
|
|
20
|
+
#
|
|
21
|
+
# Failure modes are deliberately quiet: a missing or
|
|
22
|
+
# malformed lockfile returns an empty map. The auto-detect
|
|
23
|
+
# path is the configuration default; users who want hard
|
|
24
|
+
# failures should pass an explicit `bundler.lockfile:` and
|
|
25
|
+
# check the result via the stats banner.
|
|
26
|
+
module LockfileResolver
|
|
27
|
+
# Frozen value object for one locked gem entry.
|
|
28
|
+
#
|
|
29
|
+
# `version` is the resolved version string (e.g. "8.0.1");
|
|
30
|
+
# `platform` is the lockfile's platform tag, normalised to
|
|
31
|
+
# `"ruby"` when the lockfile records `ruby` and to the
|
|
32
|
+
# raw String otherwise (e.g. "aarch64-linux-gnu").
|
|
33
|
+
LockedGem = Data.define(:name, :version, :platform) do
|
|
34
|
+
def initialize(name:, version:, platform:)
|
|
35
|
+
super(
|
|
36
|
+
name: -name.to_s,
|
|
37
|
+
version: -version.to_s,
|
|
38
|
+
platform: -platform.to_s
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @param lockfile_path [String, Pathname, nil] explicit path
|
|
44
|
+
# to the Gemfile.lock. When `nil`, falls back to
|
|
45
|
+
# `auto_detect` if `auto_detect:` is true.
|
|
46
|
+
# @param project_root [String] resolution base for a
|
|
47
|
+
# relative `lockfile_path:` and the auto-detect search.
|
|
48
|
+
# @param auto_detect [Boolean] when true and
|
|
49
|
+
# `lockfile_path:` is nil, look for
|
|
50
|
+
# `<project_root>/Gemfile.lock`.
|
|
51
|
+
# @return [Hash{String => LockedGem}] frozen map of gem
|
|
52
|
+
# name → locked entry. Returns the empty frozen hash
|
|
53
|
+
# when no lockfile is resolvable, when the file is
|
|
54
|
+
# unreadable, or when Bundler refuses to parse it.
|
|
55
|
+
def self.locked_gems(lockfile_path:, project_root: Dir.pwd, auto_detect: true)
|
|
56
|
+
resolved = resolve_lockfile_path(
|
|
57
|
+
lockfile_path: lockfile_path,
|
|
58
|
+
project_root: project_root,
|
|
59
|
+
auto_detect: auto_detect
|
|
60
|
+
)
|
|
61
|
+
return EMPTY unless resolved
|
|
62
|
+
|
|
63
|
+
parse(resolved)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Returns the resolved lockfile path (`Pathname`) or `nil`
|
|
67
|
+
# when neither explicit nor auto-detect produces one.
|
|
68
|
+
# Public so the stats banner can show what rigor picked up.
|
|
69
|
+
def self.resolve_lockfile_path(lockfile_path:, project_root: Dir.pwd, auto_detect: true)
|
|
70
|
+
if lockfile_path
|
|
71
|
+
path = Pathname.new(File.expand_path(lockfile_path.to_s, project_root))
|
|
72
|
+
return path if path.file?
|
|
73
|
+
|
|
74
|
+
return nil
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
return nil unless auto_detect
|
|
78
|
+
|
|
79
|
+
candidate = Pathname.new(File.join(project_root, "Gemfile.lock"))
|
|
80
|
+
candidate.file? ? candidate : nil
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
EMPTY = {}.freeze
|
|
84
|
+
private_constant :EMPTY
|
|
85
|
+
|
|
86
|
+
# Parses a Gemfile.lock at the given path. Bundler load
|
|
87
|
+
# errors and malformed lockfile bytes both surface as the
|
|
88
|
+
# empty frozen hash; analysis must not crash because a
|
|
89
|
+
# lockfile is malformed. A single warning is emitted to
|
|
90
|
+
# `$stderr` so the user can see why their lockfile was
|
|
91
|
+
# ignored.
|
|
92
|
+
def self.parse(path)
|
|
93
|
+
require "bundler"
|
|
94
|
+
rescue LoadError => e
|
|
95
|
+
warn "rigor: cannot read #{path}: bundler is not available (#{e.message})"
|
|
96
|
+
EMPTY
|
|
97
|
+
else
|
|
98
|
+
do_parse(path)
|
|
99
|
+
end
|
|
100
|
+
private_class_method :parse
|
|
101
|
+
|
|
102
|
+
def self.do_parse(path)
|
|
103
|
+
body = File.read(path.to_s)
|
|
104
|
+
parser = Bundler::LockfileParser.new(body)
|
|
105
|
+
locked = parser.specs.each_with_object({}) do |spec, h|
|
|
106
|
+
# `Bundler::LazySpecification` carries name, version,
|
|
107
|
+
# platform. Platform is `Gem::Platform` or the symbol
|
|
108
|
+
# `:ruby`; both stringify cleanly. The upstream
|
|
109
|
+
# bundler RBS shim (references/rbs/sig/shims/bundler.rbs)
|
|
110
|
+
# does NOT declare `LazySpecification#platform` so the
|
|
111
|
+
# call site needs a suppression marker.
|
|
112
|
+
platform = spec.platform.to_s # rigor:disable undefined-method
|
|
113
|
+
h[spec.name.to_s] = LockedGem.new(
|
|
114
|
+
name: spec.name, version: spec.version.to_s, platform: platform
|
|
115
|
+
)
|
|
116
|
+
end
|
|
117
|
+
locked.freeze
|
|
118
|
+
rescue StandardError => e
|
|
119
|
+
warn "rigor: ignoring malformed #{path} (#{e.class}: #{e.message})"
|
|
120
|
+
EMPTY
|
|
121
|
+
end
|
|
122
|
+
private_class_method :do_parse
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "yaml"
|
|
4
|
+
|
|
5
|
+
module Rigor
|
|
6
|
+
class Environment
|
|
7
|
+
# Open item O4 Layer 3 slice 2 — `rbs collection install`
|
|
8
|
+
# awareness.
|
|
9
|
+
#
|
|
10
|
+
# When the target project has been set up with `rbs
|
|
11
|
+
# collection install` (the standard RBS-ecosystem flow for
|
|
12
|
+
# pulling community RBS from
|
|
13
|
+
# https://github.com/ruby/gem_rbs_collection), a
|
|
14
|
+
# `rbs_collection.lock.yaml` records the resolved (gem,
|
|
15
|
+
# version, source) triples and `.gem_rbs_collection/<name>/
|
|
16
|
+
# <version>/` carries the actual `.rbs` files. This module
|
|
17
|
+
# parses the lockfile and returns the per-gem RBS directory
|
|
18
|
+
# paths so they can be appended to `RbsLoader`'s
|
|
19
|
+
# `signature_paths:`.
|
|
20
|
+
#
|
|
21
|
+
# The discovery is intentionally a pure file-system + YAML
|
|
22
|
+
# walk — no Bundler API call, no network access. Failure
|
|
23
|
+
# modes (missing lockfile, malformed YAML, missing
|
|
24
|
+
# collection directory) silently degrade to an empty list.
|
|
25
|
+
module RbsCollectionDiscovery
|
|
26
|
+
# `stdlib`-typed entries in the lockfile are loaded into
|
|
27
|
+
# the RBS environment by the standard library mechanism
|
|
28
|
+
# (rigor's `Environment::DEFAULT_LIBRARIES` already covers
|
|
29
|
+
# this surface). Including them as `signature_paths:`
|
|
30
|
+
# entries would risk `RBS::DuplicatedDeclarationError`
|
|
31
|
+
# (the same hazard O7's failure-memo handles). The other
|
|
32
|
+
# documented source types — `git` (the gem_rbs_collection
|
|
33
|
+
# repo), `rubygems` (sigs lifted from a gem's bundled
|
|
34
|
+
# `sig/`), and `local` (a user-managed RBS dir) — all
|
|
35
|
+
# produce a directory under the collection root and are
|
|
36
|
+
# admitted.
|
|
37
|
+
SKIPPED_SOURCE_TYPES = Set["stdlib"].freeze
|
|
38
|
+
|
|
39
|
+
DEFAULT_COLLECTION_PATH = ".gem_rbs_collection"
|
|
40
|
+
private_constant :DEFAULT_COLLECTION_PATH
|
|
41
|
+
|
|
42
|
+
# @param lockfile_path [String, Pathname, nil] explicit
|
|
43
|
+
# path to `rbs_collection.lock.yaml`. When `nil`, falls
|
|
44
|
+
# back to `auto_detect` if `auto_detect:` is true.
|
|
45
|
+
# @param project_root [String] resolution base for
|
|
46
|
+
# relative `lockfile_path:` and the auto-detect search.
|
|
47
|
+
# @param auto_detect [Boolean] when true and
|
|
48
|
+
# `lockfile_path:` is nil, look for
|
|
49
|
+
# `<project_root>/rbs_collection.lock.yaml`.
|
|
50
|
+
# @return [Array<Pathname>] every
|
|
51
|
+
# `<collection_path>/<gem-name>/<gem-version>/`
|
|
52
|
+
# directory listed in the lockfile whose entry has a
|
|
53
|
+
# non-skipped source type and whose directory exists on
|
|
54
|
+
# disk. Returns `[]` when no lockfile is resolvable,
|
|
55
|
+
# when the YAML is unreadable, or when the collection
|
|
56
|
+
# path doesn't exist.
|
|
57
|
+
def self.discover(lockfile_path:, project_root: Dir.pwd, auto_detect: true)
|
|
58
|
+
resolved = resolve_lockfile_path(
|
|
59
|
+
lockfile_path: lockfile_path,
|
|
60
|
+
project_root: project_root,
|
|
61
|
+
auto_detect: auto_detect
|
|
62
|
+
)
|
|
63
|
+
return [] if resolved.nil?
|
|
64
|
+
|
|
65
|
+
data = read_lockfile_yaml(resolved)
|
|
66
|
+
return [] if data.nil?
|
|
67
|
+
|
|
68
|
+
collection_root = resolve_collection_root(resolved, data)
|
|
69
|
+
return [] unless collection_root.directory?
|
|
70
|
+
|
|
71
|
+
gem_paths_from(collection_root, data)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Returns the resolved lockfile path (`Pathname`) or `nil`
|
|
75
|
+
# when neither explicit nor auto-detect produces one.
|
|
76
|
+
# Public so the stats banner can surface what rigor found.
|
|
77
|
+
def self.resolve_lockfile_path(lockfile_path:, project_root: Dir.pwd, auto_detect: true)
|
|
78
|
+
if lockfile_path
|
|
79
|
+
path = Pathname.new(File.expand_path(lockfile_path.to_s, project_root))
|
|
80
|
+
return path if path.file?
|
|
81
|
+
|
|
82
|
+
return nil
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
return nil unless auto_detect
|
|
86
|
+
|
|
87
|
+
candidate = Pathname.new(File.join(project_root, "rbs_collection.lock.yaml"))
|
|
88
|
+
candidate.file? ? candidate : nil
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def self.read_lockfile_yaml(path)
|
|
92
|
+
data = YAML.safe_load_file(path.to_s, aliases: false)
|
|
93
|
+
data.is_a?(Hash) ? data : nil
|
|
94
|
+
rescue StandardError
|
|
95
|
+
nil
|
|
96
|
+
end
|
|
97
|
+
private_class_method :read_lockfile_yaml
|
|
98
|
+
|
|
99
|
+
def self.resolve_collection_root(lockfile_pathname, data)
|
|
100
|
+
rel = data["path"]
|
|
101
|
+
rel = DEFAULT_COLLECTION_PATH if rel.nil? || rel.to_s.empty?
|
|
102
|
+
# `path:` is documented as relative to the directory
|
|
103
|
+
# holding the lockfile (RBS::Collection::Config::Lockfile#fullpath).
|
|
104
|
+
lockfile_pathname.parent + Pathname.new(rel.to_s)
|
|
105
|
+
end
|
|
106
|
+
private_class_method :resolve_collection_root
|
|
107
|
+
|
|
108
|
+
def self.gem_paths_from(collection_root, data)
|
|
109
|
+
Array(data["gems"]).filter_map do |entry|
|
|
110
|
+
next unless entry.is_a?(Hash)
|
|
111
|
+
|
|
112
|
+
source_type = entry.dig("source", "type").to_s
|
|
113
|
+
next if SKIPPED_SOURCE_TYPES.include?(source_type)
|
|
114
|
+
|
|
115
|
+
name = entry["name"]
|
|
116
|
+
version = entry["version"]
|
|
117
|
+
next if name.nil? || version.nil?
|
|
118
|
+
|
|
119
|
+
gem_root = collection_root + name.to_s + version.to_s
|
|
120
|
+
gem_root if gem_root.directory?
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
private_class_method :gem_paths_from
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rigor
|
|
4
|
+
class Environment
|
|
5
|
+
# Open item O4 Layer 3 slice 3 — graceful-degradation
|
|
6
|
+
# coverage report.
|
|
7
|
+
#
|
|
8
|
+
# When the user has a `Gemfile.lock` (via slice 1) and rigor
|
|
9
|
+
# has resolved its target-project RBS sources (DEFAULT_LIBRARIES,
|
|
10
|
+
# `data/vendored_gem_sigs/`, slice-1 bundle-shipped `sig/`,
|
|
11
|
+
# slice-2 `rbs_collection.lock.yaml` paths), this module
|
|
12
|
+
# classifies each locked gem by RBS provenance and surfaces
|
|
13
|
+
# the "no RBS available" set so the run-start diagnostic in
|
|
14
|
+
# {Rigor::Analysis::Runner} can suggest `rbs collection
|
|
15
|
+
# install` or `dependencies.source_inference:` for the
|
|
16
|
+
# uncovered gems.
|
|
17
|
+
#
|
|
18
|
+
# The classification is a pure function over the inputs
|
|
19
|
+
# (`locked_gems`, two arrays of resolved sig paths). It does
|
|
20
|
+
# NOT touch the filesystem on its own — the caller passes in
|
|
21
|
+
# what discovery returned.
|
|
22
|
+
module RbsCoverageReport
|
|
23
|
+
# Frozen result row.
|
|
24
|
+
#
|
|
25
|
+
# `source` is a Symbol naming where RBS for this gem
|
|
26
|
+
# resolves; `:missing` means none of the four resolution
|
|
27
|
+
# paths covered it.
|
|
28
|
+
Coverage = Data.define(:gem_name, :version, :source) do
|
|
29
|
+
def initialize(gem_name:, version:, source:)
|
|
30
|
+
super(
|
|
31
|
+
gem_name: -gem_name.to_s,
|
|
32
|
+
version: -version.to_s,
|
|
33
|
+
source: source
|
|
34
|
+
)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Names of gems whose RBS ships under
|
|
39
|
+
# `data/vendored_gem_sigs/`. Kept in sync with the
|
|
40
|
+
# vendored-stubs directory listing; when a new gem is
|
|
41
|
+
# vendored, add its name here too. (The set is small
|
|
42
|
+
# enough that hard-coding is acceptable; a directory walk
|
|
43
|
+
# at every call would add stat-cost to no benefit.)
|
|
44
|
+
VENDORED_GEM_NAMES = Set[
|
|
45
|
+
"bcrypt", "idn-ruby", "mysql2", "nokogiri", "pg", "redis"
|
|
46
|
+
].freeze
|
|
47
|
+
|
|
48
|
+
# @param locked_gems [Hash{String => LockfileResolver::LockedGem}]
|
|
49
|
+
# The lockfile-resolved gem set. Empty hash → no
|
|
50
|
+
# coverage analysis to do.
|
|
51
|
+
# @param default_libraries [Array<String>] gem names rigor
|
|
52
|
+
# auto-loads through `RBS::EnvironmentLoader#add(library:)`.
|
|
53
|
+
# Pass `Rigor::Environment::DEFAULT_LIBRARIES` from callers
|
|
54
|
+
# running in a project context.
|
|
55
|
+
# @param bundle_sig_paths [Array<Pathname, String>] the
|
|
56
|
+
# discovered `<bundle>/.../gems/<name>-<ver>/sig` paths
|
|
57
|
+
# from {BundleSigDiscovery.discover}.
|
|
58
|
+
# @param rbs_collection_paths [Array<Pathname, String>] the
|
|
59
|
+
# discovered `<collection>/<name>/<version>/` paths from
|
|
60
|
+
# {RbsCollectionDiscovery.discover}.
|
|
61
|
+
# @return [Array<Coverage>] one row per locked gem; sorted
|
|
62
|
+
# by gem name for deterministic output.
|
|
63
|
+
def self.classify(locked_gems:, default_libraries:,
|
|
64
|
+
bundle_sig_paths:, rbs_collection_paths:)
|
|
65
|
+
default_set = default_libraries.to_set
|
|
66
|
+
bundle_names = extract_gem_names_from_bundle_paths(bundle_sig_paths)
|
|
67
|
+
collection_names = extract_gem_names_from_collection_paths(rbs_collection_paths)
|
|
68
|
+
|
|
69
|
+
locked_gems.each_value.map do |locked|
|
|
70
|
+
name = locked.name
|
|
71
|
+
source = if default_set.include?(name)
|
|
72
|
+
:default_library
|
|
73
|
+
elsif VENDORED_GEM_NAMES.include?(name)
|
|
74
|
+
:vendored_gem_sig
|
|
75
|
+
elsif bundle_names.include?(name)
|
|
76
|
+
:bundle_sig
|
|
77
|
+
elsif collection_names.include?(name)
|
|
78
|
+
:rbs_collection
|
|
79
|
+
else
|
|
80
|
+
:missing
|
|
81
|
+
end
|
|
82
|
+
Coverage.new(gem_name: name, version: locked.version, source: source)
|
|
83
|
+
end.sort_by(&:gem_name)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Convenience accessor for the run-start diagnostic.
|
|
87
|
+
# Filters {classify} down to `:missing` rows.
|
|
88
|
+
def self.missing(coverage_rows)
|
|
89
|
+
coverage_rows.select { |row| row.source == :missing }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def self.extract_gem_names_from_bundle_paths(paths)
|
|
93
|
+
paths.each_with_object(Set.new) do |path, set|
|
|
94
|
+
pathname = path.is_a?(Pathname) ? path : Pathname.new(path)
|
|
95
|
+
set << BundleSigDiscovery.gem_name_from_sig_path(pathname)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
private_class_method :extract_gem_names_from_bundle_paths
|
|
99
|
+
|
|
100
|
+
def self.extract_gem_names_from_collection_paths(paths)
|
|
101
|
+
# `RbsCollectionDiscovery.discover` returns
|
|
102
|
+
# `<collection_root>/<name>/<version>/` so the parent
|
|
103
|
+
# basename is the gem name.
|
|
104
|
+
paths.each_with_object(Set.new) do |path, set|
|
|
105
|
+
pathname = path.is_a?(Pathname) ? path : Pathname.new(path)
|
|
106
|
+
set << pathname.parent.basename.to_s
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
private_class_method :extract_gem_names_from_collection_paths
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|