normalizePath function

String normalizePath(
  1. String filePath
)

Normalize a file path by collapsing . and .. segments.

FLC paths from SourceTraceRegistry often contain .dart_tool/../lib/... which needs collapsing before resolution.

Implementation

String normalizePath(String filePath) {
  final isAbsolute = filePath.startsWith('/');
  final parts = filePath.split('/');
  final resolved = <String>[];
  for (final part in parts) {
    if (part == '.' || part.isEmpty) {
      continue;
    } else if (part == '..' && resolved.isNotEmpty && resolved.last != '..') {
      resolved.removeLast();
    } else {
      resolved.add(part);
    }
  }
  final joined = resolved.join('/');
  return isAbsolute ? '/$joined' : joined;
}