diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f1f037e..b49be21 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -279,6 +279,8 @@ jobs: name: RAG fixture on Ruby 4.0 runs-on: ubuntu-latest timeout-minutes: 10 + env: + ARCHUNIT_RUBY_PATH: ../ArchUnitRuby steps: - name: Check out ArchUnitRuby @@ -299,8 +301,7 @@ jobs: bundler-cache: true working-directory: ArchUnitRuby-TestRepo-RAG env: - # The fixture locks an older version of the adjacent, checked-out path gem. - # Refresh that entry for this revision while retaining its locked dependencies. + # Switch the fixture's locked release to the adjacent revision in this disposable job. BUNDLE_FROZEN: "false" - name: Verify the fixture loads this checkout diff --git a/.yardopts b/.yardopts index d8b44ef..449e36c 100644 --- a/.yardopts +++ b/.yardopts @@ -6,6 +6,7 @@ --no-private --no-stats --asset docs-assets/custom.css:css/archunit.css +--asset docs-assets/custom.js:js/archunit.js lib/**/*.rb - README.md diff --git a/API.md b/API.md index e2c931b..bc33ec6 100644 --- a/API.md +++ b/API.md @@ -359,6 +359,10 @@ observed files matching multiple layers with UserError; empty layers use the nor EmptyTestViolation defense unless explicitly permitted. CheckOptions controls analysis, exclusions, cache and logging as usual; source parse errors remain fatal. `.because` and framework matchers work normally. Rails conventions do not boot Rails or automatically turn on expanded/helper modes. +For an autoloaded Rails app, pass +`CheckOptions.new(dependency_analysis: :imports_and_constants)` to `check` or `assert_passes` +(CLI: `--dependency-analysis imports_and_constants`). Otherwise, a check can pass without seeing +references between mapped layers that have no imports. See [choosing a mode](README.md#analysis). These presets are not complete architecture/cycle/protocol verification; compose more rules or build arbitrary policies with `project_layers`. diff --git a/GUIDE.md b/GUIDE.md index 07f7f8a..770901e 100644 --- a/GUIDE.md +++ b/GUIDE.md @@ -503,8 +503,17 @@ puts ArchUnit.project_slices.defined_by('app/(**)/').to_plantuml(options) values = ArchUnit.metrics.distance.instability.measure(options) ~~~ -The default remains `dependency_analysis: :imports`. Expanded mode indexes declarations from the -same included source files, then resolves references to definition files. Files remain graph nodes. +The default remains `dependency_analysis: :imports` to preserve the meaning of existing rules and +keep cold checks focused on explicit imports. Start there for gems and apps that declare their +dependencies with `require` or `require_relative`. Start with `:imports_and_constants` for +Rails/Zeitwerk or other autoloaded applications: a green imports-only check may simply mean the +dependency was not represented in the import graph. `rails_imports: true` is a separate setting for +legacy `require_dependency` calls, not a substitute for constant analysis. + +Expanded mode indexes declarations from the same included source files, then resolves references +to definition files. This adds cold-check time and memory, especially in larger projects; later +checks with the same extraction options can reuse the cached graph. Measure on your own code and +verify a known violating reference before enabling a rule in CI. Files remain graph nodes. It supports lexical nesting, compact namespaces, root-qualified names, and explicit superclass, `include`, `prepend`, and `extend` references. Nested `module A; module B` includes `A` in lexical lookup; top-level `module A::B` does not. A nearer namespace shadows a root namespace even when its @@ -698,6 +707,16 @@ patterns can be overridden with `PresetOptions.folders` (String or Array of sour Unknown/duplicate keys and empty patterns are errors. For arbitrary policies use `project_layers`. The Rails preset is opinionated and expects a services layer; it is not full Rails/DDD verification and does not automatically enable `rails_imports` or constant analysis. +For a Zeitwerk app, pass expanded analysis at check time: + +~~~ruby +rule = ArchUnit.architecture_preset(:rails) +options = ArchUnit::CheckOptions.new(dependency_analysis: :imports_and_constants) +ArchUnit.assert_passes(rule, options) +~~~ + +For a CLI-configured preset use `bundle exec archunit check --dependency-analysis imports_and_constants`. +Neither option boots Rails or reproduces its runtime autoloading. In trusted CLI configuration use `rule('Core independent') { architecture_preset(:hexagonal) }`; the helper defaults to the CLI's selected project root. Public `ArchUnit.architecture_preset` uses diff --git a/README.md b/README.md index 744de7e..4ec9fcc 100644 --- a/README.md +++ b/README.md @@ -183,10 +183,17 @@ report queries, and complete examples. ## Choosing the analysis mode -The default `:imports` mode recognizes supported static `require`, `require_relative`, -`autoload`, and `load` calls. It does not infer the runtime behavior of Ruby. +The default `:imports` mode checks supported static `require`, `require_relative`, +`autoload`, and `load` calls. It keeps existing rules compatible and avoids the cost and +uncertainty of inferring dependencies from constant names. -For autoloaded code, explicitly add the supported static constant-reference model: +| Your project | Start with | Why | +| --- | --- | --- | +| Rails/Zeitwerk or another autoloaded app | `:imports_and_constants` | Cross-layer constant uses often have no explicit import. | +| Gem or app with explicit imports | `:imports` | Checks the declared dependencies without building a constant index. | + +The Rails preset defines folder boundaries; it does **not** switch analysis modes. In imports-only +mode, a green check can miss an autoloaded dependency. Opt in when checking autoloaded code: ~~~ruby @@ -196,8 +203,10 @@ options = ArchUnit::CheckOptions.new(dependency_analysis: :imports_and_constants violations = rule.check(options) ~~~ -Expanded analysis maps known project constants to definition files and retains evidence. -It is still bounded: ambiguous, inherited, or dynamic lookup can remain unresolved. +Expanded analysis builds a project-wide constant index and resolves references on a cold check. +Expect extra time and memory, especially on large projects; repeated checks can reuse the cached +graph. A green result still is not proof of runtime behavior: ambiguous, inherited, and dynamic +lookups can remain unresolved. Verify your rule against a known violation before relying on it in CI. [Resolution details](GUIDE.md#analysis) · [Options and caching](API.md#check-options) diff --git a/docs-assets/custom.css b/docs-assets/custom.css index 8171127..8f2b13f 100644 --- a/docs-assets/custom.css +++ b/docs-assets/custom.css @@ -60,7 +60,9 @@ a:hover, a:focus-visible, button:focus-visible, -input:focus-visible { +input:focus-visible, +summary:focus-visible, +pre:focus-visible { outline: 3px solid var(--au-gold); outline-offset: 3px; } @@ -70,6 +72,7 @@ input:focus-visible { radial-gradient(circle at 78% -10%, rgb(228 73 63 / 14%), transparent 34rem), var(--au-bg); color: var(--au-text); + min-width: 0; padding: 0 3rem 4rem; } @@ -391,6 +394,7 @@ pre, font-family: var(--au-mono) !important; line-height: 1.65; margin: 1.5rem 0; + max-width: 100%; overflow-x: auto; padding: 1.35rem !important; } @@ -551,6 +555,10 @@ h2 small a:hover, text-transform: uppercase; } +#toc > summary { + cursor: pointer; +} + #toc li { line-height: 1.5; } @@ -678,6 +686,10 @@ pre.code .rubyid_include { position: static; width: 100%; } + + #content { + overflow-wrap: anywhere; + } } @media (max-width: 620px) { @@ -702,7 +714,6 @@ pre.code .rubyid_include { pre { border-radius: 8px !important; font-size: 0.78rem; - margin-inline: -0.5rem; padding: 1rem !important; } } diff --git a/docs-assets/custom.js b/docs-assets/custom.js new file mode 100644 index 0000000..813a9f9 --- /dev/null +++ b/docs-assets/custom.js @@ -0,0 +1,31 @@ +(() => { + // YARD also loads pages in a hidden iframe before swapping their content into #main. + if (window !== window.top) return; + const main = document.getElementById("main"); + if (!main) return; + + const narrow = window.matchMedia("(max-width: 920px)"); + function enhanceContents() { + const toc = main.querySelector("div#toc"); + const list = toc?.querySelector("ol.top"); + if (!list) return; + + const disclosure = document.createElement("details"); + disclosure.id = toc.id; + disclosure.open = !narrow.matches; + + const summary = document.createElement("summary"); + summary.className = "title"; + summary.textContent = "Table of Contents"; + disclosure.append(summary, list); + toc.replaceWith(disclosure); + } + + // Keep working when YARD replaces #main's contents during reference navigation. + new MutationObserver(enhanceContents).observe(main, { childList: true, subtree: true }); + enhanceContents(); + narrow.addEventListener("change", () => { + const toc = main.querySelector("details#toc"); + if (toc) toc.open = !narrow.matches; + }); +})(); diff --git a/scripts/check_docs.rb b/scripts/check_docs.rb index c7ff0ea..6ec22ed 100644 --- a/scripts/check_docs.rb +++ b/scripts/check_docs.rb @@ -7,8 +7,8 @@ ROOT = Pathname.new(__dir__).join('..').expand_path OUTPUT = ROOT.join('docs') REQUIRED_FILES = %w[ - index.html file.API.html file.GUIDE.html class_list.html css/archunit.css assets/logo-rounded.png - assets/social-preview.png .nojekyll robots.txt sitemap.xml + index.html file.API.html file.GUIDE.html _index.html class_list.html css/archunit.css + js/archunit.js assets/logo-rounded.png assets/social-preview.png .nojekyll robots.txt sitemap.xml ].freeze def internal_target(page, href) @@ -20,6 +20,15 @@ def internal_target(page, href) page.dirname.join(URI::DEFAULT_PARSER.unescape(relative)).cleanpath end +def check_reference_navigation(page, content) + return unless content.include?('class="archunit-topbar"') + + href = content[%r{Reference}, 1] + return if href && internal_target(page, href) == OUTPUT.join('_index.html') + + raise "Reference must link to the full API index from #{page.relative_path_from(OUTPUT)}" +end + missing = REQUIRED_FILES.reject { |relative| OUTPUT.join(relative).exist? } raise "Documentation build is missing: #{missing.join(', ')}" unless missing.empty? @@ -29,6 +38,8 @@ def internal_target(page, href) unless content.include?('data-archunit-theme') raise "Custom theme is missing from #{page.relative_path_from(OUTPUT)}" end + + check_reference_navigation(page, content) if page.basename.to_s.match?(/\A(?:class|method|file)_list\.html\z/) && !content.include?('
') raise "Navigation theme hook is missing from #{page.relative_path_from(OUTPUT)}" @@ -40,6 +51,12 @@ def internal_target(page, href) end end +reference = OUTPUT.join('_index.html').binread.force_encoding(Encoding::UTF_8) +unless reference.include?('class="archunit-topbar"') && + reference.include?('href="ArchUnit.html"') && reference.include?('href="ArchUnit/CLI.html"') + raise 'The full API index must keep site navigation and link to ArchUnit and CLI' +end + raise "Broken documentation links:\n#{broken.join("\n")}" unless broken.empty? fragments = pages.flat_map { |page| ArchUnitDocumentationLinks.rendered_errors(page) } diff --git a/scripts/prepare_docs.rb b/scripts/prepare_docs.rb index a6c108b..d567997 100644 --- a/scripts/prepare_docs.rb +++ b/scripts/prepare_docs.rb @@ -6,6 +6,7 @@ ROOT = Pathname.new(__dir__).join('..').expand_path OUTPUT = ROOT.join('docs') STYLESHEET = OUTPUT.join('css', 'archunit.css') +THEME_SCRIPT = OUTPUT.join('js', 'archunit.js') LOGO = OUTPUT.join('assets', 'logo-rounded.png') SOCIAL_PREVIEW = OUTPUT.join('assets', 'social-preview.png') SITE_URL = 'https://dragonsec-ai.github.io/ArchUnitRuby/' @@ -32,7 +33,6 @@ HTML PAGE_HEAD = <<~HTML - @@ -43,7 +43,9 @@ + HTML +VIEWPORT = %(\n) def relative_href(target, page) target.relative_path_from(page.dirname).to_s.tr('\\', '/') @@ -53,15 +55,17 @@ def navigation(page) guide = relative_href(OUTPUT.join('index.html'), page) api_guide = relative_href(OUTPUT.join('file.API.html'), page) user_guide = relative_href(OUTPUT.join('file.GUIDE.html'), page) - reference = relative_href(OUTPUT.join('class_list.html'), page) + reference = relative_href(OUTPUT.join('_index.html'), page) logo = relative_href(LOGO, page) format(NAVIGATION, guide:, user_guide:, api_guide:, reference:, logo:) end -def page_head(page) +def page_head(page, content) stylesheet = relative_href(STYLESHEET, page) + theme_script = relative_href(THEME_SCRIPT, page) logo = relative_href(LOGO, page) - format(PAGE_HEAD, site_url: SITE_URL, logo:, stylesheet:) + viewport = content.include?('name="viewport"') ? '' : VIEWPORT + viewport + format(PAGE_HEAD, site_url: SITE_URL, logo:, stylesheet:, theme_script:) end def rewrite_source_links(content, page) @@ -85,7 +89,7 @@ def prepare_page(path) if page.basename.to_s.match?(/\A(?:class|method|file)_list\.html\z/) content.sub!('', '') end - content.sub!('', "#{page_head(page)}") + content.sub!('', "#{page_head(page, content)}") content.sub!(/(