Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(health): classify generator output, native external libraries, minified trees and build logic (#2899)

Raghav Chamadiya5dd4c382f920
diff --git a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
index 140c47bd4..d92f275bb 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/analyzer.py
@@ -36,7 +36,7 @@ from .constants import (
_PURE_WRAPPER_DECORATOR_ATTRS,
_PURE_WRAPPER_DECORATOR_MODULES,
_is_fixture_path,
- never_flag_match,
+ never_flag_path,
)
from .contract_methods import is_contract_method
from .dynamic_markers import (
@@ -1908,7 +1908,7 @@ class DeadCodeAnalyzer:
"""
if path in whitelist:
return True
- if never_flag_match(path):
+ if never_flag_path(path):
return True
# Workspace-driven never-flag — set by language warmups that read
# the build manifest (Gradle non-``main`` source sets, Cargo
diff --git a/packages/core/src/repowise/core/analysis/dead_code/constants.py b/packages/core/src/repowise/core/analysis/dead_code/constants.py
index 44dd2cded..3f769dbe0 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/constants.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/constants.py
@@ -16,6 +16,7 @@ import os
import re
from functools import lru_cache
+from repowise.core.code_origin import is_vendored_or_generated_path
from repowise.core.ingestion.languages.registry import REGISTRY as _LANG_REGISTRY
# Non-code languages (registry passthrough languages plus "unknown").
@@ -1138,6 +1139,15 @@ def never_flag_match(path: str) -> bool:
return False
+def never_flag_path(path: str) -> bool:
+ """The never-flag globs, plus vendored and generated code by path.
+
+ A copied library or a generator's output is not this repository's to
+ delete, whatever its importers look like.
+ """
+ return never_flag_match(path) or is_vendored_or_generated_path(path)
+
+
def _is_fixture_path(path: str) -> bool:
"""Return True if path is under a test fixture / sample data directory."""
path_lower = path.lower().replace("\\", "/")
diff --git a/packages/core/src/repowise/core/analysis/dead_code/file_reachability.py b/packages/core/src/repowise/core/analysis/dead_code/file_reachability.py
index 6ebdb2248..6adcbf31d 100644
--- a/packages/core/src/repowise/core/analysis/dead_code/file_reachability.py
+++ b/packages/core/src/repowise/core/analysis/dead_code/file_reachability.py
@@ -45,7 +45,7 @@ from typing import Any
from repowise.core.entry_candidacy import is_reachability_root
from repowise.core.ids import SYMBOL_SEP, file_path_of, is_external
-from .constants import never_flag_match
+from .constants import never_flag_path
from .cpp_reachability import build_cpp_package_files, is_cpp_file_reachable, is_cpp_path
from .go_reachability import build_go_package_files, is_go_file_reachable
from .jvm_reachability import build_jvm_package_files, is_jvm_file_reachable
@@ -289,7 +289,7 @@ def is_file_reachable(
# routes, migration scripts. Reached from outside the import graph the same
# way an entry point is, so this belongs to the question, not to the
# analyzer that used to own the matcher.
- if never_flag_match(path):
+ if never_flag_path(path):
return True
if path in rescues.whitelist:
return True
diff --git a/packages/core/src/repowise/core/code_origin.py b/packages/core/src/repowise/core/code_origin.py
index d84f85780..8946f357d 100644
--- a/packages/core/src/repowise/core/code_origin.py
+++ b/packages/core/src/repowise/core/code_origin.py
@@ -12,7 +12,9 @@ file has exactly one: ``generated`` beats ``vendored`` beats ``test`` beats
The generated-file banner rule is the one ingestion already applies before it
indexes a file (:func:`is_generated_header`, moved here from the traverser so
-both read one rule). Ingestion also drops ``vendor/``, ``node_modules/`` and
+both read one rule); classification adds a "this class is generated" sentence
+further down the head, which is too weak to drop a file from the graph but
+enough to keep it out of a fix list. Ingestion also drops ``vendor/``, ``node_modules/`` and
``*.min.js`` before analysis ever sees them; they are classified here anyway so
a caller holding a bare path gets the same answer.
"""
@@ -20,7 +22,7 @@ a caller holding a bare path gets the same answer.
from __future__ import annotations
import re
-from functools import lru_cache
+from functools import cache, lru_cache
from pathlib import PurePosixPath
from typing import Literal
@@ -32,6 +34,7 @@ __all__ = [
"CodeOrigin",
"code_origin",
"is_generated_header",
+ "is_vendored_or_generated_path",
]
CodeOrigin = Literal["production", "test", "vendored", "docs_example", "generated", "tooling"]
@@ -67,7 +70,35 @@ _THIS_FILE_GENERATED_RE = re.compile(
_COMMENT_PREFIXES: tuple[str, ...] = ("<!--", "//", "/*", "--", "#", "*", ";")
_DOCSTRING_OPENER_RE = re.compile(r"""^[rRuUbB]?(?:\"\"\"|''')""")
-_GENERATED_DIR_TOKENS = frozenset({"__generated__"})
+# Output directories of code generators: Relay/GraphQL's ``__generated__`` and
+# the Gradle/Maven ``generated-src`` / ``generated-sources`` count anywhere.
+# A bare ``generated/`` counts only as a build output (``src/main/generated``,
+# ``build/generated``, ``target/generated``): ``app/generated/`` can be
+# hand-written code about something generated.
+_GENERATED_DIR_TOKENS = frozenset({"__generated__", "generated-src", "generated-sources"})
+_GENERATED_BUILD_DIR = "generated"
+_BUILD_OUTPUT_PARENTS = frozenset({"main", "build", "target"})
+# Beyond the first two lines, only an explicit sentence in a comment counts:
+# Java generators put "This class is generated." in the class Javadoc, after
+# the license and the imports. The looser markers ("DO NOT EDIT") stay
+# first-lines-only, since a hand-written file can ask readers not to edit a
+# table further down. The sentence is read from source files only, and only
+# up to the first line of code that is not an import, so a template or a
+# generator script that prints the banner into its output is not taken for
+# the output.
+#
+# The sentence must also say it is machine output: generated automatically,
+# by or from something, or not to be edited ("Edit X instead"). "This file is
+# generated when you run make docs" describes a hand-written file.
+_GENERATED_SENTENCE_RE = re.compile(
+ r"\bTHIS (?:CLASS|FILE) (?:IS|WAS) (?:"
+ r"(?:AUTO-?|AUTOMATICALLY )GENERATED\b"
+ r"|GENERATED (?:BY|FROM)\b"
+ r"|GENERATED\b.*(?:\bINSTEAD\b|\bDO NOT (?:HAND[- ])?(?:EDIT|MODIFY)|\bNOT BE (?:EDITED|MODIFIED))"
+ r")"
+)
+_GENERATED_SENTENCE_LINES = 40
+_PREAMBLE_PREFIXES = ("package ", "import ", "from ", "using ")
def _comment_text(line: str, in_docstring: bool) -> str | None:
@@ -112,10 +143,67 @@ def is_generated_header(header: str) -> bool:
return False
-@lru_cache(maxsize=1)
+def _declares_generated(name: str, header: str) -> bool:
+ """Whether a comment in the file's head says the file or class is generated.
+
+ Code analysis only: ingestion's skip rule stays :func:`is_generated_header`,
+ because skipping drops a file from the graph and its callers lose edges.
+ """
+ if is_generated_header(header):
+ return True
+ return name.lower().endswith(_code_suffixes()) and _leading_generated_sentence(header)
+
+
+def _leading_generated_sentence(header: str) -> bool:
+ """The generated sentence in a comment before the first line of real code."""
+ lines = [line.strip() for line in header.splitlines()[:_GENERATED_SENTENCE_LINES]]
+ for i, line in enumerate(lines):
+ text = _comment_text(line, False) if line else ""
+ if text is None:
+ if not line.startswith(_PREAMBLE_PREFIXES):
+ return False
+ elif _GENERATED_SENTENCE_RE.search(text.upper()) and not _above_import(lines, i):
+ return True
+ return False
+
+
+def _above_import(lines: list[str], i: int) -> bool:
+ """A comment right above an import is about that import ("this file is
+ generated at build time" over the import of a build artifact)."""
+ return i + 1 < len(lines) and lines[i + 1].startswith(_PREAMBLE_PREFIXES)
+
+
+# .NET reference assemblies (``src/libraries/<Lib>/ref/<Lib>.cs``) declare the
+# public API with ``throw null`` bodies; the implementation under ``src/``
+# carries the code. Counted as generated because they are consumed the same
+# way: tooling regenerates them and nothing in them is a place to fix code.
+# The path alone is not enough (a ``ref/`` folder can hold anything), so a
+# ``{ throw null; }`` member body or the API-review banner must show in the head.
+_REFERENCE_STUB_RE = re.compile(
+ r"\{\s*throw null;\s*\}|^// Changes to this file must follow the https://aka\.ms/api-review",
+ re.MULTILINE,
+)
+
+
+def _is_reference_stub(name: str, dirs: list[str], header: str) -> bool:
+ return (
+ name.lower().endswith(".cs")
+ and "ref" in dirs
+ and _REFERENCE_STUB_RE.search(header) is not None
+ )
+
+
+# Deferred registry reads: the ingestion package imports the traverser, which
+# imports this module for the banner rule above.
+@cache
+def _code_suffixes() -> tuple[str, ...]:
+ from .ingestion.languages.registry import REGISTRY
+
+ return tuple(sorted(REGISTRY.all_code_extensions()))
+
+
+@cache
def _generated_suffixes() -> tuple[str, ...]:
- # Deferred: the ingestion package imports the traverser, which imports
- # this module for the banner rule above.
from .ingestion.languages.registry import REGISTRY
return tuple(sorted(REGISTRY.generated_suffixes()))
@@ -129,10 +217,11 @@ def _is_generated_name(name: str) -> bool:
# Vendored
# --------------------------------------------------------------------------
+# No plural ``vendors``: that is as often first-party code about vendors
+# (``llms/src/providers/vendors/openai.ts``) as a copied library.
_VENDORED_DIR_TOKENS = frozenset(
{
"vendor",
- "vendors",
"third_party",
"thirdparty",
"third-party",
@@ -141,15 +230,34 @@ _VENDORED_DIR_TOKENS = frozenset(
"site-packages",
}
)
-# Matched only as the first segment: ``pkg/external/`` is as often a package
-# that talks to external services as it is a copy of someone else's code.
+# Matched as the first segment, or as a native tree's library shelf
+# (``src/native/external/<lib>/``). Anywhere else ``external/`` is as often a
+# package that talks to external services (``inference/external/``) or a
+# monorepo's own libraries as it is a copy of someone else's code.
_VENDORED_ROOT_TOKENS = frozenset({"external", "externals"})
+_NATIVE_TREE = "native"
_MINIFIED_SUFFIXES = (".min.js", ".min.css", ".min.mjs")
+
+def _is_vendored_path(lowered_name: str, dirs: list[str]) -> bool:
+ if any(d in _VENDORED_DIR_TOKENS for d in dirs):
+ return True
+ if dirs and dirs[0] in _VENDORED_ROOT_TOKENS:
+ return True
+ # ``native/external/<lib>/...``: the library directory must be there too.
+ if any(
+ d == _NATIVE_TREE and dirs[i + 1] in _VENDORED_ROOT_TOKENS for i, d in enumerate(dirs[:-2])
+ ):
+ return True
+ return lowered_name.endswith(_MINIFIED_SUFFIXES)
+
+
# A copied library is recognisable by its header only in the directories a
# site serves assets from. Anywhere else a license header is far more often
# the repository's own.
-_ASSET_DIR_TOKENS = frozenset({"docs", "doc", "static", "assets", "public", "_static"})
+# ``min/`` is a minified distribution tree (Monaco ships as ``min/vs/...``); the
+# header rule below keeps a first-party ``src/min/`` out.
+_ASSET_DIR_TOKENS = frozenset({"docs", "doc", "static", "assets", "public", "_static", "min"})
_HEADER_LINES = 20
_LICENSE_RE = re.compile(
r"copyright|\(c\)|©|@license|licen[cs]ed\s+under|released\s+under|\bMIT\b",
@@ -185,7 +293,10 @@ def _third_party_header(header: str, project: str | None) -> bool:
``Copyright (c) 2024 Acme`` header has no release banner, so it stays.
"""
comments = "\n".join(_comment_lines(header))
- if not comments or not _LICENSE_RE.search(comments) or not _BANNER_RE.search(comments):
+ # The comment prefix is stripped from *comments*, so a preserved ``/*!``
+ # opener is read from the raw head.
+ banner = _BANNER_RE.search(comments) or header.lstrip().startswith("/*!")
+ if not comments or not _LICENSE_RE.search(comments) or not banner:
return False
return not (project and _squash(project) in _squash(comments))
@@ -233,6 +344,12 @@ _TOOLING_DIR_TOKENS = frozenset(
"migrations",
}
)
+# Build logic kept as its own Gradle build at the repository root. Root only:
+# deeper, a ``build-tools`` package is as likely the product of a repository
+# that ships build tooling.
+_TOOLING_ROOT_DIRS = frozenset(
+ {"buildsrc", "build-logic", "build-conventions", "build-tools", "build-tools-internal"}
+)
_TOOLING_ROOT_NAMES = frozenset(
{"setup.py", "noxfile.py", "fabfile.py", "Rakefile", "Gruntfile.js"}
)
@@ -252,6 +369,49 @@ def _header_text(text: str | bytes | None) -> str:
return text[:4096]
+def _split(path: str) -> tuple[str, str, list[str]] | None:
+ normalized = path.replace("\\", "/")
+ parts = PurePosixPath(normalized).parts
+ if not parts:
+ return None
+ return normalized, parts[-1], [p.lower() for p in parts[:-1]]
+
+
+def _is_generated_path(name: str, dirs: list[str]) -> bool:
+ return (
+ _is_generated_name(name)
+ or any(d in _GENERATED_DIR_TOKENS for d in dirs)
+ or any(
+ d == _GENERATED_BUILD_DIR and i and dirs[i - 1] in _BUILD_OUTPUT_PARENTS
+ for i, d in enumerate(dirs)
+ )
+ )
+
+
+@lru_cache(maxsize=65536)
+def is_vendored_or_generated_path(path: str) -> bool:
+ """Whether the path alone marks code nobody here maintains by hand.
+
+ For callers with no file content, such as the dead-code passes: a copied
+ library or a generator's output is never this repository's dead code.
+ """
+ split = _split(path)
+ if split is None:
+ return False
+ _, name, dirs = split
+ return _is_generated_path(name, dirs) or _is_vendored_path(name.lower(), dirs)
+
+
+def _is_tooling(normalized: str, name: str, dirs: list[str]) -> bool:
+ return (
+ any(d in _TOOLING_DIR_TOKENS for d in dirs)
+ or bool(dirs and dirs[0] in _TOOLING_ROOT_DIRS)
+ or "/alembic/versions/" in f"/{normalized.lower()}"
+ or (not dirs and name in _TOOLING_ROOT_NAMES)
+ or bool(_TOOLING_NAME_RE.match(name.lower()))
+ )
+
+
def code_origin(
path: str,
text: str | bytes | None = None,
@@ -261,9 +421,10 @@ def code_origin(
) -> CodeOrigin:
"""The one origin of the file at repo-relative *path*.
- *text* is the file's content, or just its head; only the first 20 lines
- are read. Without it the content rules (generated banner, third-party
- license header) cannot fire and the answer rests on the path.
+ *text* is the file's content, or just its head; only its first 4 KB are
+ read. Without it the content rules (generated banner, reference-assembly
+ stub, third-party license header) cannot fire and the answer rests on the
+ path.
*is_test* is the flag ingestion stored, when the caller has it. ``None``
derives it from the path with the shared test classifier.
@@ -271,31 +432,20 @@ def code_origin(
*project* is the repository's name, so its own release banner is not
taken for someone else's.
"""
- normalized = path.replace("\\", "/")
- parts = PurePosixPath(normalized).parts
- if not parts:
+ split = _split(path)
+ if split is None:
return "production"
- name = parts[-1]
- dirs = [p.lower() for p in parts[:-1]]
+ normalized, name, dirs = split
header = _header_text(text)
- if (
- _is_generated_name(name)
- or any(d in _GENERATED_DIR_TOKENS for d in dirs)
- or (header and is_generated_header(header))
+ if _is_generated_path(name, dirs) or (
+ header and (_declares_generated(name, header) or _is_reference_stub(name, dirs, header))
):
return "generated"
-
- lowered_name = name.lower()
- if (
- any(d in _VENDORED_DIR_TOKENS for d in dirs)
- or (dirs and dirs[0] in _VENDORED_ROOT_TOKENS)
- or lowered_name.endswith(_MINIFIED_SUFFIXES)
- or (
- header
- and any(d in _ASSET_DIR_TOKENS for d in dirs)
- and _third_party_header(header, project)
- )
+ if _is_vendored_path(name.lower(), dirs) or (
+ header
+ and any(d in _ASSET_DIR_TOKENS for d in dirs)
+ and _third_party_header(header, project)
):
return "vendored"
@@ -305,12 +455,7 @@ def code_origin(
if _is_docs_example(dirs):
return "docs_example"
- if (
- any(d in _TOOLING_DIR_TOKENS for d in dirs)
- or "/alembic/versions/" in f"/{normalized.lower()}"
- or (not dirs and name in _TOOLING_ROOT_NAMES)
- or _TOOLING_NAME_RE.match(lowered_name)
- ):
+ if _is_tooling(normalized, name, dirs):
return "tooling"
return "production"
diff --git a/tests/unit/dead_code/test_convention_never_flag.py b/tests/unit/dead_code/test_convention_never_flag.py
index 7920b74b7..a97be8488 100644
--- a/tests/unit/dead_code/test_convention_never_flag.py
+++ b/tests/unit/dead_code/test_convention_never_flag.py
@@ -9,7 +9,7 @@ from __future__ import annotations
import networkx as nx
import pytest
-from repowise.core.analysis.dead_code.constants import never_flag_match
+from repowise.core.analysis.dead_code.constants import never_flag_match, never_flag_path
from repowise.core.analysis.dead_code.file_reachability import is_file_reachable
@@ -78,3 +78,28 @@ def test_python_startup_hooks_are_never_flagged(path):
)
def test_near_misses_stay_flaggable(path):
assert not never_flag_match(path)
+
+
+ "path",
+ [
+ # Copied libraries and generator output are not this repository's to delete.
+ "src/native/external/zlib/trees.c",
+ "x-pack/plugin/esql/src/main/generated/org/x/FooEvaluator.java",
+ ],
+)
+def test_vendored_and_generated_files_are_never_flagged(path):
+ assert never_flag_path(path)
+ assert not _unreachable(path)
+
+
+ "path",
+ [
+ "src/pkg/external/client.py",
+ "x-pack/plugin/inference/src/main/java/org/x/external/Sender.java",
+ "src/libraries/System.Runtime/src/System/DateTime.cs",
+ ],
+)
+def test_origin_near_misses_stay_flaggable(path):
+ assert not never_flag_path(path)
diff --git a/tests/unit/test_code_origin.py b/tests/unit/test_code_origin.py
index 7d68aa9e0..34ed07cd5 100644
--- a/tests/unit/test_code_origin.py
+++ b/tests/unit/test_code_origin.py
@@ -4,7 +4,12 @@ from __future__ import annotations
import pytest
-from repowise.core.code_origin import CODE_ORIGINS, code_origin, is_generated_header
+from repowise.core.code_origin import (
+ CODE_ORIGINS,
+ code_origin,
+ is_generated_header,
+ is_vendored_or_generated_path,
+)
_JQUERY = (
"/*!\n"
@@ -73,6 +78,37 @@ _OWN_HEADER = (
("api/user_grpc.pb.go", "generated"),
("Forms/Main.Designer.cs", "generated"),
("src/client.generated.ts", "generated"),
+ ("esql/src/main/generated/org/x/FooEvaluator.java", "generated"),
+ ("compute/src/main/generated-src/org/x/IntBlock.java", "generated"),
+ ("target/generated-sources/annotations/X.java", "generated"),
+ ("src/generator/emit.py", "production"), # the generator itself ships
+ ("src/native/external/zlib/inflate.c", "vendored"),
+ ("src/native/external/libunwind/include/libunwind.h", "vendored"),
+ ("src/native/external/rapidjson/prettywriter.hpp", "vendored"),
+ # external/ in a non-native tree is a package about external services.
+ ("x-pack/inference/src/main/java/org/x/external/http/Sender.java", "production"),
+ ("src/pkg/external/client.py", "production"),
+ ("sdk/llms/src/providers/vendors/openai.ts", "production"),
+ # min/ needs a third-party banner (test below); the path alone is not enough
+ ("src/min/app.js", "production"),
+ ("lib/min/util.js", "production"),
+ ("min/site.css", "production"),
+ ("src/min/compute.py", "production"),
+ # external/ is vendored at the root or as a native tree's library shelf
+ ("src/external/foo.cpp", "production"),
+ ("libs/external/x/a.h", "production"),
+ ("a/b/External/Foo.cpp", "production"),
+ ("src/native/external/zlib.cmake", "production"), # no library directory
+ # a bare generated/ only as a build output
+ ("app/generated/handlers.py", "production"),
+ ("tests/generated/test_x.py", "test"),
+ ("src/generated/schema.ts", "production"),
+ ("build/generated/source/X.java", "generated"),
+ ("build-tools-internal/src/main/java/org/x/Plugin.java", "tooling"),
+ ("build-tools/src/main/java/org/x/Task.java", "tooling"),
+ ("buildSrc/src/main/kotlin/conventions.gradle.kts", "tooling"),
+ ("build-logic/src/main/kotlin/Plugin.kt", "tooling"),
+ ("packages/build-tools/src/index.ts", "production"), # a shipped package
],
)
def test_path_rules(path: str, expected: str) -> None:
@@ -114,6 +150,71 @@ def test_generated_banner_negatives() -> None:
assert not is_generated_header("a = 1\nb = 2\n# DO NOT EDIT\n")
+_ES_EVALUATOR = (
+ "// Copyright Elasticsearch B.V. Licensed under the Elastic License 2.0.\n"
+ "package org.elasticsearch.xpack.esql.expression;\n"
+ "\n"
+ "import java.lang.Override;\n"
+ "import org.elasticsearch.compute.data.Block;\n"
+ "\n"
+ "/**\n"
+ " * {@link ExpressionEvaluator} implementation for {@link Greatest}.\n"
+ " * This class is generated. Edit {@code EvaluatorImplementer} instead.\n"
+ " */\n"
+ "public final class GreatestBooleanEvaluator {\n"
+)
+
+
+def test_generated_sentence_in_the_class_doc_comment() -> None:
+ path = "server/src/main/java/org/x/GreatestBooleanEvaluator.java"
+ assert code_origin(path, _ES_EVALUATOR) == "generated"
+ # Ingestion's skip rule still reads the first two lines only.
+ assert not is_generated_header(_ES_EVALUATOR)
+
+
+def test_generated_sentence_must_sit_in_a_comment() -> None:
+ text = 'package x;\n\nString msg = "This file is generated by the build";\n'
+ assert code_origin("src/main/java/x/Msg.java", text) == "production"
+
+
+def test_do_not_edit_below_the_header_is_not_a_banner() -> None:
+ text = "package x;\n\nclass T {\n // DO NOT EDIT this table without the schema\n}\n"
+ assert code_origin("src/main/java/x/T.java", text) == "production"
+
+
+_REF_STUB = (
+ "// Licensed to the .NET Foundation under one or more agreements.\n"
+ "// ----------------------------------------------------------------\n"
+ "// Changes to this file must follow the https://aka.ms/api-review process.\n"
+ "// ----------------------------------------------------------------\n"
+ "namespace System\n{\n"
+ " public partial struct DateTime\n {\n"
+ " public DateTime(int year, int month, int day) { throw null; }\n"
+)
+
+
+def test_reference_assembly_stub_is_generated() -> None:
+ path = "src/libraries/System.Runtime/ref/System.Runtime.cs"
+ assert code_origin(path, _REF_STUB) == "generated"
+ body = "namespace L { public class C { public int M() { throw null; } } }\n"
+ assert code_origin("src/tools/illink/src/linker/ref/Linker/C.cs", body) == "generated"
+
+
+def test_reference_stub_needs_the_stub_body_and_the_ref_dir() -> None:
+ real = "namespace App { public class Ref { public int M() => 1; } }\n"
+ assert code_origin("src/App/ref/Ref.cs", real) == "production"
+ assert code_origin("src/libraries/System.Runtime/src/DateTime.cs", _REF_STUB) == "production"
+ assert code_origin("src/libraries/System.Runtime/ref/System.Runtime.cs") == "production"
+
+
+def test_path_only_vendored_or_generated() -> None:
+ assert is_vendored_or_generated_path("src/native/external/zlib/trees.c")
+ assert is_vendored_or_generated_path("esql/src/main/generated/X.java")
+ assert not is_vendored_or_generated_path("src/Monaco/monacoSRC/min/vs/loader.js")
+ assert not is_vendored_or_generated_path("src/pkg/external/client.py")
+ assert not is_vendored_or_generated_path("tests/test_app.py")
+
+
@pytest.mark.parametrize("header", [_JQUERY, _TYPEAHEAD])
@pytest.mark.parametrize("folder", ["docs/_static", "static/js", "assets", "public/lib"])
def test_third_party_header_under_asset_dirs_is_vendored(header: str, folder: str) -> None:
@@ -155,3 +256,85 @@ def test_header_is_read_from_the_first_twenty_lines_only() -> None:
def test_windows_separators() -> None:
assert code_origin("docs_src\\tutorial\\app.py") == "docs_example"
+
+
+def test_generator_inputs_are_not_their_output() -> None:
+ # A template carries the banner it stamps on its output.
+ assert code_origin("compute/src/main/java/org/x/X-Block.java.st", _ES_EVALUATOR) == (
+ "production"
+ )
+ # A generator script holds the banner in a string, after its own code.
+ script = (
+ "import os\n"
+ "from util import write\n"
+ "\n"
+ 'PROLOG = """// Licensed under MIT.\n'
+ "/*\n"
+ "This file is generated using the logic from genEvents.py\n"
+ '*/"""\n'
+ )
+ assert code_origin("src/codegen/genEvents.py", script) == "production"
+
+
+def test_generated_sentence_about_an_import_is_not_a_banner() -> None:
+ text = (
+ "/**\n * @license\n * Copyright Google LLC\n */\n\n"
+ "import {signal} from '@angular/core';\n"
+ "// This file is generated at build-time, error is expected here.\n"
+ "import MANIFEST from '../assets/api/manifest.json';\n"
+ "\n"
+ "export class ApiManager {}\n"
+ )
+ assert code_origin("adev/src/app/api-manager.service.ts", text) == "production"
+
+
+_MONACO = (
+ "/*!-----------------------------------------------------------\n"
+ " * Copyright (c) Microsoft Corporation. All rights reserved.\n"
+ " * Version: 0.47.0(69991d66135e4a1fc1cf0b1ac4ad25d429866a0d)\n"
+ " * Released under the MIT license\n"
+ " *-----------------------------------------------------------*/"
+ 'define("vs/base/common/worker/simpleWorker.nls.de",{});\n'
+)
+
+
+def test_minified_tree_needs_a_third_party_banner() -> None:
+ assert code_origin("src/Monaco/monacoSRC/min/vs/loader.js", _MONACO, project="PowerToys") == (
+ "vendored"
+ )
+ assert code_origin("src/min/app.js", "export function boot() {}\n") == "production"
+ assert code_origin("src/min/app.js", _OWN_HEADER, project="acme") == "production"
+
+
+ "line",
+ [
+ "// This file is generated when you run make docs.",
+ "// This file was generated once, then edited by hand.",
+ "// This class is generated on demand by the factory below.",
+ ],
+)
+def test_generated_sentence_needs_a_machine_output_completion(line: str) -> None:
+ text = "package x;\n\n" + line + "\npublic class A {}\n"
+ assert code_origin("src/main/java/x/A.java", text) == "production"
+
+
+ "line",
+ [
+ "// This file is generated by GenerateTests.csx.",
+ "// This file is auto-generated from a template file.",
+ "// WARNING: This file is generated and should not be modified directly.",
+ "// THIS FILE IS GENERATED. DO NOT HAND EDIT.",
+ ],
+)
+def test_generated_sentence_completions(line: str) -> None:
+ text = "using System;\n\n" + line + "\nnamespace A {}\n"
+ assert code_origin("src/A.cs", text) == "generated"
+
+
+def test_reference_stub_markers_are_anchored() -> None:
+ mention = (
+ "namespace L { // callers may throw null; see docs\n public class C { int M() => 1; } }\n"
+ )
+ assert code_origin("src/Lib/ref/C.cs", mention) == "production"