dart-lang/path

relative() returns incorrect result

Closed this issue · 7 comments

We're seeing a failure in flutter's tests (flutter/flutter#1709) which looks like a bug in path's relative() function:

from: /var/folders/00/0w978000h01000cxqpysvccm003j4x/T/flutter_toolsZUIVMq
to:   /Users/devoncarew/projects/flutter/flutter/packages

result = path.relative(to, from: from);

result: ../../../../../../Users/devoncarew/projects/flutter/flutter/packages

The relative result isn't correct. It's off by one in the number of times it should traverse up to the parent directory. From flutter_toolsZUIVMq it can only go up 5 times to /, but it has 6 ..'s in the relative result.

nex3 commented

This looks correct to me. It satisfies the invariant that p.join(from, p.relative(path, from: from)) refers to the same path as path.

import 'package:path/path.dart' as p;

void main() {
  var from = "/var/folders/00/0w978000h01000cxqpysvccm003j4x/T/flutter_toolsZUIVMq";
  var path = "/Users/devoncarew/projects/flutter/flutter/packages";

  print(p.normalize(from, p.join(p.relative(path, from: from))));
  // => /Users/devoncarew/projects/flutter/flutter/packages
}

Keep in mind that the from argument takes the place of the current directory. So if flutter_toolsZUIVMq is a file and you want a relative path that is valid within that file, from should be /var/folders/00/0w978000h01000cxqpysvccm003j4x/T.

flutter_toolsZUIVMq is a directory - we're creating a pubspec file in there and trying to write a relative path from there to the flutter package. (flutter_toolsZUIVMq/pubspec.yaml)

nex3 commented

In that case, the path you listed should work. I count six ..s, which matches the six path segments in from.

But from flutter_toolsZUIVMq, it's only 5 segments up.

What's odd is that this test works on linux. And it does look like the relative path being created in the pubspec is bad.

nex3 commented

But from flutter_toolsZUIVMq, it's only 5 segments up.

Not if it's a directory and you're resolving a path inside it. You still have to .. up through flutter_toolsZUIVMq.

What's odd is that this test works on linux. And it does look like the relative path being created in the pubspec is bad.

I suspect the real issue here is OS X's symlinking of the /tmp directory. Relative paths that move up out of symlinked directories are unreliable; they can be resolved differently depending on what the resolving entity thinks the current working directory's path is.

Ah, something to look into. I'll see if canonicalizing the paths helps.