Skip to content
Merged
16 changes: 15 additions & 1 deletion project_validator/build_systems/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class BuildSystem:
`members` reads, from a build file, the directories (globs relative to it) of the workspace it declares; each
member is a unit of the project of its own (see `project_validator.facts.structure.Unit`).
`compiler_evidence` gives the words of one of its commands that show the compiler it builds with.
`standalone` tells, from its text, whether one of its build files below another of its build files is a project
of its own (a nested Package.swift, a package.json with a name; see `standalone_package`), a unit of the project
with the role its directory tells; None: a nested build file is part of the one above it (a CMake
subdirectory, a Maven module).
`path_dependencies` reads, from a build file, the directories (relative to it) of the packages it depends on by
path (Cargo's `path = "../x"`): what a unit of tests tests (see `project_validator.facts.structure.Unit`).
"""

name: str
Expand All @@ -40,6 +46,14 @@ class BuildSystem:
configured_build_types: Callable[[Command, Callable[[str], str | None]], frozenset[str]] | None = None
members: Callable[[str], tuple[str, ...]] | None = None
compiler_evidence: Callable[[tuple[str, ...]], tuple[str, ...]] | None = None
standalone: Callable[[str], bool] | None = None
path_dependencies: Callable[[str], tuple[str, ...]] | None = None


__all__ = ["C_FAMILY", "BuildSystem"]
def standalone_package(_text: str) -> bool:
"""Every build file of a build system whose nested build files are projects of their own (a nested Mix project
is a package of its own, whatever it says)."""
return True


__all__ = ["C_FAMILY", "BuildSystem", "standalone_package"]
2 changes: 2 additions & 0 deletions project_validator/checks/tests_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
answers=tests,
judge=judge_tests,
aliases=("ctest-runs",),
# A unit the map names auxiliary is asked for tests it has: a cargo-fuzz crate need hold none (upstream #230).
auxiliary_unit=lambda answer: bool(answer.definitions() or answer.unknown_definitions),
)
5 changes: 4 additions & 1 deletion project_validator/ecosystems/beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import re
from collections.abc import Callable, Iterator

from project_validator.build_systems.model import standalone_package
from project_validator.core import Context
from project_validator.ecosystems.model import BuildSystem, Ecosystem, subcommands, toolchain_patterns
from project_validator.facts import Command, Facts, Origin, Setting, resolved
Expand Down Expand Up @@ -53,7 +54,9 @@ def _umbrella_members(text: str) -> tuple[str, ...]:


# A Mix project compiles the Erlang sources in `src/` too; an umbrella's applications are the units.
MIX = BuildSystem(name="Mix", files=(MIX_FILE,), languages=ELIXIR_ONLY, members=_umbrella_members)
MIX = BuildSystem(
name="Mix", files=(MIX_FILE,), languages=ELIXIR_ONLY, members=_umbrella_members, standalone=standalone_package
)
REBAR3 = BuildSystem(name="rebar3", files=(REBAR_CONFIG, "rebar.config.script"), languages=ERLANG_ONLY)


Expand Down
17 changes: 15 additions & 2 deletions project_validator/ecosystems/clojure.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import re
from pathlib import Path

from project_validator.build_systems.model import standalone_package
from project_validator.core import Context
from project_validator.ecosystems.model import BuildSystem, Ecosystem, subcommands, toolchain_patterns
from project_validator.facts import Command, Facts, Origin, resolved
Expand Down Expand Up @@ -42,8 +43,20 @@ def _is_build_tree(directory: Path) -> bool:
return directory.name == "target" and any((directory.parent / name).is_file() for name in (DEPS_EDN, PROJECT_CLJ))


CLOJURE_CLI = BuildSystem(name="Clojure CLI", files=(DEPS_EDN,), languages=CLOJURE, is_build_tree=_is_build_tree)
LEININGEN = BuildSystem(name="Leiningen", files=(PROJECT_CLJ,), languages=CLOJURE, is_build_tree=_is_build_tree)
CLOJURE_CLI = BuildSystem(
name="Clojure CLI",
files=(DEPS_EDN,),
languages=CLOJURE,
is_build_tree=_is_build_tree,
standalone=standalone_package,
)
LEININGEN = BuildSystem(
name="Leiningen",
files=(PROJECT_CLJ,),
languages=CLOJURE,
is_build_tree=_is_build_tree,
standalone=standalone_package,
)
BABASHKA = BuildSystem(name="Babashka", files=("bb.edn",), languages=CLOJURE)
SHADOW_CLJS = BuildSystem(
name="shadow-cljs", files=("shadow-cljs.edn",), languages=CLOJURE, is_build_tree=_is_build_tree
Expand Down
8 changes: 7 additions & 1 deletion project_validator/ecosystems/dart.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import Path, PurePosixPath

from project_validator import yaml_support
from project_validator.build_systems.model import standalone_package
from project_validator.core import Project
from project_validator.ecosystems.model import BuildSystem, Ecosystem, subcommands, toolchain_patterns
from project_validator.facts import Command, Facts, resolved
Expand Down Expand Up @@ -55,7 +56,12 @@ def _build_beside_pubspec(directory: Path) -> bool:


PUB = BuildSystem(
name="pub", files=(PUBSPEC,), languages=DART, members=_workspace_members, is_build_tree=_build_beside_pubspec
name="pub",
files=(PUBSPEC,),
languages=DART,
members=_workspace_members,
is_build_tree=_build_beside_pubspec,
standalone=standalone_package,
)


Expand Down
2 changes: 2 additions & 0 deletions project_validator/ecosystems/fortran.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from project_validator.build_configurations import build_type_kinds
from project_validator.build_systems.cmake.build import CMAKE
from project_validator.build_systems.meson.build import MESON
from project_validator.build_systems.model import standalone_package
from project_validator.core import Context
from project_validator.ecosystems.model import BuildSystem, Ecosystem
from project_validator.facts import Command, Fact, Facts, Origin, Setting, resolved
Expand Down Expand Up @@ -63,6 +64,7 @@ def fpm_build_types(command: Command) -> frozenset[str]:
files=(FPM_MANIFEST,),
languages=FORTRAN_LANGUAGE,
build_types=fpm_build_types,
standalone=standalone_package,
)
FPM_SETTINGS = SettingsReader(
name="fpm", files=(FPM_MANIFEST,), read=lambda path, text: toml_settings("fpm", path, text)
Expand Down
2 changes: 2 additions & 0 deletions project_validator/ecosystems/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
from collections.abc import Callable, Iterator, Mapping

from project_validator.build_systems.model import standalone_package
from project_validator.core import Context
from project_validator.ecosystems.model import BuildSystem, Ecosystem, subcommands, toolchain_patterns
from project_validator.facts import Command, Fact, Facts, Origin, Setting, resolved
Expand Down Expand Up @@ -55,6 +56,7 @@ def _go_work_members(text: str) -> tuple[str, ...]:
files=("go.mod", "go.sum", "go.work", "go.work.sum"),
languages=GO_LANGUAGE,
members=_go_work_members,
standalone=standalone_package,
)

GOLANGCI_CONFIG_FILES = (".golangci.yml", ".golangci.yaml", ".golangci.toml", ".golangci.json")
Expand Down
2 changes: 2 additions & 0 deletions project_validator/ecosystems/haskell/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from pathlib import Path, PurePosixPath

from project_validator import yaml_support
from project_validator.build_systems.model import standalone_package
from project_validator.ecosystems.haskell.cabal import CabalSyntaxError, parse, top_level
from project_validator.ecosystems.haskell.options import HASKELL_LANGUAGE
from project_validator.ecosystems.model import BuildSystem, Ecosystem, subcommands, toolchain_patterns
Expand Down Expand Up @@ -70,6 +71,7 @@ def _hpack_languages(text: str) -> frozenset[str]:
languages=HASKELL_LANGUAGE,
is_build_tree=_is_build_tree,
members=_members,
standalone=standalone_package,
)
STACK = BuildSystem(name="Stack", files=("stack.yaml",), languages=HASKELL_LANGUAGE, members=_members)
HPACK = BuildSystem(
Expand Down
89 changes: 69 additions & 20 deletions project_validator/ecosystems/javascript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@
from project_validator.facts.coverage import Granularity, coverage_tool, measured, reported
from project_validator.facts.dependencies import PackageManager, lockfile_reader, manifest_reader
from project_validator.facts.exit_status import POSIX
from project_validator.facts.expansion import CommandExpander, Expansion, Script, found_at, joined, launcher
from project_validator.facts.expansion import (
CommandExpander,
Expansion,
Launched,
Script,
found_at,
joined,
launcher,
)
from project_validator.facts.formatting import Enforcement, flagged, format_tool
from project_validator.facts.licenses import declared_license_reader
from project_validator.facts.outputs import BuildOutput, directories, ignored_at_top_level
Expand All @@ -60,35 +68,73 @@
JAVASCRIPT = "JavaScript"
TYPESCRIPT = "TypeScript"
PACKAGE_JSON = "package.json"
PNPM_WORKSPACE = "pnpm-workspace.yaml"
DENO_CONFIG_FILES = ("deno.json", "deno.jsonc")
# A package.json that names what it provides is a JavaScript package, even before any source is found.
ENTRY_POINTS = ("main", "module", "exports", "bin")


def _declared_by_package(_path: str, text: str) -> frozenset[str]:
"""package.json is everywhere, as the tooling of projects in other languages; only a package's entry points (or
JavaScript sources, recognised by their suffixes) make a project a JavaScript one."""
def _manifest(text: str) -> dict[str, object]:
"""A package.json's fields; none when it is not a JSON object."""
try:
manifest = json.loads(text)
except json.JSONDecodeError:
return frozenset()
is_package = isinstance(manifest, dict) and any(key in manifest for key in ENTRY_POINTS)
return {}
return manifest if isinstance(manifest, dict) else {}


