10 // FileDetector implements Detector to detect file paths.
11 type FileDetector struct{}
13 func (d *FileDetector) Detect(src, pwd string) (string, bool, error) {
18 if !filepath.IsAbs(src) {
20 return "", true, fmt.Errorf(
21 "relative paths require a module with a pwd")
24 // Stat the pwd to determine if its a symbolic link. If it is,
25 // then the pwd becomes the original directory. Otherwise,
26 // `filepath.Join` below does some weird stuff.
28 // We just ignore if the pwd doesn't exist. That error will be
29 // caught later when we try to use the URL.
30 if fi, err := os.Lstat(pwd); !os.IsNotExist(err) {
34 if fi.Mode()&os.ModeSymlink != 0 {
35 pwd, err = os.Readlink(pwd)
40 // The symlink itself might be a relative path, so we have to
41 // resolve this to have a correctly rooted URL.
42 pwd, err = filepath.Abs(pwd)
49 src = filepath.Join(pwd, src)
52 return fmtFileURL(src), true, nil
55 func fmtFileURL(path string) string {
56 if runtime.GOOS == "windows" {
57 // Make sure we're using "/" on Windows. URLs are "/"-based.
58 path = filepath.ToSlash(path)
59 return fmt.Sprintf("file://%s", path)
62 // Make sure that we don't start with "/" since we add that below.
66 return fmt.Sprintf("file:///%s", path)