Consolidate selected overlay acquisition

This commit is contained in:
2026-10-25 13:18:54 +00:00
parent 9a64a65b6c
commit b915dd776a
9 changed files with 138 additions and 193 deletions

View File

@@ -12,17 +12,20 @@ func validateFilesystemContext(source Source, context *FilesystemContext, main p
return validateAbsentHostFilesystemContext(context)
}
if nativeContext(source, context) {
access, basePath, mainPath, diagnostic := nativeFilesystemAccess(source, context)
resolved, diagnostic := resolveNativeFilesystemContext(source, context)
if diagnostic != nil {
return diagnostic
}
return validateNativeDirectories(access, basePath, mainPath)
return validateResolvedFilesystemDirectories(resolved)
}
rootPath, _, _, diagnostic := importFilesystem(source, context, main)
resolved, diagnostic := resolveHostFilesystemContext(source, context, main, nil)
if diagnostic != nil {
return diagnostic
}
root, err := os.OpenRoot(rootPath)
if diagnostic := validateResolvedFilesystemDirectories(resolved); diagnostic != nil {
return diagnostic
}
root, err := os.OpenRoot(resolved.rootPath)
if err != nil {
value := globFilesystemDiagnostic(err, "configuration root could not be opened", Location{})
value.Primary = nil
@@ -133,52 +136,6 @@ func nativeContext(source Source, context *FilesystemContext) bool {
return source.kind == sourceFilesystem || context != nil && context.FS != nil
}
// nativeFilesystemAccess resolves namespace context without opening caller
// resources. File inputs always derive their base from the selected file.
func nativeFilesystemAccess(source Source, context *FilesystemContext) (*filesystemAccess, string, string, *Diagnostic) {
resolved, diagnostic := resolveNativeFilesystemContext(source, context)
if diagnostic != nil {
return nil, "", "", diagnostic
}
access, err := openResolvedFilesystemAccess(resolved)
if err != nil {
value := globFilesystemDiagnostic(err, "configuration root could not be opened", Location{})
value.Primary = nil
return nil, "", "", &value
}
return access, resolved.basePath, resolved.mainPath, nil
}
func validateNativeDirectories(access *filesystemAccess, basePath, mainPath string) *Diagnostic {
if !access.contains(basePath) || mainPath != "" && !access.contains(mainPath) {
value := importDiagnostic(CodeImportOutsideRoot, "selected source and import base must stay inside the configuration root", nil)
return &value
}
for _, directory := range []string{access.rootPath, basePath} {
info, err := access.stat(directory)
if err != nil || !info.IsDir() {
value := importDiagnostic(CodeImportInvalid, "configuration root and import base must be directories", nil)
return &value
}
}
return nil
}
func importFilesystem(source Source, context *FilesystemContext, main parsedSource) (rootPath, basePath, mainPath string, diagnostic *Diagnostic) {
if main.source == nil || main.document == nil {
value := importDiagnostic(CodeImportInvalid, "imports require a selected configuration source", nil)
return "", "", "", &value
}
resolved, diagnostic := resolveHostFilesystemContext(source, context, main, nil)
if diagnostic != nil {
return "", "", "", diagnostic
}
if diagnostic := validateResolvedFilesystemDirectories(resolved); diagnostic != nil {
return "", "", "", diagnostic
}
return resolved.rootPath, resolved.basePath, resolved.mainPath, nil
}
func canonicalExistingPath(name string) (string, error) {
absolute, err := filepath.Abs(name)
if err != nil {