@@ -613,11 +613,27 @@ type serveCommandFileSystem struct {
613613}
614614
615615func (fs serveCommandFileSystem ) Open (requestName string ) (http.File , error ) {
616- name := path .Join (fs .serveRoot , requestName [1 :]) // requestName[0] == '/'
617- log .Printf ("Request: %s" , name )
616+ requestName = strings .TrimPrefix (requestName , `/` ) // Strip leading '/'
617+ name := path .Join (fs .serveRoot , requestName )
618+ log .WithField (`request` , requestName ).
619+ Print (`Request` )
620+
621+ // Check if the file is reachable from the serve root.
622+ if f , err := http .Dir (fs .serveRoot ).Open (requestName ); err == nil {
623+ log .WithField (`request` , requestName ).
624+ Print (`Found in serve root` )
625+ return f , nil
626+ }
627+
628+ if strings .HasPrefix (requestName , `.well-known/` ) {
629+ // Ignore ".well-known/" requests since they are probes.
630+ // See https://en.wikipedia.org/wiki/Well-known_URI
631+ return nil , os .ErrNotExist
632+ }
618633
619634 dir , file := path .Split (name )
620- base := path .Base (dir ) // base is parent folder name, which becomes the output file name.
635+ dir = strings .TrimSuffix (dir , `/` ) // Strip tailing '/'
636+ base := path .Base (dir ) // base is parent folder name, which becomes the output file name.
621637
622638 isPkg := file == base + ".js"
623639 isMap := file == base + ".js.map"
@@ -627,9 +643,42 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
627643 // TODO(dmitshur): might be possible to get a single session to detect changes to source code on disk
628644 s , err := gbuild .NewSession (fs .options )
629645 if err != nil {
646+ log .WithField (`request` , requestName ).WithError (err ).
647+ Error (`Failed to creates a session` )
630648 return nil , err
631649 }
632650
651+ // Check if the file is reachable from the Go path.
652+ if f , err := http .Dir (path .Join (s .XContext ().Env ().GOPATH , `src` )).Open (requestName ); err == nil {
653+ log .WithField (`request` , requestName ).
654+ Print (`Found in Go path` )
655+ return f , nil
656+ }
657+
658+ // Check if the file is reachable from the Go root.
659+ if f , err := http .Dir (path .Join (s .XContext ().Env ().GOROOT , `src` )).Open (requestName ); err == nil {
660+ log .WithField (`request` , requestName ).
661+ Print (`Found in Go root` )
662+ return f , nil
663+ }
664+
665+ // Check if the request's dir is an import path.
666+ if pkg , err := s .XContext ().Import (dir , fs .serveRoot , build .FindOnly ); err == nil {
667+ f , err := http .Dir (pkg .Dir ).Open (file )
668+ if err == nil {
669+ log .WithField (`request` , requestName ).
670+ Print (`Found in import path` )
671+ return f , nil
672+ }
673+
674+ if ! os .IsNotExist (err ) {
675+ log .WithField (`request` , requestName ).
676+ WithError (err ).
677+ Error (`Failed to open file in import path` )
678+ return nil , err
679+ }
680+ }
681+
633682 if isPkg || isMap || isIndex {
634683 // If we're going to be serving our special files, make sure there's a Go command in this folder.
635684 pkg , err := gbuild .Import (path .Dir (name ), 0 , s .InstallSuffix (), fs .options .BuildTags )
@@ -646,6 +695,10 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
646695 err := func () error {
647696 archive , err := s .BuildProject (pkg )
648697 if err != nil {
698+ log .WithField (`request` , requestName ).
699+ WithField (`package` , pkg .ImportPath ).
700+ WithError (err ).
701+ Error (`Failed to build project` )
649702 return err
650703 }
651704
@@ -654,9 +707,17 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
654707
655708 deps , err := compiler .ImportDependencies (archive , s .ImportResolverFor ("" ))
656709 if err != nil {
710+ log .WithField (`request` , requestName ).
711+ WithField (`package` , pkg .ImportPath ).
712+ WithError (err ).
713+ Error (`Failed to import dependencies` )
657714 return err
658715 }
659716 if err := compiler .WriteProgramCode (deps , sourceMapFilter , s .GoRelease ()); err != nil {
717+ log .WithField (`request` , requestName ).
718+ WithField (`package` , pkg .ImportPath ).
719+ WithError (err ).
720+ Error (`Failed to write program code` )
660721 return err
661722 }
662723
@@ -671,30 +732,42 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
671732 if err != nil {
672733 buf = browserErrors
673734 }
735+ log .WithField (`request` , requestName ).
736+ Print (`Created faked JS file for package` )
674737 return newFakeFile (base + ".js" , buf .Bytes ()), nil
675738
676739 case isMap :
677740 if content , ok := fs .sourceMaps [name ]; ok {
741+ log .WithField (`request` , requestName ).
742+ Print (`Found source map for faked JS file` )
678743 return newFakeFile (base + ".js.map" , content ), nil
679744 }
680745 }
681746 }
682747
683748 // First try to serve the request with a root prefix supplied in the CLI.
684749 if f , err := fs .serveSourceTree (s .XContext (), name ); err == nil {
750+ log .WithField (`request` , requestName ).
751+ Print (`Found with root prefix` )
685752 return f , nil
686753 }
687754
688755 // If that didn't work, try without the prefix.
689756 if f , err := fs .serveSourceTree (s .XContext (), requestName ); err == nil {
757+ log .WithField (`request` , requestName ).
758+ Print (`Found without prefix` )
690759 return f , nil
691760 }
692761
693762 if isIndex {
694763 // If there was no index.html file in any dirs, supply our own.
764+ log .WithField (`request` , requestName ).
765+ Print (`Created faked index.html file` )
695766 return newFakeFile ("index.html" , []byte (`<html><head><meta charset="utf-8"><script src="` + base + `.js"></script></head><body></body></html>` )), nil
696767 }
697768
769+ log .WithField (`request` , requestName ).
770+ Error (`Failed to find` )
698771 return nil , os .ErrNotExist
699772}
700773
0 commit comments