Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Invalid case-insensitive filepath handling #982

Closed
egonelbre opened this issue Mar 22, 2018 · 5 comments
Closed

Invalid case-insensitive filepath handling #982

egonelbre opened this issue Mar 22, 2018 · 5 comments
Milestone

Comments

@egonelbre
Copy link
Contributor

egonelbre commented Mar 22, 2018

Some FileSystems are case-insensitive, mainly Windows, which means using strings.TrimPrefix to find the relative directory or path, will eventually fail. Something like this, will be probably more successful in most cases:

func ImportPath(gopaths []string, targetpath string) (string, error) {
	var err error
	var rel string
	for _, gopath := range gopaths {
		rel, err = filepath.Rel(gopath, targetpath)
		if err == nil {
			return filepath.ToSlash(rel), nil
		}
	}
	return "", err
}

Example in Windows with a case-mismatch:

f:\Go\src\sandbox\buffalo>echo %GOPATH%
F:\Go

f:\Go\src\sandbox\buffalo>buffalo new example
      create  .buffalo.dev.yml
      create  assets/images/logo.svg
      create  assets\css\application.scss
<<< SNIP >>>
      create  public\robots.txt
      create  templates\_flash.html
      create  templates\application.html
      create  templates\index.html
         run  go get -t ./...
can't load package: package sandbox/buffalo/example:
main.go:6:3: invalid import path: "f:/Go/src/sandbox/buffalo/example/actions"
can't load package: package sandbox/buffalo/example/actions:
actions\app.go:11:3: invalid import path: "f:/Go/src/sandbox/buffalo/example/models"
can't load package: package sandbox/buffalo/example/grifts:
grifts\init.go:5:2: invalid import path: "f:/Go/src/sandbox/buffalo/example/actions"
Usage:
  buffalo new [name] [flags]
<<< SNIP >>>
ERRO[0038] Error: exit status 1

And the main.go, app.go, Dockerfile... contain import paths such as:

package main

import (
  "log"

  "f:/Go/src/sandbox/buffalo/example/actions"
)

func main() {
  app := actions.App()
  if err := app.Serve(); err != nil {
    log.Fatal(err)
  }
}

I suspect there are other places that may contain similar mistakes.

@markbates
Copy link
Member

markbates commented Mar 22, 2018 via email

@egonelbre
Copy link
Contributor Author

Tried my best.

I wasn't sure what the policy of error handling is, but I took the following approach:

  1. When there was error handling around, returned the error
  2. When there was no error handling, then fell back to strings.TrimPrefix results.

I'm not sure what the appropriate action should be for paths that are outside of GoPath, or is that handled somewhere further above? In those cases the result from filepath.Rel can give a result such as ../../x/y/z.

@egonelbre
Copy link
Contributor Author

PS: I'm not sure whether I caught them all. There were few strings.TrimPrefix-es that I wasn't sure about, and there might be some dependencies outside of gobuffalo that are still affected.

@egonelbre
Copy link
Contributor Author

Also there might be a better place to put that version of RelativePath with convenient fallback, but I couldn't find a place where to put it.

markbates pushed a commit to gobuffalo/envy that referenced this issue Mar 25, 2018
strings.TrimPrefix can give the wrong result with case-insensitive
paths. filepath.Rel usually works better.
markbates pushed a commit to gobuffalo/packr that referenced this issue Mar 25, 2018
strings.TrimPrefix can give the wrong result with case-insensitive
paths. filepath.Rel usually works better.
@markbates markbates added this to the 0.11.1 milestone Mar 25, 2018
markbates pushed a commit that referenced this issue Mar 25, 2018
strings.TrimPrefix can give the wrong result with case-insensitive
paths. filepath.Rel usually works better.
@markbates
Copy link
Member

Thank you for this!

stanislas-m pushed a commit that referenced this issue May 12, 2018
strings.TrimPrefix can give the wrong result with case-insensitive
paths. filepath.Rel usually works better.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants