Skip to content

Commit

Permalink
build: Try to get and use working directory, when possible.
Browse files Browse the repository at this point in the history
Previously, in build.Import, the srcDir argument to importWithSrcDir
call was always empty string.

Ideally, the Import method should have a srcDir parameter and pass it
onwards. Then the caller (gopherjs tool, for example) can provide the
working directory, if available. However, this is an exported method
and changing its signature at this time is hard. Settle for an
intermediate solution of try to figure out working directory inside
Import, but continue to use empty string on failure (such as if in
GOARCH=js mode, e.g., inside browser).

This will be used by the gopherjs tool in the next commit to be able to
resolve relative import paths, such as "." or "./foo/bar".

The future plan is to bring all the "supported" code into gopherjs repo
for convenience, mark the build API as unstable and make breaking API
changes to bring it up to speed.

Helps #302.
  • Loading branch information
dmitshur committed May 29, 2017
1 parent 42c113c commit 25c3660
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion build/build.go
Expand Up @@ -62,7 +62,14 @@ func NewBuildContext(installSuffix string, buildTags []string) *build.Context {
// If an error occurs, Import returns a non-nil error and a nil
// *PackageData.
func Import(path string, mode build.ImportMode, installSuffix string, buildTags []string) (*PackageData, error) {
return importWithSrcDir(path, "", mode, installSuffix, buildTags)
wd, err := os.Getwd()
if err != nil {
// Getwd may fail if we're in GOARCH=js mode. That's okay, handle
// it by falling back to empty working directory. It just means
// Import will not be able to resolve relative import paths.
wd = ""
}
return importWithSrcDir(path, wd, mode, installSuffix, buildTags)
}

func importWithSrcDir(path string, srcDir string, mode build.ImportMode, installSuffix string, buildTags []string) (*PackageData, error) {
Expand Down

0 comments on commit 25c3660

Please sign in to comment.