// findImportPath takes a absolute directory and returns the import path and go path.
509980
func (ctx *Context) findImportPath(dir string) (importPath, gopath string, err error) {
	dirResolved, err := filepath.EvalSymlinks(dir)
	if err != nil {
		return "", "", err
	}
	dirs := make([]string, 1)
	dirs = append(dirs, dir)
	if dir != dirResolved {
		dirs = append(dirs, dirResolved)
	}

	for _, gopath := range ctx.GopathList {
		for _, dir := range dirs {
			if pathos.FileHasPrefix(dir, gopath) || pathos.FileStringEquals(dir, gopath) {
				importPath = pathos.FileTrimPrefix(dir, gopath)
				importPath = pathos.SlashToImportPath(importPath)
				return importPath, gopath, nil
			}
		}
	}
	return "", "", ErrNotInGOPATH{dir}
}
664341
func LocatePackage(path string) (string, error) {
	p, err := build.Default.Import(path, "", build.FindOnly)
	if err != nil {
		return "", errors.Wrap(err, "LocatePackage: failed to find import")
	}

	cwd, err := os.Getwd()
	if err != nil {
		return "", errors.Wrap(err, "LocatePackage: could not get working directory")
	}

	p.Dir, err = filepath.Rel(cwd, p.Dir)
	if err != nil {
		return "", errors.Wrap(err, "LocatePackage: could not construct relative path")
	}

	return p.Dir, nil
}
1252791
func (a *API) AddImport(v string) error {
	a.imports[v] = true
	return nil
}
257136
func (s *CreateFileSystemLustreConfiguration) SetExportPath(v string) *CreateFileSystemLustreConfiguration {
	s.ExportPath = &v
	return s
}
240169
func (r *runtimeVerbose) pathFromGopath(path string) string {
	return filepath.Dir(path[len(r.Gopath)+len("/src/"):])
}
398660
func (pc *processContainer) processPath(path string, info os.FileInfo, err error) error {
	if err != nil {
		utils.Logger.Error("Error scanning app source:", "error", err)
		return nil
	}

	if !info.IsDir() || info.Name() == "tmp" {
		return nil
	}

	// Get the import path of the package.
	pkgImportPath := pc.rootImportPath
	if pc.root != path {
		pkgImportPath = pc.rootImportPath + "/" + filepath.ToSlash(path[len(pc.root)+1:])
	}

	// Parse files within the path.
	var pkgs map[string]*ast.Package
	fset := token.NewFileSet()
	pkgs, err = parser.ParseDir(
		fset,
		path,
		func(f os.FileInfo) bool {
			return !f.IsDir() && !strings.HasPrefix(f.Name(), ".") && strings.HasSuffix(f.Name(), ".go")
		},
		0)

	if err != nil {
		if errList, ok := err.(scanner.ErrorList); ok {
			var pos = errList[0].Pos
			newError := &utils.Error{
				SourceType:  ".go source",
				Title:       "Go Compilation Error",
				Path:        pos.Filename,
				Description: errList[0].Msg,
				Line:        pos.Line,
				Column:      pos.Column,
				SourceLines: utils.MustReadLines(pos.Filename),
			}

			errorLink := pc.paths.Config.StringDefault("error.link", "")
			if errorLink != "" {
				newError.SetLink(errorLink)
			}
			return newError
		}

		// This is exception, err already checked above. Here just a print
		ast.Print(nil, err)
		utils.Logger.Fatal("Failed to parse dir", "error", err)
	}

	// Skip "main" packages.
	delete(pkgs, "main")

	// Ignore packages that end with _test
	// These cannot be included in source code that is not generated specifically as a test
	for i := range pkgs {
		if len(i) > 6 {
			if string(i[len(i)-5:]) == "_test" {
				delete(pkgs, i)
			}
		}
	}

	// If there is no code in this directory, skip it.
	if len(pkgs) == 0 {
		return nil
	}

	// There should be only one package in this directory.
	if len(pkgs) > 1 {
		for i := range pkgs {
			println("Found package ", i)
		}
		utils.Logger.Fatal("Most unexpected! Multiple packages in a single directory:", "packages", pkgs)
	}

	var pkg *ast.Package
	for _, v := range pkgs {
		pkg = v
	}

	if pkg != nil {
		pc.srcInfo = appendSourceInfo(pc.srcInfo, processPackage(fset, pkgImportPath, path, pkg))
	} else {
		utils.Logger.Info("Ignoring package, because it contained no packages", "path", path)
	}
	return nil
}
1097070
func libImportPath(rootImportPath, typ string, libRootURLs []string) string {
	// all library use '.',
	// return nothing if it is not a library
	if strings.Index(typ, ".") < 0 {
		return ""
	}

	if strings.Index(typ, "json.RawMessage") >= 0 {
		return `"encoding/json"`
	}

	// library name in the current document
	libName := strings.Split(typ, ".")[0]

	if libName == "goraml" { // special package name, reserved for goraml
		return fmt.Sprintf(`"%s"`, joinImportPath(rootImportPath, "goraml"))
	}

	// raml file of this lib
	libDir, libFile := globAPIDef.FindLibFile(commons.DenormalizePkgName(libName))

	if libFile == "" {
		return ""
	}

	libPath := libraries.JoinPath(libDir, libFile, libRootURLs)

	importPath := joinImportPath(rootImportPath, path.Join(goLibPackageDir(libName, libPath), typeDir))
	return aliasLibTypeImportPath(importPath)
}
182327
func (d *FileDescriptor) goPackageOption() (impPath GoImportPath, pkg GoPackageName, ok bool) {
	opt := d.GetOptions().GetGoPackage()
	if opt == "" {
		return "", "", false
	}
	// A semicolon-delimited suffix delimits the import path and package name.
	sc := strings.Index(opt, ";")
	if sc >= 0 {
		return GoImportPath(opt[:sc]), cleanPackageName(opt[sc+1:]), true
	}
	// The presence of a slash implies there's an import path.
	slash := strings.LastIndex(opt, "/")
	if slash >= 0 {
		return GoImportPath(opt), cleanPackageName(opt[slash+1:]), true
	}
	return "", cleanPackageName(opt), true
}
33955
func ModuleFromPath(path string, addGopathToPath bool) (module *Module) {
	path = filepath.ToSlash(path)
	gopathList := filepath.SplitList(build.Default.GOPATH)
	// Strip away the vendor folder
	if i := strings.Index(path, "/vendor/"); i > 0 {
		path = path[i+len("vendor/"):]
	}

	// See if the path exists in the module based
	for i := range Modules {
		if addGopathToPath {
			for _, gopath := range gopathList {
				if strings.Contains(filepath.ToSlash(filepath.Clean(filepath.Join(gopath, "src", path))), Modules[i].Path) {
					module = Modules[i]
					break
				}
			}
		} else {
			if strings.Contains(path, Modules[i].ImportPath) {
				module = Modules[i]
				break
			}

		}

		if module != nil {
			break
		}
	}
	// Default to the app module if not found
	if module == nil {
		module = appModule
	}
	return
}
516171
func FindSrcPaths(appImportPath, revelImportPath string, packageResolver func(pkgName string) error) (appSourcePath, revelSourcePath string, err error) {
	var (
		gopaths = filepath.SplitList(build.Default.GOPATH)
		goroot  = build.Default.GOROOT
	)

	if len(gopaths) == 0 {
		err = errors.New("GOPATH environment variable is not set. " +
			"Please refer to http://golang.org/doc/code.html to configure your Go environment.")
		return
	}

	if ContainsString(gopaths, goroot) {
		err = fmt.Errorf("GOPATH (%s) must not include your GOROOT (%s). "+
			"Please refer to http://golang.org/doc/code.html to configure your Go environment. ",
			build.Default.GOPATH, goroot)
		return

	}

	appPkgDir := ""
	appPkgSrcDir := ""
	if len(appImportPath)>0 {
		Logger.Info("Seeking app package","app",appImportPath)
		appPkg, err := build.Import(appImportPath, "", build.FindOnly)
		if err != nil {
			err = fmt.Errorf("Failed to import " + appImportPath + " with error %s", err.Error())
			return "","",err
		}
		appPkgDir,appPkgSrcDir =appPkg.Dir, appPkg.SrcRoot
	}
	Logger.Info("Seeking remote package","using",appImportPath, "remote",revelImportPath)
	revelPkg, err := build.Default.Import(revelImportPath, appPkgDir, build.FindOnly)
	if err != nil {
		Logger.Info("Resolved called Seeking remote package","using",appImportPath, "remote",revelImportPath)
		packageResolver(revelImportPath)
		revelPkg, err = build.Import(revelImportPath, appPkgDir, build.FindOnly)
		if err != nil {
			err = fmt.Errorf("Failed to find Revel with error: %s", err.Error())
			return
		}
	}

	revelSourcePath, appSourcePath = revelPkg.Dir[:len(revelPkg.Dir)-len(revelImportPath)], appPkgSrcDir
	return
}
932178
func (importer *MemoryImporter) Import(importedFrom, importedPath string) (contents Contents, foundAt string, err error) {
	if content, ok := importer.Data[importedPath]; ok {
		return content, importedPath, nil
	}
	return Contents{}, "", fmt.Errorf("import not available %v", importedPath)
}
65053
func DefaultImportPathToName(path, srcDir string) (string, error) {
	if path == "C" {
		return "C", nil
	}
	pkg, err := build.Default.Import(path, srcDir, 0)
	return pkg.Name, err
}
583046
func findTypeDef(importPath, typeName string) (*ast.TypeSpec, error) {
	cwd, err := os.Getwd()
	if err != nil {
		return nil, err
	}

	conf := loader.Config{
		ParserMode: goparser.SpuriousErrors,
		Cwd:        cwd,
	}

	conf.Import(importPath)

	lprog, err := conf.Load()
	if err != nil {
		return nil, err
	}

	// If the pkg is vendored, the actual pkg path is going to resemble
	// something like "{importPath}/vendor/{importPath}"
	for k := range lprog.AllPackages {
		realPkgPath := k.Path()

		if strings.Contains(realPkgPath, "vendor/"+importPath) {
			importPath = realPkgPath
		}
	}

	pkgInfo := lprog.Package(importPath)

	if pkgInfo == nil {
		return nil, errors.New("package was nil")
	}

	// TODO: possibly cache pkgInfo since it's an expensive operation

	for i := range pkgInfo.Files {
		for _, astDeclaration := range pkgInfo.Files[i].Decls {
			if generalDeclaration, ok := astDeclaration.(*ast.GenDecl); ok && generalDeclaration.Tok == token.TYPE {
				for _, astSpec := range generalDeclaration.Specs {
					if typeSpec, ok := astSpec.(*ast.TypeSpec); ok {
						if typeSpec.Name.String() == typeName {
							return typeSpec, nil
						}
					}
				}
			}
		}
	}
	return nil, errors.New("type spec not found")
}
493117
func guessImportPath(importPath string) (*Directory, error) {
	if !strings.Contains(importPath, ".git") {
		// Assume GitHub-like where two path elements is the project
		// root.
		parts := strings.SplitN(importPath, "/", 4)
		if len(parts) < 3 {
			return nil, fmt.Errorf("invalid GitHub-like import path: %q", importPath)
		}
		repo := parts[0] + "/" + parts[1] + "/" + parts[2]
		return &Directory{
			ImportPath:  importPath,
			ProjectRoot: repo,
			CloneURL:    "http://" + repo,
			VCS:         "git",
		}, nil
	}

	// TODO(slimsag): We assume that .git only shows up
	// once in the import path. Not always true, but generally
	// should be in 99% of cases.
	split := strings.Split(importPath, ".git")
	if len(split) != 2 {
		return nil, fmt.Errorf("expected one .git in %q", importPath)
	}

	return &Directory{
		ImportPath:  importPath,
		ProjectRoot: split[0] + ".git",
		CloneURL:    "http://" + split[0] + ".git",
		VCS:         "git",
	}, nil
}
1348547
func (conf *Config) GetInitPath() string {
	conf.Lock()
	defer conf.Unlock()
	if conf.InitPath != "" {
		return conf.InitPath
	}
	if conf.DefaultInitBinary != "" {
		return conf.DefaultInitBinary
	}
	return DefaultInitBinary
}
1249545
func (s *DataRepositoryConfiguration) SetExportPath(v string) *DataRepositoryConfiguration {
	s.ExportPath = &v
	return s
}
851144
func importPaths(args []string) []string {
	args = importPathsNoDotExpansion(args)
	var out []string
	for _, a := range args {
		if strings.Contains(a, "...") {
			if build.IsLocalImport(a) {
				out = append(out, allPackagesInFS(a)...)
			} else {
				out = append(out, allPackages(a)...)
			}
			continue
		}
		out = append(out, a)
	}
	return out
}