Plan absolute and parent-relative imports

This commit is contained in:
2026-12-05 19:05:54 +00:00
parent 9037fdb74e
commit e2ec298cbc

View File

@@ -0,0 +1,247 @@
# Absolute and parent-relative imports
Status: proposed; no runtime changes have been implemented.
Baseline: `9037fdb74e1cd4f35ac78032f1bf70854b0e8754`.
## Purpose and agreed direction
Applications should be able to import shared configuration from locations such
as `/etc/shared/logging.yml`, even when their discovered main file lives in
another directory. The declaring file's canonical directory supplies the base
for relative imports. Absolute host paths select their named targets directly.
For ordinary host-file loading, enabling imports is sufficient to enable these
paths. Omission of an explicit containment root permits imports elsewhere on the
host, subject to filesystem access and the existing import rules. Parent-relative
paths such as `../shared/logging.yml` follow the same access policy. Absolute and
relative spellings of the same target should not acquire different permissions
merely because of their spelling.
An explicitly supplied `FilesystemContext.Root` remains an intentional boundary.
Applications can continue choosing containment when they need it. Supplied
`fs.FS` values remain their own namespaces; an import from such a namespace does
not silently switch to host filesystem access. The significant unresolved
details are collected under Open Decisions.
This feature changes filesystem selection and access. Existing composition,
precedence, configuration destinations, schema/version handling and inspection
semantics continue to apply.
## Current baseline
The [configuration contract](../config.md#explicit-imports) currently accepts
slash-separated relative selectors and rejects absolute paths. Host access uses
a canonical root which defaults to the selected main file's directory. Relative
targets and symlink targets must remain within that root. A caller can supply a
wider root, but absolute selector syntax remains invalid.
Host byte, reader and mapping sources currently require explicit absolute
`BaseDir` and `Root` directories for filesystem imports. A `Filesystem.File`
source uses its selected namespace, with `.` as its default root. Synthetic
sources with native context provide namespace-relative base and root directories.
Diagnostic labels do not establish filesystem locations.
Internally, the resolved context carries a root and base, and live host access
opens an `os.Root` for that root. Selection, link resolution and resource-context
construction rely on those facts. In particular,
`contextsForResolvedSources` rejects source directories outside the selected
root. Updating only selector validation would therefore leave external imports
unusable.
The same context machinery supports main-relative inputs, captured main sources,
selected overlays, passive resource contexts and configured secrets bootstrap.
Their uses of a root need explicit consideration when import access changes.
## Target behavior
### Host selectors and resolution
The following is valid when imports are enabled:
```yaml
config:
imports:
- path: /etc/shared/logging.yml
- path: ../shared/application.yml
- dir: /etc/shared/fragments
pattern: "*.yml"
- glob: "/etc/shared/services/*.yml"
```
Apply the same absolute/relative resolution policy to `path`, `dir` and `glob`,
including each element of a path or directory list. Existing selector-specific
options, wildcard grammar, filtering, ordering and requiredness remain in place.
An absolute glob establishes its absolute starting location; it does not scan
the declaring file's directory first. Handle path roots separately from pattern
segments so normalization preserves wildcard and escape semantics.
Relative imports in an acquired file resolve from that file's own canonical
directory, including when it lies outside the main file's directory. YAML
`destination` and `placement` continue to describe configuration-tree placement;
they do not affect physical path resolution.
Resolve host paths using the supported host platform's path rules while
preserving existing slash-based selector and glob conventions. Absolute volume
roots must not be accidentally interpreted as relative components where the
platform supports them. This feature does not expand the project's platform
support commitments. It adds no URL fetching, shell expansion, environment
substitution or implicit directory search.
### Explicit roots and links
Keep relative-path base selection independent of optional import containment.
For host files, an omitted `Root` means unrestricted host import scope; a
supplied root retains existing validation and containment semantics. Host
synthetic contexts should likewise be able to omit `Root` without manufacturing
a main-directory boundary; their base requirements are discussed below.
Apply explicit containment to canonical targets, including symlink targets and
every transitive import. Without an explicit boundary, a host symlink target
outside the main file's directory is eligible under the normal import rules.
Existing discovered-link `follow` behavior remains independent of whether the
resolved target is inside the former default root.
Use ordinary filesystem facilities and existing stable-input assumptions. No
additional sandbox, isolation, adversarial mutation guarantee, allowlist,
confirmation step or numeric limit is part of this feature.
### Identity, loading and reporting
Retain canonical access identity separately from selector spelling. Absolute,
parent-relative and symlink spellings of the same file must participate in the
existing cycle/repeated-source detection within its established scopes.
Cross-directory imports remain in the same host namespace for those checks.
Preserve established canonical-path identity, including the existing treatment
of hard links, rather than adding inode identity or new deduplication rules.
Maintain deterministic selection and dependency-ready composition ordering under
the existing [import scheduling model](../internal/imports.md). Source records,
import sites, source-local coordinates, version evidence and configuration
destinations continue to identify actual acquisitions. Absolute paths must not
create a second composition or precedence model.
External imports must work through ordinary and layered loading, mapping and
sequence-item placement, discovered mains and main-relative underlay contexts.
Prepared main sources must retain whether containment was expressly selected;
materializing a default context during preparation must not turn an omitted
import boundary into an explicit one on a subsequent load.
Keep passive `SourceContext` reporting usable for externally imported files,
including through resolution and available failed-load inspection. Its filesystem
and slash-relative `BaseDir` must address the contributing source's directory.
The default resource root for newly reachable sources is an open decision;
loading must not fail simply because that directory lies outside the former
implicit import root.
Release library-owned acquisition handles before returning, as today. No
additional close obligation or background activity is introduced.
### Related contexts retain their own contracts
Selected-overlay lookup conventions and main-relative context selection retain
their existing meanings. Imports within inputs that support them use the policy
above once their context has been selected. A broader import scope must not
accidentally change overlay search locations.
Configured secrets bootstrap retains its separately documented relative-path
and containment rules in [ADR 0023](../adr/0023-explicit-configured-secrets-bootstrap.md)
and the [configuration contract](../config.md#explicit-secrets-directory-bootstrap).
Changing imports does not implicitly enable absolute secrets directories, widen
their default root, or make `Load` modify the environment. Separate the internal
facts where a shared root currently conflates these responsibilities.
## Changes needed from the baseline
- **Declarations and selection:** revise selector validation, absolute-root
recognition, parent-relative normalization, directory selection and glob
starting locations. Preserve existing import defaults and error distinctions;
newly valid paths must still honor missing/required and inaccessible-target
behavior. Filesystem-dependent interpretation belongs with the selected
namespace rather than assuming every selector is a host path.
- **Access and contexts:** distinguish an authored containment boundary from
a relative base and from any root used for passive resource access. Revise
host/native context resolution, rooted access and link handling accordingly.
Unrestricted host access may require different internal access setup from
explicitly rooted access; opening the main directory alone cannot implement
it. Preserve caller-owned filesystem and reader ownership.
- **Pipeline integration:** carry those distinctions through discovery,
main-relative bindings, ordinary/grouped acquisition, prepared mains and
source-context construction. Reuse existing acquisition accounting and graph
admission. Avoid a separate absolute-import pipeline, repeated source reads,
or a retained root handle per imported file.
- **Contracts and decisions:** update `docs/config.md`, public filesystem-context
comments, relevant consumer examples and `docs/internal/imports.md` when the
implementation lands. Record the revised default and context distinctions in
a new ADR, including the extension of inherited containment described by
[ADR 0022](../adr/0022-main-relative-context.md). Preserve accepted ADR decision
text and the configured-bootstrap contract. Update architecture policy only
where its statements are affected.
- **Behavioral verification:** replace tests of the former implicit restriction
with coverage of the new default and retained explicit-root choice. Cover all
selectors, transitive relative bases, parent-relative paths, explicit-root
failures, symlink aliases, repeated sources and cycles. Include representative
discovery/preparation, mounted input, provenance/resource-context and failed
inspection interactions. Exercise native namespace behavior once selected.
Reuse existing semantic, budget and cleanup coverage rather than adding a
Cartesian test matrix or performance thresholds.
The implementation plan will own sequencing and concrete verification commands.
This roadmap proposes no new release gates, dependencies or public performance
guarantees. Completion means eligible absolute and parent-relative imports work
end to end, with intentional caller-selected boundaries and consistent source
evidence, without unrelated changes to configuration composition or startup.
## Open Decisions
### 1. Absolute selector syntax in supplied filesystem namespaces
**Recommendation:** interpret a leading `/` as the root of the selected `fs.FS`
namespace, and translate it to a valid namespace-relative path for Go filesystem
operations. Thus `/shared/common.yml` starts at that namespace's root, while
`../shared/common.yml` starts from the declaring file's directory. An explicit
`Root` still bounds canonical targets. This offers one conceptual resolution
rule across host and supplied filesystems while retaining namespace ownership.
It requires documenting YAML selector syntax separately from `Filesystem.File`
and `FilesystemContext` Go arguments, whose existing `fs.ValidPath` conventions
need not change.
**Alternative:** add absolute syntax only for host imports and retain relative
selectors in supplied filesystems. This follows Go's native `fs.FS` path format
directly and reduces implementation scope, but the same absolute YAML spelling
would have different acceptance depending on its selected namespace. Both
approaches keep native imports within the supplied filesystem.
### 2. Base requirements for synthetic host sources
**Recommendation:** retain an explicitly supplied absolute `BaseDir` for byte,
reader and mapping sources, while allowing their host context's `Root` to be
omitted. This preserves their existing explicit context model, supplies a
predictable base for relative declarations, and avoids deriving paths from
display labels or the working directory. It leaves some redundant setup for a
synthetic source whose declarations are all absolute.
**Alternative:** permit an explicitly selected host context without `BaseDir`
when a synthetic document uses only absolute selectors. Report a missing-base
error only if that document actually selects a relative path; imported files
still have their own directories. This reduces consumer setup for absolute-only
documents but requires representing a base-less context through acquisition and
passive reporting. It must not silently substitute the process working directory.
### 3. Default passive resource roots for external host imports
**Recommendation:** preserve existing passive contexts for sources inside their
previous default root. For a newly eligible external source with no explicit
containment root, expose a passive context rooted at that source's canonical
directory. With an explicit root, continue using it. This supports source-local
resource reads using the existing public API, preserves existing consumers'
contexts, and avoids making the entire host filesystem the default resource
view merely to broaden imports. It gives an external source the same default
resource scope as loading that file directly, while resource reads beyond its
directory may need a broader context selected by the caller.
**Alternative:** expose a host-filesystem-root resource view for unrestricted
imports, with each source directory represented relative to it. This is uniform
and accommodates cross-directory resource references with fewer per-source
choices, but changes existing `FS`/`BaseDir` coordinates and broadens passive
resource access for established consumers. Neither choice changes configured
secrets bootstrap or implies a general filesystem sandbox guarantee.