2026-10-25 13:09:44 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io/fs"
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type resolvedFilesystemContext struct {
|
2026-12-05 20:37:05 +00:00
|
|
|
native *nativeNamespace
|
|
|
|
|
hostToken *namespaceToken
|
|
|
|
|
rootPath string
|
|
|
|
|
importRoot string
|
|
|
|
|
basePath string
|
|
|
|
|
mainPath string
|
2026-10-25 13:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resolveFilesystemContext(source Source, context *FilesystemContext, main parsedSource, hostToken *namespaceToken) (resolvedFilesystemContext, *Diagnostic) {
|
2026-11-18 00:44:59 +00:00
|
|
|
if source.captured != nil {
|
|
|
|
|
return resolveCapturedFilesystemContext(source, context, main, hostToken)
|
|
|
|
|
}
|
2026-10-25 13:13:47 +00:00
|
|
|
if source.kind == sourceNone && context != nil {
|
|
|
|
|
return resolveAbsentFilesystemContext(context)
|
|
|
|
|
}
|
2026-10-25 13:09:44 +00:00
|
|
|
if nativeContext(source, context) {
|
|
|
|
|
return resolveNativeFilesystemContext(source, context)
|
|
|
|
|
}
|
|
|
|
|
return resolveHostFilesystemContext(source, context, main, hostToken)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 00:44:59 +00:00
|
|
|
func resolveCapturedFilesystemContext(source Source, context *FilesystemContext, main parsedSource, hostToken *namespaceToken) (resolvedFilesystemContext, *Diagnostic) {
|
|
|
|
|
captured := source.captured
|
2026-12-05 20:52:29 +00:00
|
|
|
resolved := resolvedFilesystemContext{hostToken: hostToken, rootPath: captured.rootPath, importRoot: captured.importRoot, basePath: captured.basePath, mainPath: captured.mainPath}
|
2026-11-18 00:44:59 +00:00
|
|
|
if context == nil {
|
|
|
|
|
return resolved, nil
|
|
|
|
|
}
|
|
|
|
|
if context.FS != nil || context.BaseDir != "" {
|
|
|
|
|
value := optionDiagnostic(CodeOptionsInvalid, "prepared source requires a compatible host filesystem context")
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
2026-12-05 20:52:29 +00:00
|
|
|
if context.Root == "" {
|
|
|
|
|
return resolved, nil
|
|
|
|
|
}
|
|
|
|
|
// Check only the supplied root. Captured main identity and base remain
|
|
|
|
|
// usable after deletion or retargeting of the original main filename.
|
|
|
|
|
root, err := canonicalExistingPath(context.Root)
|
|
|
|
|
if err != nil || !sameHostPath(root, captured.rootPath) {
|
2026-11-18 00:44:59 +00:00
|
|
|
value := optionDiagnostic(CodeOptionsInvalid, "prepared source requires a compatible host filesystem context")
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
2026-12-05 20:52:29 +00:00
|
|
|
resolved.importRoot = root
|
2026-11-18 00:44:59 +00:00
|
|
|
return resolved, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sameHostPath(left, right string) bool {
|
|
|
|
|
return containedPath(left, right) && containedPath(right, left)
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-25 13:13:47 +00:00
|
|
|
func resolveAbsentFilesystemContext(context *FilesystemContext) (resolvedFilesystemContext, *Diagnostic) {
|
|
|
|
|
if context.FS != nil {
|
|
|
|
|
return resolveNativeFilesystemContext(Source{kind: sourceNone}, context)
|
|
|
|
|
}
|
2026-12-05 20:37:05 +00:00
|
|
|
if context.Root == "" && context.BaseDir == "" {
|
|
|
|
|
value := optionDiagnostic(CodeOptionsInvalid, "absent-input filesystem context requires a base or root directory")
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
|
|
|
|
anchor := context.Root
|
|
|
|
|
if anchor == "" {
|
|
|
|
|
anchor = context.BaseDir
|
|
|
|
|
}
|
|
|
|
|
rootPath, err := canonicalExistingPath(anchor)
|
2026-10-25 13:13:47 +00:00
|
|
|
if err != nil {
|
|
|
|
|
value := importDiagnostic(CodeImportInvalid, "configuration root could not be canonicalized", nil)
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
2026-12-05 20:37:05 +00:00
|
|
|
resolved := resolvedFilesystemContext{rootPath: rootPath, basePath: rootPath}
|
|
|
|
|
if context.Root != "" {
|
|
|
|
|
resolved.importRoot = rootPath
|
|
|
|
|
}
|
2026-10-25 13:13:47 +00:00
|
|
|
if context.BaseDir != "" {
|
2026-12-05 20:37:05 +00:00
|
|
|
resolved.basePath, err = canonicalExistingPath(context.BaseDir)
|
2026-10-25 13:13:47 +00:00
|
|
|
if err != nil {
|
|
|
|
|
value := importDiagnostic(CodeImportInvalid, "import base directory could not be canonicalized", nil)
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-12-05 20:37:05 +00:00
|
|
|
if resolved.importRoot != "" && !containedPath(resolved.importRoot, resolved.basePath) {
|
2026-10-25 13:18:54 +00:00
|
|
|
value := importDiagnostic(CodeImportOutsideRoot, "selected source and import base must stay inside the configuration root", nil)
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
2026-12-05 20:37:05 +00:00
|
|
|
return resolved, nil
|
2026-10-25 13:13:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-10-25 13:09:44 +00:00
|
|
|
func resolveNativeFilesystemContext(source Source, context *FilesystemContext) (resolvedFilesystemContext, *Diagnostic) {
|
|
|
|
|
var namespace *nativeNamespace
|
|
|
|
|
resolved := resolvedFilesystemContext{rootPath: ".", basePath: "."}
|
|
|
|
|
if source.kind == sourceFilesystem {
|
|
|
|
|
namespace = source.namespace
|
|
|
|
|
resolved.mainPath = source.path
|
|
|
|
|
resolved.basePath = path.Dir(source.path)
|
|
|
|
|
if context != nil && context.Root != "" {
|
|
|
|
|
resolved.rootPath = context.Root
|
|
|
|
|
}
|
|
|
|
|
} else if context != nil && context.FS != nil {
|
|
|
|
|
namespace = context.FS.namespace
|
|
|
|
|
resolved.rootPath, resolved.basePath = context.Root, context.BaseDir
|
|
|
|
|
}
|
|
|
|
|
if namespace == nil || nilFilesystem(namespace.filesystem) || !fs.ValidPath(resolved.rootPath) || !fs.ValidPath(resolved.basePath) {
|
|
|
|
|
value := optionDiagnostic(CodeOptionsInvalid, "filesystem context is invalid")
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
|
|
|
|
resolved.native = namespace
|
2026-12-05 20:37:05 +00:00
|
|
|
resolved.importRoot = resolved.rootPath
|
2026-10-25 13:09:44 +00:00
|
|
|
if !containsNativePath(resolved.rootPath, resolved.basePath) || resolved.mainPath != "" && !containsNativePath(resolved.rootPath, resolved.mainPath) {
|
|
|
|
|
value := importDiagnostic(CodeImportOutsideRoot, "selected source and import base must stay inside the configuration root", nil)
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
|
|
|
|
return resolved, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resolveHostFilesystemContext(source Source, context *FilesystemContext, main parsedSource, hostToken *namespaceToken) (resolvedFilesystemContext, *Diagnostic) {
|
2026-10-25 13:13:47 +00:00
|
|
|
if main.source == nil {
|
2026-10-25 13:09:44 +00:00
|
|
|
value := importDiagnostic(CodeImportInvalid, "imports require a selected configuration source", nil)
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
|
|
|
|
resolved := resolvedFilesystemContext{hostToken: hostToken}
|
|
|
|
|
if source.kind == sourceFile {
|
|
|
|
|
if context != nil && context.BaseDir != "" {
|
|
|
|
|
value := optionDiagnostic(CodeOptionsInvalid, "file sources determine their own import base directory")
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
|
|
|
|
canonical := main.source.Path
|
|
|
|
|
if canonical == "" {
|
|
|
|
|
var err error
|
|
|
|
|
canonical, err = canonicalExistingPath(source.path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
value := importDiagnostic(CodeImportInvalid, "selected source path could not be canonicalized", nil)
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
resolved.mainPath = canonical
|
|
|
|
|
resolved.basePath = filepath.Dir(canonical)
|
|
|
|
|
resolved.rootPath = resolved.basePath
|
|
|
|
|
if context != nil && context.Root != "" {
|
|
|
|
|
rootPath, err := canonicalExistingPath(context.Root)
|
|
|
|
|
if err != nil {
|
|
|
|
|
value := importDiagnostic(CodeImportInvalid, "configuration root could not be canonicalized", nil)
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
|
|
|
|
resolved.rootPath = rootPath
|
2026-12-05 20:37:05 +00:00
|
|
|
resolved.importRoot = rootPath
|
2026-10-25 13:09:44 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-12-05 20:37:05 +00:00
|
|
|
if context == nil || context.BaseDir == "" {
|
|
|
|
|
value := optionDiagnostic(CodeOptionsInvalid, "synthetic imports require an absolute base directory")
|
2026-10-25 13:09:44 +00:00
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
2026-12-05 20:37:05 +00:00
|
|
|
if !filepath.IsAbs(context.BaseDir) || context.Root != "" && !filepath.IsAbs(context.Root) {
|
2026-10-25 13:09:44 +00:00
|
|
|
value := optionDiagnostic(CodeOptionsInvalid, "byte and reader import base and root directories must be absolute")
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
|
|
|
|
var err error
|
|
|
|
|
resolved.basePath, err = canonicalExistingPath(context.BaseDir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
value := importDiagnostic(CodeImportInvalid, "import base directory could not be canonicalized", nil)
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
2026-12-05 20:37:05 +00:00
|
|
|
resolved.rootPath = resolved.basePath
|
|
|
|
|
if context.Root != "" {
|
|
|
|
|
resolved.rootPath, err = canonicalExistingPath(context.Root)
|
|
|
|
|
resolved.importRoot = resolved.rootPath
|
|
|
|
|
}
|
2026-10-25 13:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
|
value := importDiagnostic(CodeImportInvalid, "configuration root could not be canonicalized", nil)
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !containedPath(resolved.rootPath, resolved.basePath) || resolved.mainPath != "" && !containedPath(resolved.rootPath, resolved.mainPath) {
|
|
|
|
|
value := importDiagnostic(CodeImportOutsideRoot, "selected source and import base must stay inside the configuration root", nil)
|
|
|
|
|
return resolvedFilesystemContext{}, &value
|
|
|
|
|
}
|
|
|
|
|
return resolved, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func containsNativePath(root, name string) bool {
|
|
|
|
|
if !fs.ValidPath(name) || !fs.ValidPath(root) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return root == "." || name == root || strings.HasPrefix(name, root+"/")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateResolvedFilesystemDirectories(resolved resolvedFilesystemContext) *Diagnostic {
|
|
|
|
|
for _, directory := range []string{resolved.rootPath, resolved.basePath} {
|
2026-12-05 20:37:05 +00:00
|
|
|
if directory == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-10-25 13:09:44 +00:00
|
|
|
var info fs.FileInfo
|
|
|
|
|
var err error
|
|
|
|
|
if resolved.native != nil {
|
|
|
|
|
info, err = fs.Stat(resolved.native.filesystem, directory)
|
|
|
|
|
} else {
|
|
|
|
|
info, err = os.Stat(directory)
|
|
|
|
|
}
|
|
|
|
|
if err != nil || !info.IsDir() {
|
|
|
|
|
value := importDiagnostic(CodeImportInvalid, "configuration root and import base must be directories", nil)
|
|
|
|
|
return &value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func openResolvedFilesystemAccess(resolved resolvedFilesystemContext) (*filesystemAccess, error) {
|
|
|
|
|
if resolved.native != nil {
|
|
|
|
|
access := newNativeAccess(resolved.native.filesystem, resolved.rootPath, resolved.native.token)
|
|
|
|
|
access.label = resolved.native.label
|
|
|
|
|
return access, nil
|
|
|
|
|
}
|
2026-12-05 20:38:31 +00:00
|
|
|
access, err := openHostAccess(resolved.importRoot)
|
2026-10-25 13:09:44 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
if resolved.hostToken != nil {
|
|
|
|
|
access.token = resolved.hostToken
|
|
|
|
|
}
|
|
|
|
|
return access, nil
|
|
|
|
|
}
|