Files
rustfs/scripts/check_layer_dependencies.sh
T
GatewayJ 0b7988c37d feat: add S3 Vectors service integration (#9476)
* feat(vector): integrate S3 Vectors catalog and APIs

Add scoped Vector resources, managed Lance storage, IAM checks, and
Gateway REST JSON operations with compatibility and recovery tests.

* fix(vector): adapt owned requests and preserve native quota errors

* test(ci): exercise Vector KMS archive consumers

* feat(vector): add maintenance and recovery workflows

* test(multipart): assert typed quota refusal causes

Verify the retained quota cause and its current and limit values alongside the existing refusal-order assertions. Remove the retired authentication compatibility entry after the main rebase.

* fix(vector): move maintenance dispatch guard ownership

Retain the namespace guard in the returned maintenance owner. Synchronize Gateway dependencies for corpus recording and format the existing audit test in its external module.

* feat(vector): add shared pagination sessions

* fix(vector): bound managed request and catalog futures

* test(vector): adapt managed fixtures to Gateway request envelopes

* test(vector): verify maintenance across uniform deployments

* test: simplify SSE-C counting and Catalog fixture ownership

* fix(test): propagate owned device validation errors

* fix(auth): bound early authorization futures

* feat(vector): complete resource pagination and bucket retirement

* fix(vector): harden bucket lifecycle, retirement, authz, and layering

Preserve continuous bucket incarnation fencing across Vector bucket Catalog parent initialization, map deleted-bucket races during DeleteVectorBucket to NotFoundException, and re-evaluate IAM, resource policies, and ABAC bucket tags inside the storage bucket lock across all five Vector bucket mutations.

Scale used-bucket Catalog retirement proof, plan, and execution linearly across multi-resource multi-page histories, sweep uncommitted pre-authority page artifacts before rewriting authority records, enforce manifest completeness and immutable delete-intent source-copy identity, and allow same-name recreation over v4 deleted Vector tombstones.

Make ListVectorBuckets and ListIndexes resilient to concurrent deletions with single-pass bucket authorization and bounded sparse-prefix scan budgets, preserve literal plus characters when decoding UntagResource tagKeys query parameters to match Gateway SigV4 canonicalization, and normalize operation-scoped Kms*Exception codes across all 21 S3 Vectors APIs.

Extract ManagedLanceObjects into app::vector_usecase::managed_objects, inject background node admission into Vector maintenance startup to eliminate the app-composition cycle, and widen scripts/check_layer_dependencies.sh to detect inline reverse-layer crate paths while recording the 12 pre-existing non-Vector inline edges from main in scripts/layer-dependency-baseline.txt (backlog#1834) with zero Vector exemptions.

* chore(vector): remove Batch 1 dead code and test debug residue

* chore(vector): remove Batch 2 dead test code and gate test-only APIs

* refactor(vector,iam): simplify dead branches and pass-through wrappers

* fix(object,test): bound PutObject futures and cover reconciled routes

* feat(vector,object,connect): activate S3 Vectors and fix CI regressions

* test(object,vector): fix copy future size check and vector dispatch test
2026-11-29 18:07:23 +08:00

891 lines
28 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BASELINE_FILE="${ROOT_DIR}/scripts/layer-dependency-baseline.txt"
MODE="check"
if [[ "${1:-}" == "--update-baseline" ]]; then
MODE="update"
fi
classify_source_layer() {
local file="$1"
if [[ "$file" == rustfs/src/server/* ]] ||
[[ "$file" == rustfs/src/startup_*.rs ]] ||
[[ "$file" == rustfs/src/init.rs ]] ||
[[ "$file" == rustfs/src/main.rs ]] ||
[[ "$file" == rustfs/src/lib.rs ]] ||
[[ "$file" == rustfs/src/embedded.rs ]]; then
printf 'composition'
elif [[ "$file" == rustfs/src/app/* ]]; then
printf 'app'
elif [[ "$file" == rustfs/src/protocols/client.rs ]] || [[ "$file" == rustfs/src/protocols/client/* ]]; then
printf 'interface'
elif [[ "$file" == rustfs/src/admin/* ]] || [[ "$file" == rustfs/src/storage/ecfs.rs ]] ||
[[ "$file" == rustfs/src/storage/legacy_edge/* ]] || [[ "$file" == rustfs/src/storage/s3_api/* ]] ||
[[ "$file" == rustfs/src/storage/s3_compat/* ]]; then
printf 'interface'
elif [[ "$file" == rustfs/src/* ]]; then
printf 'infra'
else
printf 'unknown'
fi
}
classify_target_layer() {
local path="$1"
local root="${path%%::*}"
local storage_path
case "$root" in
init | main | lib | embedded | startup_*)
printf 'composition'
;;
server)
# Server files are composition roots when they import lower layers, but
# their exported HTTP contracts belong to the interface boundary.
printf 'interface'
;;
protocols)
case "${path#protocols::}" in
client | client::* | ProtocolStorageClient | ProtocolStorageClient::*) printf 'interface' ;;
*) printf 'infra' ;;
esac
;;
app)
printf 'app'
;;
admin)
printf 'interface'
;;
storage)
storage_path="${path#storage::}"
if [[ "$storage_path" == "ecfs" ]] || [[ "$storage_path" == ecfs::* ]] ||
[[ "$storage_path" == "legacy_edge" ]] || [[ "$storage_path" == legacy_edge::* ]] ||
[[ "$storage_path" == "s3_api" ]] || [[ "$storage_path" == s3_api::* ]] ||
[[ "$storage_path" == "s3_compat" ]] || [[ "$storage_path" == s3_compat::* ]]; then
printf 'interface'
else
printf 'infra'
fi
;;
*)
printf 'infra'
;;
esac
}
layer_rank() {
case "$1" in
composition)
printf '4'
;;
interface)
printf '3'
;;
app)
printf '2'
;;
infra)
printf '1'
;;
*)
printf '0'
;;
esac
}
is_reverse_dependency() {
local source_rank target_rank
source_rank="$(layer_rank "$1")"
target_rank="$(layer_rank "$2")"
(( source_rank < target_rank ))
}
assert_dependency_direction() {
local expected="$1"
local source="$2"
local target="$3"
local actual='allowed'
if is_reverse_dependency "$source" "$target"; then
actual='reverse'
fi
if [[ "$actual" != "$expected" ]]; then
printf 'Layer dependency guard self-test failed: %s -> %s (expected %s, got %s)\n' \
"$source" "$target" "$expected" "$actual" >&2
exit 1
fi
}
run_layer_model_self_tests() {
local server_source app_source storage_source server_target admin_target app_target storage_target
local s3_compat_source s3_compat_target
server_source="$(classify_source_layer rustfs/src/server/http.rs)"
app_source="$(classify_source_layer rustfs/src/app/bucket_usecase.rs)"
storage_source="$(classify_source_layer rustfs/src/storage/rpc/node_service.rs)"
server_target="$(classify_target_layer server::http)"
admin_target="$(classify_target_layer admin::router)"
app_target="$(classify_target_layer app::bucket_usecase)"
storage_target="$(classify_target_layer storage::rpc)"
# The s3s edge adapters convert wire requests into app types, as ecfs.rs does.
s3_compat_source="$(classify_source_layer rustfs/src/storage/s3_compat/envelope.rs)"
s3_compat_target="$(classify_target_layer storage::s3_compat::envelope)"
assert_dependency_direction 'allowed' "$server_source" "$admin_target"
assert_dependency_direction 'allowed' "$server_source" "$app_target"
assert_dependency_direction 'allowed' "$server_source" "$storage_target"
assert_dependency_direction 'reverse' "$app_source" "$server_target"
assert_dependency_direction 'reverse' "$storage_source" "$server_target"
assert_dependency_direction 'reverse' "$app_source" "$admin_target"
assert_dependency_direction 'reverse' "$storage_source" "$admin_target"
assert_dependency_direction 'allowed' "$s3_compat_source" "$app_target"
assert_dependency_direction 'reverse' "$app_source" "$s3_compat_target"
assert_dependency_direction 'reverse' "$storage_source" "$s3_compat_target"
# A mechanical split moves ecfs.rs code into storage/legacy_edge/ and the
# usecase files' code into same-named directories; each keeps its layer.
assert_source_layer 'interface' rustfs/src/storage/ecfs.rs
assert_source_layer 'interface' rustfs/src/storage/legacy_edge/bucket_core.rs
assert_source_layer 'interface' rustfs/src/storage/legacy_edge/tests/object_meta.rs
assert_source_layer 'infra' rustfs/src/storage/legacy_edge.rs
assert_source_layer 'infra' rustfs/src/storage/legacy_edge_helper.rs
assert_source_layer 'interface' rustfs/src/protocols/client.rs
assert_source_layer 'interface' rustfs/src/protocols/client/owned.rs
assert_source_layer 'infra' rustfs/src/protocols/client_helper.rs
assert_target_layer 'interface' protocols::client
assert_target_layer 'interface' protocols::client::ProtocolStorageClient
assert_target_layer 'interface' protocols::ProtocolStorageClient
assert_target_layer 'infra' protocols::client_helper
assert_dependency_direction 'allowed' "$(classify_source_layer rustfs/src/protocols/client/owned.rs)" "$app_target"
assert_dependency_direction 'reverse' "$app_source" "$(classify_target_layer protocols::client::ProtocolStorageClient)"
assert_dependency_direction 'reverse' "$app_source" "$(classify_target_layer protocols::ProtocolStorageClient)"
assert_source_layer 'app' rustfs/src/app/bucket_usecase/cors.rs
assert_source_layer 'app' rustfs/src/app/multipart_usecase/complete.rs
assert_target_layer 'interface' storage::legacy_edge
assert_target_layer 'interface' storage::legacy_edge::object_meta
assert_target_layer 'infra' storage::legacy_edge_helper
assert_dependency_direction 'allowed' "$(classify_source_layer rustfs/src/storage/legacy_edge/bucket_core.rs)" "$app_target"
assert_dependency_direction 'reverse' "$app_source" "$(classify_target_layer storage::legacy_edge::bucket_core)"
assert_dependency_direction 'reverse' "$storage_source" "$(classify_target_layer storage::legacy_edge)"
assert_dedicated_test_file 'yes' rustfs/src/app/lifecycle_transition_api_test.rs
assert_dedicated_test_file 'yes' rustfs/src/app/lifecycle_transition_api_test/put.rs
assert_dedicated_test_file 'yes' rustfs/src/app/vector_access_tests.rs
assert_dedicated_test_file 'yes' rustfs/src/app/vector_usecase/tests.rs
assert_dedicated_test_file 'no' rustfs/src/app/lifecycle_transition_api_test_support.rs
assert_dedicated_test_file 'no' rustfs/src/app/lifecycle_transition_api.rs
assert_dedicated_test_file 'no' rustfs/src/app/bucket_usecase/cors.rs
assert_dedicated_test_file 'no' rustfs/src/storage/legacy_edge/bucket_core.rs
run_inline_crate_path_self_tests
}
assert_source_layer() {
local expected="$1" file="$2" actual
actual="$(classify_source_layer "$file")"
if [[ "$actual" != "$expected" ]]; then
printf 'Layer dependency guard self-test failed: %s classified as %s (expected %s)\n' "$file" "$actual" "$expected" >&2
exit 1
fi
}
assert_target_layer() {
local expected="$1" path="$2" actual
actual="$(classify_target_layer "$path")"
if [[ "$actual" != "$expected" ]]; then
printf 'Layer dependency guard self-test failed: crate::%s classified as %s (expected %s)\n' "$path" "$actual" "$expected" >&2
exit 1
fi
}
assert_dedicated_test_file() {
local expected="$1" file="$2" actual='no'
if is_dedicated_test_file "$file"; then
actual='yes'
fi
if [[ "$actual" != "$expected" ]]; then
printf 'Layer dependency guard self-test failed: %s dedicated test file = %s (expected %s)\n' "$file" "$actual" "$expected" >&2
exit 1
fi
}
normalize_import_group_item() {
local prefix="$1"
local item="$2"
local path nested_prefix nested_items
item="$(sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//; s/[[:space:]]+as[[:space:]].*$//' <<<"$item")"
item="$(sed -E 's/[[:space:]]//g' <<<"$item")"
prefix="$(sed -E 's/[[:space:]]//g' <<<"$prefix")"
[[ -z "$item" ]] && return 0
if [[ "$item" == "self" ]]; then
prefix="${prefix%::}"
[[ -n "$prefix" ]] && printf '%s\n' "$prefix"
return 0
fi
if [[ "$item" == *"{"* ]]; then
nested_prefix="${item%%\{*}"
nested_items="${item#*\{}"
nested_items="${nested_items%\}*}"
nested_prefix="${nested_prefix%::}"
if [[ -n "$prefix" ]]; then
nested_prefix="${prefix}::${nested_prefix}"
fi
normalize_import_group "$nested_prefix" "$nested_items"
return 0
fi
if [[ -n "$prefix" ]]; then
path="${prefix}::${item}"
else
path="$item"
fi
if [[ "$path" == *"::*" ]]; then
path="${path%::*}"
fi
while [[ "$path" == *"::" ]]; do
path="${path%::}"
done
[[ -n "$path" ]] && printf '%s\n' "$path"
}
normalize_import_group() {
local prefix="$1"
local group="$2"
local item="" char depth=0 i
for ((i = 0; i < ${#group}; i++)); do
char="${group:i:1}"
case "$char" in
"{")
depth=$((depth + 1))
;;
"}")
depth=$((depth - 1))
;;
",")
if (( depth == 0 )); then
normalize_import_group_item "$prefix" "$item"
item=""
continue
fi
;;
esac
item+="$char"
done
normalize_import_group_item "$prefix" "$item"
}
normalize_import_path() {
local text="$1"
local path
path="$(sed -E 's/.*use[[:space:]]+crate::([^;]+);?.*/\1/' <<<"$text")"
if [[ -z "$path" ]] || [[ "$path" == "$text" ]]; then
return 0
fi
path="$(sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//' <<<"$path")"
if [[ "$path" == *"{"* ]]; then
local prefix group
prefix=""
group="$path"
if [[ "$path" != \{* ]]; then
prefix="${path%%\{*}"
group="${path#*\{}"
else
group="${path#\{}"
fi
group="${group%\}*}"
prefix="${prefix%::}"
normalize_import_group "$prefix" "$group"
return 0
fi
path="$(sed -E 's/[[:space:]]+as[[:space:]].*$//' <<<"$path")"
path="$(sed -E 's/[[:space:]]//g' <<<"$path")"
if [[ "$path" == *"::*" ]]; then
path="${path%::*}"
fi
while [[ "$path" == *"::" ]]; do
path="${path%::}"
done
printf '%s\n' "$path"
}
# The guard is file-scoped: dedicated test modules are excluded, while inline
# #[cfg(test)] imports remain subject to the source file's layer. Production
# inline `crate::<module>::...` path scanning strips comments, string literals,
# and unconditional `#[cfg(test)]` items. The lifecycle_transition_api_test/
# directory is where a mechanical split of lifecycle_transition_api_test.rs
# moves its tests (rustfs/backlog#2765).
is_dedicated_test_file() {
local file="$1"
[[ "$file" == *_test.rs ]] || [[ "$file" == *_tests.rs ]] ||
[[ "$file" == */tests.rs ]] || [[ "$file" == */tests/* ]] ||
[[ "$file" == rustfs/src/app/lifecycle_transition_api_test/* ]]
}
list_rust_sources() {
if command -v rg >/dev/null 2>&1; then
(cd "$ROOT_DIR" && rg --files -g '*.rs' rustfs/src | LC_ALL=C sort)
else
(cd "$ROOT_DIR" && find rustfs/src -type f -name '*.rs' | sed 's#^\./##' | LC_ALL=C sort)
fi
}
emit_crate_use_statements() {
local -a files=()
local file
while IFS= read -r file; do
if is_dedicated_test_file "$file"; then
continue
fi
files+=("$file")
done < <(list_rust_sources)
if (( ${#files[@]} > 0 )); then
(cd "$ROOT_DIR" && perl -0777 -ne '
while (/\buse\s+crate::.*?;/sg) {
my $statement = $&;
my $line = substr($_, 0, $-[0]) =~ tr/\n//;
$line += 1;
$statement =~ s/\s+/ /g;
print "$ARGV:$line:$statement\n";
}
' "${files[@]}")
fi
}
extract_inline_crate_paths() {
perl - "$@" <<'PERL'
use strict;
use warnings;
sub is_test_cfg {
my ($cond) = @_;
$cond =~ s/^\s+|\s+$//g;
return 1 if $cond eq 'test';
if ($cond =~ /^all\s*\(([\s\S]*)\)$/) {
my $inner = $1;
my ($depth, $part) = (0, '');
my @parts;
for my $ch (split //, $inner) {
if ($ch eq '(') {
$depth++;
$part .= $ch;
} elsif ($ch eq ')') {
$depth--;
$part .= $ch;
} elsif ($ch eq ',' && $depth == 0) {
$part =~ s/^\s+|\s+$//g;
push @parts, $part;
$part = '';
} else {
$part .= $ch;
}
}
$part =~ s/^\s+|\s+$//g;
push @parts, $part if length $part;
return 1 if grep { $_ eq 'test' } @parts;
}
return 0;
}
sub strip_comments_and_strings {
my ($src) = @_;
$src =~ s{
//[^\n]*
| /\*[\s\S]*?\*/
| (?:br|rb|cr|rc|r)(\#*)"[\s\S]*?"\1
| (?:b|c)?"(?:\\[\s\S]|[^"\\])*"
| ('[A-Za-z_][A-Za-z0-9_]*)(?!')
| b?'(?:\\[^'\n]+|[^'\\\n])'
}{
if (defined $2) {
$2;
} else {
my $matched = $&;
$matched =~ s/[^\n]/ /g;
$matched;
}
}gex;
return $src;
}
sub strip_cfg_test_items {
my ($src) = @_;
my $len = length($src);
my $pos = 0;
my $out = '';
while ($pos < $len) {
pos($src) = $pos;
if ($src !~ /\#\s*\[\s*cfg\s*\(([^)\]]*(?:\([^)\]]*\)[^)\]]*)*)\)\s*\]/g) {
$out .= substr($src, $pos);
last;
}
my ($m_start, $m_end, $cond) = ($-[0], $+[0], $1);
if (!is_test_cfg($cond)) {
$out .= substr($src, $pos, $m_end - $pos);
$pos = $m_end;
next;
}
$out .= substr($src, $pos, $m_start - $pos);
my $attr = substr($src, $m_start, $m_end - $m_start);
$attr =~ s/[^\n]/ /g;
$out .= $attr;
my $j = $m_end;
while ($j < $len) {
while ($j < $len && substr($src, $j, 1) =~ /\s/) {
my $ws = substr($src, $j, 1);
$out .= ($ws eq "\n" ? "\n" : ' ');
$j++;
}
if ($j + 1 < $len && substr($src, $j, 2) eq '#[') {
my $b_depth = 0;
while ($j < $len) {
my $c = substr($src, $j, 1);
$out .= ($c eq "\n" ? "\n" : ' ');
$j++;
if ($c eq '[') {
$b_depth++;
} elsif ($c eq ']') {
$b_depth--;
last if $b_depth <= 0;
}
}
next;
}
last;
}
my ($p_d, $b_d, $c_d, $saw_brace) = (0, 0, 0, 0);
while ($j < $len) {
my $c = substr($src, $j, 1);
$out .= ($c eq "\n" ? "\n" : ' ');
$j++;
if ($c eq '(') {
$p_d++;
} elsif ($c eq ')') {
$p_d-- if $p_d > 0;
} elsif ($c eq '[') {
$b_d++;
} elsif ($c eq ']') {
$b_d-- if $b_d > 0;
} elsif ($c eq '{') {
$c_d++;
$saw_brace = 1;
} elsif ($c eq '}') {
$c_d--;
last if $saw_brace && $c_d <= 0 && $p_d == 0 && $b_d == 0;
} elsif (($c eq ';' || $c eq ',') && !$saw_brace && $c_d == 0 && $p_d == 0 && $b_d == 0) {
last;
}
}
$pos = $j;
}
return $out;
}
for my $file (@ARGV) {
open my $fh, '<', $file or die "failed to read $file: $!";
my $raw = do { local $/; <$fh> };
close $fh;
my $clean = strip_comments_and_strings($raw);
my $prod = strip_cfg_test_items($clean);
$prod =~ s{\buse\s+crate::[^;]*;}{
my $u = $&;
$u =~ s/[^\n]/ /g;
$u;
}sge;
while ($prod =~ /(?<![A-Za-z0-9_:])crate::([A-Za-z_][A-Za-z0-9_]*(?:::[A-Za-z_][A-Za-z0-9_]*)*)/g) {
my $symbol = $1;
my $line = (substr($prod, 0, $-[0]) =~ tr/\n//) + 1;
print "$file:$line:$symbol\n";
}
}
PERL
}
emit_crate_inline_paths() {
local -a files=()
local file
while IFS= read -r file; do
if is_dedicated_test_file "$file"; then
continue
fi
files+=("$file")
done < <(list_rust_sources)
if (( ${#files[@]} > 0 )); then
(cd "$ROOT_DIR" && extract_inline_crate_paths "${files[@]}")
fi
}
run_inline_crate_path_self_tests() {
local probe_dir probe_file output app_layer target_layer
probe_dir="$(mktemp -d)"
probe_file="${probe_dir}/probe.rs"
cat >"$probe_file" <<'EOF'
use crate::startup_services::ImportedOnly;
// crate::startup_services::CommentIgnored
const LITERAL: &str = "crate::startup_services::StringIgnored";
const RAW_LITERAL: &str = r#"crate::startup_background::RawStringIgnored"#;
struct Holder<'a> {
#[cfg(test)]
test_only_field: &'a crate::startup_services::TestFieldIgnored,
live_ref: &'a crate::startup_services::ManagedLanceObjects,
}
fn call_inline<'a>(_input: &'a str) {
let _ = crate::startup_background::background_node_admission();
let _ = crate::table_catalog::Namespace::parse("indexes");
}
#[cfg(any(target_os = "linux", test))]
fn linux_prod_item() {
let _ = crate::server::http::active_requests();
}
#[cfg(test)]
fn unit_test_helper() {
let _ = crate::startup_services::TestHelperIgnored::run();
}
#[cfg(all(test, unix))]
mod unix_tests {
pub(super) fn helper() {
let _ = crate::startup_background::UnixTestIgnored::run();
}
}
EOF
output="$(extract_inline_crate_paths "$probe_file")"
rm -rf "$probe_dir"
for expected in \
"startup_services::ManagedLanceObjects" \
"startup_background::background_node_admission" \
"table_catalog::Namespace::parse" \
"server::http::active_requests"
do
if ! grep -Fq ":${expected}" <<<"$output"; then
printf 'Layer dependency guard inline self-test failed: missing %s in:\n%s\n' "$expected" "$output" >&2
exit 1
fi
done
for forbidden in \
"ImportedOnly" \
"CommentIgnored" \
"StringIgnored" \
"RawStringIgnored" \
"TestFieldIgnored" \
"TestHelperIgnored" \
"UnixTestIgnored"
do
if grep -Fq "$forbidden" <<<"$output"; then
printf 'Layer dependency guard inline self-test failed: unexpectedly matched %s in:\n%s\n' "$forbidden" "$output" >&2
exit 1
fi
done
app_layer="$(classify_source_layer rustfs/src/app/vector_usecase/dataset.rs)"
target_layer="$(classify_target_layer startup_services::ManagedLanceObjects)"
assert_dependency_direction 'reverse' "$app_layer" "$target_layer"
assert_dependency_direction 'reverse' "$app_layer" "$(classify_target_layer startup_background::background_node_admission)"
assert_dependency_direction 'allowed' "$app_layer" "$(classify_target_layer table_catalog::Namespace::parse)"
}
write_baseline_file() {
local entries="$1"
cat >"$BASELINE_FILE" <<'EOF'
# Layer dependency baseline for the rustfs binary crate.
#
# RATCHET RULE (backlog#1834): this file only shrinks. A PR may delete lines
# (after migrating the violation) via --update-baseline; a PR that ADDS a line
# is baselining a brand-new layering violation and must carry an explicit
# exemption rationale in its description — the baseline is a migration ledger,
# not an amnesty list.
#
# The guard models production imports as:
# composition -> interface -> app -> infra
#
# Canonical dependency entry:
# dep|source_file|source_layer->target_layer|crate::imported_symbol
#
# Canonical conceptual cycle entry:
# cycle|left_layer<->right_layer
EOF
cat "$entries" >>"$BASELINE_FILE"
}
run_layer_model_self_tests
normalize_baseline_file() {
local input="$1"
local output="$2"
local line status first second third
: >"$output"
while IFS= read -r line; do
[[ -z "$line" ]] && continue
[[ "$line" == \#* ]] && continue
IFS='|' read -r status first second third _ <<<"$line"
case "$status" in
dep)
if [[ -n "$first" ]] && [[ -n "$second" ]] && [[ -n "$third" ]]; then
printf 'dep|%s|%s|%s\n' "$first" "$second" "$third" >>"$output"
fi
;;
cycle)
if [[ -n "$first" ]]; then
printf 'cycle|%s\n' "$first" >>"$output"
fi
;;
accepted | todo)
if [[ "$first" == "cycle" ]] && [[ -n "$second" ]]; then
printf 'cycle|%s\n' "$second" >>"$output"
elif [[ -n "$first" ]] && [[ -n "$second" ]] && [[ -n "$third" ]]; then
printf 'dep|%s|%s|%s\n' "$first" "$second" "$third" >>"$output"
fi
;;
esac
done <"$input"
sort -u -o "$output" "$output"
}
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT
# --- Cross-crate Rust source include guard (rustfs/backlog#1884) ---
#
# `use`-based layer checks cannot see `include_str!`/`include!` edges, and a
# crate that includes another crate's `.rs` source couples itself to that
# crate's file layout — a contract crate reverse-including an implementation
# or binary crate was exactly the unguarded edge found in backlog#1884. Rule:
# an `include_str!`/`include!` whose target is a `.rs` file must not resolve
# outside the including crate's own directory. Same-crate source includes
# (self-asserting tripwires) stay allowed; non-`.rs` targets (protos, fixtures)
# and `OUT_DIR` includes of generated code are out of scope.
INCLUDE_GUARD="${TMP_DIR}/include_guard.pl"
cat >"$INCLUDE_GUARD" <<'PERL'
use strict;
use warnings;
use File::Basename qw(dirname);
use File::Find qw(find);
my ($root, @dirs) = @ARGV;
my @files;
find(
sub { push @files, $File::Find::name if -f && /\.rs$/ && $File::Find::name !~ m{/target/} },
map { "$root/$_" } @dirs
);
sub normalize {
my @parts;
for my $part (split m{/+}, $_[0]) {
next if $part eq '' || $part eq '.';
if ($part eq '..') { pop @parts } else { push @parts, $part }
}
return '/' . join('/', @parts);
}
my @violations;
for my $file (sort @files) {
open my $fh, '<', $file or next;
my $src = do { local $/; <$fh> };
close $fh;
my $dir = dirname($file);
my $crate = $dir;
$crate = dirname($crate) while $crate ne $root && $crate ne '/' && !-f "$crate/Cargo.toml";
next if $crate eq $root || $crate eq '/';
while (
$src =~ /include(?:_str)?!\s*\(\s*(?:concat!\s*\(\s*env!\s*\(\s*"CARGO_MANIFEST_DIR"\s*\)\s*,\s*"([^"]+)"|"([^"]+)")/gs
) {
my ($manifest_rel, $file_rel) = ($1, $2);
my $target = defined $manifest_rel ? "$crate$manifest_rel" : "$dir/$file_rel";
next unless $target =~ /\.rs$/;
my $resolved = normalize($target);
my $crate_prefix = normalize($crate) . '/';
if (index($resolved, $crate_prefix) != 0) {
push @violations, "$file includes $resolved outside its crate " . normalize($crate);
}
}
}
print "$_\n" for @violations;
exit(@violations ? 1 : 0);
PERL
# Self-test: a fixture workspace with one escaping file-relative include, one
# escaping CARGO_MANIFEST_DIR include, and several allowed forms.
FIXTURE_ROOT="${TMP_DIR}/include_fixture"
mkdir -p "$FIXTURE_ROOT/crates/a/src" "$FIXTURE_ROOT/crates/a/tests" "$FIXTURE_ROOT/crates/b/src"
touch "$FIXTURE_ROOT/crates/a/Cargo.toml" "$FIXTURE_ROOT/crates/b/Cargo.toml"
cat >"$FIXTURE_ROOT/crates/a/src/lib.rs" <<'EOF'
const ESCAPE_RELATIVE: &str = include_str!("../../b/src/lib.rs");
const ESCAPE_MANIFEST: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../b/src/other.rs"));
const SAME_CRATE: &str = include_str!("sibling.rs");
const SAME_CRATE_MANIFEST: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/sibling.rs"));
const NON_RUST: &str = include_str!("../../b/src/schema.proto");
EOF
cat >"$FIXTURE_ROOT/crates/a/tests/contract.rs" <<'EOF'
const OWN_SRC: &str = include_str!("../src/lib.rs");
EOF
INCLUDE_SELF_TEST_OUTPUT="$(perl "$INCLUDE_GUARD" "$FIXTURE_ROOT" crates 2>&1 || true)"
if [[ "$(printf '%s\n' "$INCLUDE_SELF_TEST_OUTPUT" | grep -c 'outside its crate')" != "2" ]] ||
! grep -q 'crates/b/src/lib.rs' <<<"$INCLUDE_SELF_TEST_OUTPUT" ||
! grep -q 'crates/b/src/other.rs' <<<"$INCLUDE_SELF_TEST_OUTPUT"; then
echo "Cross-crate include guard self-test failed; expected exactly the two escaping fixtures to be flagged:" >&2
printf '%s\n' "$INCLUDE_SELF_TEST_OUTPUT" >&2
exit 1
fi
if ! INCLUDE_VIOLATIONS="$(perl "$INCLUDE_GUARD" "$ROOT_DIR" crates rustfs 2>&1)"; then
echo "Cross-crate include guard failed: a Rust source include escapes its crate directory."
echo "Move the assertion into the crate that owns the included file (or assert the compiled artifact) instead of reading another crate's source."
printf '%s\n' "$INCLUDE_VIOLATIONS"
exit 1
fi
VIOLATIONS_RAW="${TMP_DIR}/violations_raw.txt"
EDGES_RAW="${TMP_DIR}/edges_raw.txt"
CURRENT_BASELINE="${TMP_DIR}/current_baseline.txt"
: >"$VIOLATIONS_RAW"
: >"$EDGES_RAW"
while IFS= read -r line; do
file="${line%%:*}"
rest="${line#*:}"
lineno="${rest%%:*}"
text="${rest#*:}"
source_layer="$(classify_source_layer "$file")"
if [[ "$source_layer" == "unknown" ]]; then
continue
fi
while IFS= read -r import_path; do
if [[ -z "$import_path" ]]; then
continue
fi
target_layer="$(classify_target_layer "$import_path")"
if [[ "$target_layer" == "unknown" ]]; then
continue
fi
if [[ "$source_layer" != "$target_layer" ]]; then
printf '%s->%s\n' "$source_layer" "$target_layer" >>"$EDGES_RAW"
fi
if is_reverse_dependency "$source_layer" "$target_layer"; then
printf 'dep|%s|%s->%s|crate::%s\n' "$file" "$source_layer" "$target_layer" "$import_path" >>"$VIOLATIONS_RAW"
fi
done < <(normalize_import_path "$text")
done < <(emit_crate_use_statements)
while IFS= read -r line; do
file="${line%%:*}"
rest="${line#*:}"
lineno="${rest%%:*}"
import_path="${rest#*:}"
[[ -z "$import_path" ]] && continue
source_layer="$(classify_source_layer "$file")"
if [[ "$source_layer" == "unknown" ]]; then
continue
fi
target_layer="$(classify_target_layer "$import_path")"
if [[ "$target_layer" == "unknown" ]]; then
continue
fi
if [[ "$source_layer" != "$target_layer" ]]; then
printf '%s->%s\n' "$source_layer" "$target_layer" >>"$EDGES_RAW"
fi
if is_reverse_dependency "$source_layer" "$target_layer"; then
printf 'dep|%s|%s->%s|crate::%s\n' "$file" "$source_layer" "$target_layer" "$import_path" >>"$VIOLATIONS_RAW"
fi
done < <(emit_crate_inline_paths)
sort -u "$VIOLATIONS_RAW" >"${TMP_DIR}/violations_sorted.txt"
sort -u "$EDGES_RAW" >"${TMP_DIR}/edges_sorted.txt"
while IFS= read -r edge; do
[[ -z "$edge" ]] && continue
left="${edge%%->*}"
right="${edge#*->}"
reverse="${right}->${left}"
if grep -Fxq "$reverse" "${TMP_DIR}/edges_sorted.txt"; then
if [[ "$left" < "$right" ]]; then
printf 'cycle|%s<->%s\n' "$left" "$right"
fi
fi
done <"${TMP_DIR}/edges_sorted.txt" | sort -u >"${TMP_DIR}/cycles_sorted.txt"
cat "${TMP_DIR}/violations_sorted.txt" "${TMP_DIR}/cycles_sorted.txt" | sort -u >"$CURRENT_BASELINE"
if [[ "$MODE" == "update" ]]; then
write_baseline_file "$CURRENT_BASELINE"
echo "Updated baseline: $BASELINE_FILE"
exit 0
fi
if [[ ! -f "$BASELINE_FILE" ]]; then
echo "Baseline file missing: $BASELINE_FILE"
echo "Run: scripts/check_layer_dependencies.sh --update-baseline"
exit 1
fi
normalize_baseline_file "$BASELINE_FILE" "${TMP_DIR}/baseline_sorted.txt"
NEW_ITEMS="${TMP_DIR}/new_items.txt"
comm -13 "${TMP_DIR}/baseline_sorted.txt" "$CURRENT_BASELINE" >"$NEW_ITEMS"
STALE_ITEMS="${TMP_DIR}/stale_items.txt"
comm -23 "${TMP_DIR}/baseline_sorted.txt" "$CURRENT_BASELINE" >"$STALE_ITEMS"
if [[ -s "$NEW_ITEMS" ]]; then
echo "Layer dependency guard failed: new reverse dependencies or cycles detected"
echo "Fix the layering instead of baselining it. The baseline is a shrink-only migration ledger (backlog#1834):"
echo "re-running with --update-baseline to ADD these entries requires an explicit exemption rationale in the PR description."
cat "$NEW_ITEMS"
exit 1
fi
if [[ -s "$STALE_ITEMS" ]]; then
echo "Layer dependency guard failed: stale baseline entries detected"
cat "$STALE_ITEMS"
exit 1
fi
echo "Layer dependency guard passed (no new reverse dependencies/cycles)."