Skip to content

Commit

Permalink
Remove GOROOT and GOPATH fields from build.Options.
Browse files Browse the repository at this point in the history
They were used as a side channel to extract GOROOT and GOPATH from
`build.Session`, which is an incorrect use of options. Eventually all
logic that requires these paths should be moved to the `build` package,
but for now we just provide an additional method to create a source
mapping callback.
  • Loading branch information
nevkontakte committed Apr 19, 2022
1 parent 59958da commit 9813bf2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
30 changes: 14 additions & 16 deletions build/build.go
Expand Up @@ -305,9 +305,6 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke

// Options controls build process behavior.
type Options struct {
// FIXME(nevkontakte): Remove GOROOT and GOPATH from options. They are in Env now.
GOROOT string
GOPATH string
Verbose bool
Quiet bool
Watch bool
Expand Down Expand Up @@ -428,25 +425,20 @@ type Session struct {

// NewSession creates a new GopherJS build session.
func NewSession(options *Options) (*Session, error) {
if options.GOROOT == "" {
options.GOROOT = DefaultGOROOT
}
if options.GOPATH == "" {
options.GOPATH = build.Default.GOPATH
}
options.Verbose = options.Verbose || options.Watch

// Go distribution version check.
if err := compiler.CheckGoVersion(options.GOROOT); err != nil {
return nil, err
}

s := &Session{
options: options,
UpToDateArchives: make(map[string]*compiler.Archive),
}
s.xctx = NewBuildContext(s.InstallSuffix(), s.options.BuildTags)
env := s.xctx.Env()

// Go distribution version check.
if err := compiler.CheckGoVersion(env.GOROOT); err != nil {
return nil, err
}

s.buildCache = cache.BuildCache{
GOOS: env.GOOS,
GOARCH: env.GOARCH,
Expand Down Expand Up @@ -486,7 +478,7 @@ func (s *Session) InstallSuffix() string {

// GoRelease returns Go release version this session is building with.
func (s *Session) GoRelease() string {
return compiler.GoRelease(s.options.GOROOT)
return compiler.GoRelease(s.xctx.Env().GOROOT)
}

// BuildFiles passed to the GopherJS tool as if they were a package.
Expand Down Expand Up @@ -657,6 +649,12 @@ func (s *Session) ImportResolverFor(pkg *PackageData) func(string) (*compiler.Ar
}
}

// SourceMappingCallback returns a call back for compiler.SourceMapFilter
// configured for the current build session.
func (s *Session) SourceMappingCallback(m *sourcemap.Map) func(generatedLine, generatedColumn int, originalPos token.Position) {
return NewMappingCallback(m, s.xctx.Env().GOROOT, s.xctx.Env().GOPATH, s.options.MapToLocalDisk)
}

// WriteCommandPackage writes the final JavaScript output file at pkgObj path.
func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string) error {
if err := os.MkdirAll(filepath.Dir(pkgObj), 0777); err != nil {
Expand All @@ -682,7 +680,7 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string)
fmt.Fprintf(codeFile, "//# sourceMappingURL=%s.map\n", filepath.Base(pkgObj))
}()

sourceMapFilter.MappingCallback = NewMappingCallback(m, s.options.GOROOT, s.options.GOPATH, s.options.MapToLocalDisk)
sourceMapFilter.MappingCallback = s.SourceMappingCallback(m)
}

deps, err := compiler.ImportDependencies(archive, func(path string) (*compiler.Archive, error) {
Expand Down
2 changes: 1 addition & 1 deletion tool.go
Expand Up @@ -705,7 +705,7 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {

sourceMapFilter := &compiler.SourceMapFilter{Writer: buf}
m := &sourcemap.Map{File: base + ".js"}
sourceMapFilter.MappingCallback = gbuild.NewMappingCallback(m, fs.options.GOROOT, fs.options.GOPATH, fs.options.MapToLocalDisk)
sourceMapFilter.MappingCallback = s.SourceMappingCallback(m)

deps, err := compiler.ImportDependencies(archive, s.BuildImportPath)
if err != nil {
Expand Down

0 comments on commit 9813bf2

Please sign in to comment.