From 106fcb45fd5f8b9ebde8fcf04030b5626c4a73ed Mon Sep 17 00:00:00 2001 From: Vladyslav Kuksiuk Date: Thu, 23 Apr 2026 14:52:34 +0200 Subject: [PATCH 1/3] Provide smart path resolving. --- fragmentation/fragment_file.go | 75 ++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/fragmentation/fragment_file.go b/fragmentation/fragment_file.go index 626a8d8..e29f0e1 100644 --- a/fragmentation/fragment_file.go +++ b/fragmentation/fragment_file.go @@ -107,17 +107,22 @@ func (f FragmentFile) Content() ([]string, error) { } if !isPathFileExits { + codeFileReference, err := f.codeFileReference() + if err != nil { + return nil, err + } + if f.FragmentName != "" { if f.FragmentName == "_default" { - return nil, fmt.Errorf("code file `%s` not found", f.CodePath) + return nil, fmt.Errorf("code file `%s` not found", codeFileReference) } return nil, fmt.Errorf( - "fragment `%s` from code file `%s` not found", f.FragmentName, f.CodePath, + "fragment `%s` from code file `%s` not found", f.FragmentName, codeFileReference, ) } return nil, fmt.Errorf( - "code file `%s` fragment not found", - f.CodePath, + "code file %s fragment not found", + codeFileReference, ) } @@ -147,6 +152,68 @@ func (f FragmentFile) absolutePath() string { return filepath.Join(fragmentsAbsDir, filename+fileExtension) } +// Builds a user-facing reference to the original code file for error messages. +// +// If the source file exists, returns its absolute `file://` URL. If the file path uses a named +// code root and the resolved file does not exist, returns both the prefixed path and the expanded +// absolute path. +func (f FragmentFile) codeFileReference() (string, error) { + originalCodePath, isPrefixed := f.originalCodePath() + if originalCodePath == "" { + return fmt.Sprintf("%s", f.CodePath), nil + } + + exists, err := files.IsFileExist(originalCodePath) + if err != nil { + return "", err + } + if exists { + return "file://" + originalCodePath, nil + } + if isPrefixed { + return fmt.Sprintf("%s (%s)", f.CodePath, originalCodePath), nil + } + + return fmt.Sprintf("%s", originalCodePath), nil +} + +// Resolves the original source file path from the fragment's code path. +// +// Returns the absolute path to the source file and reports whether the input path used a named +// code-root prefix such as `$runtime/...`. +func (f FragmentFile) originalCodePath() (string, bool) { + normalizedPath := filepath.ToSlash(filepath.Clean(f.CodePath)) + + if strings.HasPrefix(normalizedPath, NamedPathPrefix) { + withoutPrefix := strings.TrimPrefix(normalizedPath, NamedPathPrefix) + codeRootName, relativePath, _ := strings.Cut(withoutPrefix, "/") + + for _, codeRoot := range f.Configuration.CodeRoots { + if strings.TrimSpace(codeRoot.Name) != codeRootName { + continue + } + + absoluteRoot, err := filepath.Abs(codeRoot.Path) + if err != nil { + panic(err) + } + + return filepath.Join(absoluteRoot, filepath.FromSlash(relativePath)), true + } + } + + if len(f.Configuration.CodeRoots) == 1 { + absoluteRoot, err := filepath.Abs(f.Configuration.CodeRoots[0].Path) + if err != nil { + panic(err) + } + + return filepath.Join(absoluteRoot, filepath.FromSlash(normalizedPath)), false + } + + return "", false +} + // Calculates and returns a hash string for FragmentFile. // // Since fragments which have the same name unite into one fragment with multiple partitions, From a417b10e7aa44a8ece162ff5e761d1bdf9efc144 Mon Sep 17 00:00:00 2001 From: Vladyslav Kuksiuk Date: Thu, 23 Apr 2026 15:05:55 +0200 Subject: [PATCH 2/3] Add `code root not found` error. --- fragmentation/fragment_file.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/fragmentation/fragment_file.go b/fragmentation/fragment_file.go index e29f0e1..9eb2893 100644 --- a/fragmentation/fragment_file.go +++ b/fragmentation/fragment_file.go @@ -158,7 +158,10 @@ func (f FragmentFile) absolutePath() string { // code root and the resolved file does not exist, returns both the prefixed path and the expanded // absolute path. func (f FragmentFile) codeFileReference() (string, error) { - originalCodePath, isPrefixed := f.originalCodePath() + originalCodePath, isPrefixed, err := f.originalCodePath() + if err != nil { + return "", err + } if originalCodePath == "" { return fmt.Sprintf("%s", f.CodePath), nil } @@ -181,7 +184,9 @@ func (f FragmentFile) codeFileReference() (string, error) { // // Returns the absolute path to the source file and reports whether the input path used a named // code-root prefix such as `$runtime/...`. -func (f FragmentFile) originalCodePath() (string, bool) { +// +// Returns an error if the path uses a named code root that is not present in the configuration. +func (f FragmentFile) originalCodePath() (string, bool, error) { normalizedPath := filepath.ToSlash(filepath.Clean(f.CodePath)) if strings.HasPrefix(normalizedPath, NamedPathPrefix) { @@ -198,8 +203,11 @@ func (f FragmentFile) originalCodePath() (string, bool) { panic(err) } - return filepath.Join(absoluteRoot, filepath.FromSlash(relativePath)), true + return filepath.Join(absoluteRoot, filepath.FromSlash(relativePath)), true, nil } + + return "", true, fmt.Errorf("code root with name `%s` not found for path `%s`", + codeRootName, f.CodePath) } if len(f.Configuration.CodeRoots) == 1 { @@ -208,10 +216,10 @@ func (f FragmentFile) originalCodePath() (string, bool) { panic(err) } - return filepath.Join(absoluteRoot, filepath.FromSlash(normalizedPath)), false + return filepath.Join(absoluteRoot, filepath.FromSlash(normalizedPath)), false, nil } - return "", false + return "", false, nil } // Calculates and returns a hash string for FragmentFile. From 2e8c9d953ecd1c997fca7e29c2df2554ec3939be Mon Sep 17 00:00:00 2001 From: Vladyslav Kuksiuk Date: Thu, 30 Apr 2026 09:40:10 +0200 Subject: [PATCH 3/3] Improve error handling. --- fragmentation/fragment_file.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/fragmentation/fragment_file.go b/fragmentation/fragment_file.go index 9eb2893..c3b465e 100644 --- a/fragmentation/fragment_file.go +++ b/fragmentation/fragment_file.go @@ -163,7 +163,7 @@ func (f FragmentFile) codeFileReference() (string, error) { return "", err } if originalCodePath == "" { - return fmt.Sprintf("%s", f.CodePath), nil + return f.CodePath, nil } exists, err := files.IsFileExist(originalCodePath) @@ -177,7 +177,7 @@ func (f FragmentFile) codeFileReference() (string, error) { return fmt.Sprintf("%s (%s)", f.CodePath, originalCodePath), nil } - return fmt.Sprintf("%s", originalCodePath), nil + return originalCodePath, nil } // Resolves the original source file path from the fragment's code path. @@ -198,12 +198,8 @@ func (f FragmentFile) originalCodePath() (string, bool, error) { continue } - absoluteRoot, err := filepath.Abs(codeRoot.Path) - if err != nil { - panic(err) - } - - return filepath.Join(absoluteRoot, filepath.FromSlash(relativePath)), true, nil + return filepath.Join(resolvedRootPath(codeRoot.Path), filepath.FromSlash(relativePath)), + true, nil } return "", true, fmt.Errorf("code root with name `%s` not found for path `%s`", @@ -211,17 +207,27 @@ func (f FragmentFile) originalCodePath() (string, bool, error) { } if len(f.Configuration.CodeRoots) == 1 { - absoluteRoot, err := filepath.Abs(f.Configuration.CodeRoots[0].Path) - if err != nil { - panic(err) - } - - return filepath.Join(absoluteRoot, filepath.FromSlash(normalizedPath)), false, nil + return filepath.Join( + resolvedRootPath(f.Configuration.CodeRoots[0].Path), + filepath.FromSlash(normalizedPath), + ), false, nil } return "", false, nil } +// Resolves the given path to an absolute path when possible. +// +// If absolute-path resolution fails, returns the original path. +func resolvedRootPath(path string) string { + absolutePath, err := filepath.Abs(path) + if err != nil { + return path + } + + return absolutePath +} + // Calculates and returns a hash string for FragmentFile. // // Since fragments which have the same name unite into one fragment with multiple partitions,