On this page
Python and Dart import scanners for dependency-import validation, filtering workspace-relevant imports and distinguishing lib vs test contexts.
#rlsbl.import_scanners
#rlsbl.import_scanners
Python, Dart, npm, Go, Java, and Kotlin import scanners for dependency-import validation.
Filters raw import data to workspace-relevant imports, handles language-specific edge cases, and distinguishes lib/ vs test/ contexts.
#ImportInfo
A single workspace-relevant import detected in a source file.
#_is_test_context
def _is_test_context(filepath: str, project_path: str) -> boolDetermine whether a file is in a non-production context.
Uses a layered approach to avoid false positives for production paths that happen to contain directory names like "test":
Layer 1 -- Unconditional directories (match at any depth): __tests__/, testdata/
Layer 2 -- Root-relative directories (match only as first component): test/, tests/, example/, examples/, integration_test/
Layer 3 -- File name patterns (checked against basename): test_.py, _test.py, _test.go, _test.dart, .test.[jt]sx?, .spec.[jt]sx?, conftest.py
#build_namespace_map
def build_namespace_map(projects, workspace_root: str) -> dict[str, str]Map namespace-qualified import paths to workspace project names.
For a project named 'protocols' at 'protocols/src/orxt/protocols/', returns {'orxt.protocols': 'protocols'}.
Algorithm:
- For each project, call detect_python_package_root() to get the
package root (e.g., 'src/orxt')
- The namespace is the package root's leaf directory name (e.g., 'orxt')
- Walk subdirectories of the package root looking for the project's
directory name
- If src/orxt/protocols/ exists and project name is 'protocols',
map 'orxt.protocols' -> 'protocols'
#PythonImportScanner
Scan Python source files for workspace-relevant imports.
Uses the AST-based scanner from the lint system, then post-processes to filter out stdlib, relative imports, and non-workspace packages. Supports namespace package detection via namespace_map and import_names.
#scan
def scan(self, project_path: str, workspace_names: set[str], exclude_dirs: list[str] | None=None, *, namespace_map: dict[str, str] | None=None, import_names: dict[str, str] | None=None) -> list[ImportInfo]Scan project_path for Python imports matching workspace members.
Args:
project_path: absolute path to the project root.workspace_names: set of workspace member package names
(as they appear in pyproject.toml, e.g. "my-lib").
exclude_dirs: directory paths to skip during the walk
(relative to project_path or absolute).
namespace_map: mapping of namespace-qualified import paths
to workspace project names (e.g., {'orxt.protocols': 'protocols'}). Built by build_namespace_map().
import_names: mapping of project_name -> import_name from workspace
config. Used for explicit import_name overrides.
Returns:
- list of ImportInfo for imports that match workspace members.
#DartImportScanner
Scan Dart source files for workspace-relevant package imports.
Uses regex to extract package names from import/export statements. Checks for missing generated (.g.dart) files when build_runner is configured.
#scan
def scan(self, project_path: str, workspace_names: set[str], exclude_dirs: list[str] | None=None) -> list[ImportInfo]Scan project_path for Dart imports matching workspace members.
Args:
project_path: absolute path to the project root.workspace_names: set of workspace member package names
(as they appear in pubspec.yaml).
exclude_dirs: directory paths to skip during the walk
(relative to project_path or absolute).
Returns:
- list of ImportInfo for imports that match workspace members.
Raises:
RuntimeError: if build.yaml exists but no .g.dart files
are found in the project (missing code generation).
#_check_generated_files
def _check_generated_files(self, project_path: str) -> NoneRaise RuntimeError if build_runner is configured but no .g.dart files exist.
#_extract_npm_bare_name
def _extract_npm_bare_name(specifier: str) -> str | NoneExtract bare package name from an npm import specifier.
Returns None for relative imports, Node.js builtins, and node:-prefixed builtins. For scoped packages (@scope/pkg/foo), returns @scope/pkg. For unscoped (pkg/foo), returns pkg.
#NpmImportScanner
Scan JS/TS source files for workspace-relevant imports.
Uses the AST-based scanner from the npm lint system, then post-processes to filter out relative imports, Node.js builtins, and non-workspace packages.
#scan
def scan(self, project_path: str, workspace_names: set[str], exclude_dirs: list[str] | None=None) -> list[ImportInfo]Scan project_path for JS/TS imports matching workspace members.
Args:
project_path: absolute path to the project root.workspace_names: set of workspace member package names
(as they appear in package.json, e.g. "@scope/my-lib").
exclude_dirs: directory paths to skip during the walk
(relative to project_path or absolute).
Returns:
- list of ImportInfo for imports that match workspace members.
#GoImportScanner
Scan Go source files for workspace-relevant imports.
Uses the tree-sitter-based scanner from the Go lint system, then post-processes to filter to imports matching other workspace projects' Go module paths.
#scan
def scan(self, project_path: str, workspace_names: set[str], exclude_dirs: list[str] | None=None, *, module_path_map: dict[str, str] | None=None) -> list[ImportInfo]Scan project_path for Go imports matching workspace members.
Args:
project_path: absolute path to the project root.workspace_names: set of workspace member package names.exclude_dirs: directory paths to skip during the walk
(relative to project_path or absolute).
module_path_map: mapping of workspace project name to its
Go module path (from go.mod). Only Go projects appear in this map. Required for Go import detection.
Returns:
- list of ImportInfo for imports that match workspace members.
#_match_workspace_import
def _match_workspace_import(import_path: str, module_to_name: dict[str, str]) -> str | NoneCheck if an import path belongs to a workspace sibling.
An import matches a workspace module if the import path equals the module path or starts with it followed by '/'.
#build_jvm_package_map
def build_jvm_package_map(projects: list, workspace_root: str) -> dict[str, str]Map Java/Kotlin package prefixes to workspace project names.
For each workspace project with a pom.xml or build.gradle(.kts), reads the groupId (from POM) or group (from Gradle) and maps it to the project name. This allows import scanning to determine which workspace project an import like com.example.foo.Bar belongs to.
Args:
projects: list of workspace project dicts/objects withname
and path attributes.
workspace_root: absolute path to the workspace root.
Returns:
- dict mapping dotted package prefix to workspace project name.
- E.g.
{"com.example.foo": "foo-lib"}
#_JvmImportScannerBase
Base class for Java and Kotlin import scanners.
Scans source files for import statements matching workspace projects via a package prefix map. Subclasses specify which file extensions to scan.
#scan
def scan(self, project_path: str, workspace_names: set[str], exclude_dirs: list[str] | None=None, *, package_map: dict[str, str] | None=None) -> list[ImportInfo]Scan project_path for JVM imports matching workspace members.
Args:
project_path: absolute path to the project root.workspace_names: set of workspace member package names.exclude_dirs: directory paths to skip during the walk
(relative to project_path or absolute).
package_map: mapping of dotted package prefix to workspace
project name. Built by build_jvm_package_map(). Required for JVM import detection.
Returns:
- list of ImportInfo for imports that match workspace members.
#JavaImportScanner
Scan Java source files for workspace-relevant imports.
Uses regex to extract import statements from .java files, then matches against the workspace package prefix map.
#KotlinImportScanner
Scan Kotlin source files for workspace-relevant imports.
Uses regex to extract import statements from .kt and .kts files, then matches against the workspace package prefix map.