def _declared_by_package(_path: str, text: str) -> frozenset[str]:
"""package.json is everywhere, as the tooling of projects in other languages; only a package's entry points (or
JavaScript sources, recognised by their suffixes) make a project a JavaScript one."""
is_package = any(key in _manifest(text) for key in ENTRY_POINTS)
return frozenset({JAVASCRIPT}) if is_package else frozenset()


def _workspaces(text: str) -> tuple[str, ...]:
"""The workspaces a package.json declares (npm, yarn and bun: `workspaces`, as a list or under `packages`)."""
def _named_package(text: str) -> bool:
"""Whether a package.json below another is a package of its own: one with a name, not one that only marks its
directory's modules (`{"type": "module"}` in `dist/`)."""
return "name" in _manifest(text)


def _pnpm_packages(text: str) -> tuple[str, ...]:
"""The packages a pnpm-workspace.yaml declares (`packages:`, globs; `!` before one leaves it out)."""
try:
manifest = json.loads(text)
except json.JSONDecodeError:
declared = yaml_support.load(text)
except yaml_support.YamlError:
return ()
found = manifest.get("workspaces") if isinstance(manifest, dict) else None
packages = declared.get("packages") if isinstance(declared, dict) else None
return tuple(str(pattern) for pattern in packages) if isinstance(packages, list) else ()


