Skip to content

Commit 25c3660

Browse files
committed
build: Try to get and use working directory, when possible.
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.
1 parent 42c113c commit 25c3660

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

build/build.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ func NewBuildContext(installSuffix string, buildTags []string) *build.Context {
6262
// If an error occurs, Import returns a non-nil error and a nil
6363
// *PackageData.
6464
func Import(path string, mode build.ImportMode, installSuffix string, buildTags []string) (*PackageData, error) {
65-
return importWithSrcDir(path, "", mode, installSuffix, buildTags)
65+
wd, err := os.Getwd()
66+
if err != nil {
67+
// Getwd may fail if we're in GOARCH=js mode. That's okay, handle
68+
// it by falling back to empty working directory. It just means
69+
// Import will not be able to resolve relative import paths.
70+
wd = ""
71+
}
72+
return importWithSrcDir(path, wd, mode, installSuffix, buildTags)
6673
}
6774

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

0 commit comments

Comments
 (0)