Preserve captured filesystem contexts during loading

This commit is contained in:
2026-11-18 00:44:59 +00:00
parent a3c77f32f2
commit e750b6d423
4 changed files with 143 additions and 2 deletions

View File

@@ -17,6 +17,9 @@ type resolvedFilesystemContext struct {
}
func resolveFilesystemContext(source Source, context *FilesystemContext, main parsedSource, hostToken *namespaceToken) (resolvedFilesystemContext, *Diagnostic) {
if source.captured != nil {
return resolveCapturedFilesystemContext(source, context, main, hostToken)
}
if source.kind == sourceNone && context != nil {
return resolveAbsentFilesystemContext(context)
}
@@ -26,6 +29,30 @@ func resolveFilesystemContext(source Source, context *FilesystemContext, main pa
return resolveHostFilesystemContext(source, context, main, hostToken)
}
func resolveCapturedFilesystemContext(source Source, context *FilesystemContext, main parsedSource, hostToken *namespaceToken) (resolvedFilesystemContext, *Diagnostic) {
captured := source.captured
resolved := resolvedFilesystemContext{hostToken: hostToken, rootPath: captured.rootPath, basePath: captured.basePath, mainPath: captured.mainPath}
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
}
// The source record carries the captured canonical main path, so this does
// not follow the original filename again after preparation.
explicit, diagnostic := resolveHostFilesystemContext(source, context, main, hostToken)
if diagnostic != nil || !sameHostPath(explicit.rootPath, resolved.rootPath) || !sameHostPath(explicit.basePath, resolved.basePath) || !sameHostPath(explicit.mainPath, resolved.mainPath) {
value := optionDiagnostic(CodeOptionsInvalid, "prepared source requires a compatible host filesystem context")
return resolvedFilesystemContext{}, &value
}
return resolved, nil
}
func sameHostPath(left, right string) bool {
return containedPath(left, right) && containedPath(right, left)
}
func resolveAbsentFilesystemContext(context *FilesystemContext) (resolvedFilesystemContext, *Diagnostic) {
if context.FS != nil {
return resolveNativeFilesystemContext(Source{kind: sourceNone}, context)