def _declared_workspace(text: str) -> tuple[str, ...]:
"""The workspace members a package.json or a pnpm-workspace.yaml declares, each a unit of its own."""
return _workspaces(text) or _pnpm_packages(text)


def _path_dependencies(text: str) -> tuple[str, ...]:
"""The directories of the packages a package.json depends on by path (`file:../x`, `link:../x`)."""
manifest = _manifest(text)
tables = (manifest.get(kind) for kind in ("dependencies", "devDependencies", "peerDependencies"))
specs = (spec for table in tables if isinstance(table, dict) for spec in table.values() if isinstance(spec, str))
return tuple(dict.fromkeys(spec.partition(":")[2] for spec in specs if spec.startswith(("file:", "link:"))))


def _workspaces(text: str) -> tuple[str, ...]:
"""The workspaces a package.json declares (npm, yarn and bun: `workspaces`, as a list or under `packages`)."""
found = _manifest(text).get("workspaces")
if isinstance(found, dict):
found = found.get("packages")
return tuple(str(member) for member in found) if isinstance(found, list) else ()


NPM = BuildSystem(name="npm", files=(PACKAGE_JSON,), declared_languages=_declared_by_package, members=_workspaces)
NPM = BuildSystem(
name="npm",
files=(PACKAGE_JSON, PNPM_WORKSPACE),
declared_languages=_declared_by_package,
members=_declared_workspace,
standalone=_named_package,
path_dependencies=_path_dependencies,
)
DENO = BuildSystem(name="Deno", files=DENO_CONFIG_FILES, languages=frozenset({TYPESCRIPT}))


Expand Down Expand Up @@ -161,13 +207,8 @@ def workspace_members(context: Context, directory: str | None) -> list[str]:
if context.project.exists(path := joined(directory, PACKAGE_JSON))
else []
)
if context.project.exists(pnpm := joined(directory, "pnpm-workspace.yaml")):
try:
declared = yaml_support.load(context.project.read_text(pnpm))
except yaml_support.YamlError:
declared = None
packages = declared.get("packages") if isinstance(declared, dict) else None
patterns += [str(pattern) for pattern in packages] if isinstance(packages, list) else []
if context.project.exists(pnpm := joined(directory, PNPM_WORKSPACE)):
patterns += _pnpm_packages(context.project.read_text(pnpm))
globs = [
joined(directory, pattern.removeprefix("./").rstrip("/")) for pattern in patterns if not pattern.startswith("!")
]
Expand Down Expand Up @@ -203,7 +244,7 @@ def _package_scripts(command: Command, context: Context) -> Iterator[Expansion]
scripts = _json_strings(json_value(context, path, "scripts"))
if run.name not in scripts:
if not run.explicit:
return None
return _binary(command, run)
return iter((Unknown(origin=command.origin, subject=f"the script {run.name}", reason=f"{path} has none"),))
names = [f"{prefix}{run.name}" for prefix in (("pre", "", "post") if command.argv[0] in ("npm", "yarn") else ("",))]
return (
Expand All @@ -214,6 +255,14 @@ def _package_scripts(command: Command, context: Context) -> Iterator[Expansion]
)


def _binary(command: Command, run: _ScriptRun) -> Iterator[Expansion] | None:
"""`yarn c8 yarn test`, where the package has no script c8: the c8 binary it runs, as `yarn exec c8 yarn test`
does (prettier, upstream #188)."""
if run.name not in command.argv:
return None
return iter((Launched(argv=command.argv[command.argv.index(run.name) :]),))


PACKAGE_SCRIPTS = CommandExpander(name="package.json scripts", expand=_package_scripts)
# Monorepo task runners, which run a script in each workspace package that has it.
_TASK_RUNNERS = frozenset({"turbo", "lerna", "nx"})
Expand Down
3 changes: 2 additions & 1 deletion project_validator/ecosystems/php/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import re
from collections.abc import Callable

from project_validator.build_systems.model import standalone_package
from project_validator.ecosystems.model import BuildSystem, Ecosystem, subcommands, toolchain_patterns
from project_validator.facts import Command, Facts, Setting, resolved
from project_validator.facts.analysis import Strictness, analysed, analyzer, type_checked, type_checker
Expand All @@ -28,7 +29,7 @@
COMPOSER_JSON = "composer.json"


COMPOSER = BuildSystem(name="Composer", files=(COMPOSER_JSON,), languages=PHP_ONLY)
COMPOSER = BuildSystem(name="Composer", files=(COMPOSER_JSON,), languages=PHP_ONLY, standalone=standalone_package)


PHPUNIT_CONFIG_FILES = ("phpunit.xml", "phpunit.xml.dist", "phpunit.dist.xml")
Expand Down
2 changes: 2 additions & 0 deletions project_validator/ecosystems/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pathlib import PurePosixPath

from project_validator import yaml_support
from project_validator.build_systems.model import standalone_package
from project_validator.core import Context
from project_validator.ecosystems.model import BuildSystem, Ecosystem, toml_members, toolchain_patterns
from project_validator.facts import Command, Fact, Facts, Origin, Setting, resolved
Expand Down Expand Up @@ -37,6 +38,7 @@
files=("pyproject.toml", "setup.py"),
languages=PYTHON_ONLY,
members=toml_members("tool", "uv", "workspace"),
standalone=standalone_package,
)


Expand Down
3 changes: 2 additions & 1 deletion project_validator/ecosystems/ruby/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import re

from project_validator.build_systems.model import standalone_package
from project_validator.ecosystems.model import VERSION, BuildSystem, Ecosystem, subcommands, toolchain_patterns
from project_validator.facts.axes import versions_axis
from project_validator.facts.dependencies import Lock, PackageManager, lockfile_reader, manifest_reader
Expand All @@ -23,7 +24,7 @@
GEMFILE = "Gemfile"


BUNDLER = BuildSystem(name="Bundler", files=(GEMFILE, "*.gemspec"), languages=RUBY_ONLY)
BUNDLER = BuildSystem(name="Bundler", files=(GEMFILE, "*.gemspec"), languages=RUBY_ONLY, standalone=standalone_package)


RUNS_RAKE = re.compile(r"(?<![\w-])rake\b")
Expand Down
22 changes: 22 additions & 0 deletions project_validator/ecosystems/rust/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ def cargo_argv(argv: tuple[str, ...]) -> tuple[str, ...]:
return ("cargo", *words)


def _cargo_package(text: str) -> bool:
"""Whether a Cargo.toml below another is a project of its own: a package, or a workspace (not a manifest that
only sets a virtual workspace's members' shared settings)."""
return bool(re.search(r"^\[(package|workspace)\]", text, re.MULTILINE))


def _path_dependencies(text: str) -> tuple[str, ...]:
"""The directories of the crates a Cargo.toml depends on by path, in any of its dependency tables."""
try:
manifest = tomllib.loads(text)
except tomllib.TOMLDecodeError:
return ()
targets = manifest.get("target")
holders = [manifest, *(targets.values() if isinstance(targets, dict) else ())]
kinds = ("dependencies", "dev-dependencies", "build-dependencies")
tables = [holder.get(kind) for holder in holders if isinstance(holder, dict) for kind in kinds]
specs = (spec for table in tables if isinstance(table, dict) for spec in table.values() if isinstance(spec, dict))
return tuple(dict.fromkeys(spec["path"] for spec in specs if isinstance(spec.get("path"), str)))


CARGO = BuildSystem(
name="Cargo",
files=("Cargo.toml",),
Expand All @@ -93,6 +113,8 @@ def cargo_argv(argv: tuple[str, ...]) -> tuple[str, ...]:
build_types=cargo_build_types,
configured_build_types=cargo_profile_build_types,
members=toml_members("workspace"),
standalone=_cargo_package,
path_dependencies=_path_dependencies,
)


Expand Down
Loading
Loading