@@ -125,18 +125,13 @@ func (s *snapshot) FileSet() *token.FileSet {
125
125
return s .view .session .cache .fset
126
126
}
127
127
128
- // config returns a *packages.Config with the working directory set to the
129
- // view's root.
130
- func (s * snapshot ) config (ctx context.Context ) * packages.Config {
131
- return s .configWithDir (ctx , s .view .rootURI .Filename ())
132
- }
133
-
134
- // configWithDir returns the configuration used for the snapshot's interaction
135
- // with the go/packages API. It uses the given working directory.
128
+ // config returns the configuration used for the snapshot's interaction with
129
+ // the go/packages API. It uses the given working directory.
130
+ //
136
131
// TODO(rstambler): go/packages requires that we do not provide overlays for
137
132
// multiple modules in on config, so buildOverlay needs to filter overlays by
138
133
// module.
139
- func (s * snapshot ) configWithDir (ctx context.Context , dir string ) * packages.Config {
134
+ func (s * snapshot ) config (ctx context.Context , dir string ) * packages.Config {
140
135
s .view .optionsMu .Lock ()
141
136
env , buildFlags := s .view .envLocked ()
142
137
verboseOutput := s .view .options .VerboseOutput
@@ -174,8 +169,8 @@ func (s *snapshot) configWithDir(ctx context.Context, dir string) *packages.Conf
174
169
return cfg
175
170
}
176
171
177
- func (s * snapshot ) RunGoCommandDirect (ctx context.Context , verb string , args []string ) error {
178
- cfg := s .config (ctx )
172
+ func (s * snapshot ) RunGoCommandDirect (ctx context.Context , wd , verb string , args []string ) error {
173
+ cfg := s .config (ctx , wd )
179
174
_ , runner , inv , cleanup , err := s .goCommandInvocation (ctx , cfg , false , verb , args )
180
175
if err != nil {
181
176
return err
@@ -186,11 +181,6 @@ func (s *snapshot) RunGoCommandDirect(ctx context.Context, verb string, args []s
186
181
return err
187
182
}
188
183
189
- func (s * snapshot ) RunGoCommand (ctx context.Context , verb string , args []string ) (* bytes.Buffer , error ) {
190
- cfg := s .config (ctx )
191
- return s .runGoCommandWithConfig (ctx , cfg , verb , args )
192
- }
193
-
194
184
func (s * snapshot ) runGoCommandWithConfig (ctx context.Context , cfg * packages.Config , verb string , args []string ) (* bytes.Buffer , error ) {
195
185
_ , runner , inv , cleanup , err := s .goCommandInvocation (ctx , cfg , true , verb , args )
196
186
if err != nil {
@@ -201,8 +191,8 @@ func (s *snapshot) runGoCommandWithConfig(ctx context.Context, cfg *packages.Con
201
191
return runner .Run (ctx , * inv )
202
192
}
203
193
204
- func (s * snapshot ) RunGoCommandPiped (ctx context.Context , verb string , args []string , stdout , stderr io.Writer ) error {
205
- cfg := s .config (ctx )
194
+ func (s * snapshot ) RunGoCommandPiped (ctx context.Context , wd , verb string , args []string , stdout , stderr io.Writer ) error {
195
+ cfg := s .config (ctx , wd )
206
196
_ , runner , inv , cleanup , err := s .goCommandInvocation (ctx , cfg , true , verb , args )
207
197
if err != nil {
208
198
return err
@@ -607,6 +597,30 @@ func (s *snapshot) CachedImportPaths(ctx context.Context) (map[string]source.Pac
607
597
return results , nil
608
598
}
609
599
600
+ func (s * snapshot ) GoModForFile (ctx context.Context , fh source.FileHandle ) (source.VersionedFileHandle , error ) {
601
+ if fh .Kind () != source .Go {
602
+ return nil , fmt .Errorf ("expected Go file, got %s" , fh .Kind ())
603
+ }
604
+ if len (s .modules ) == 0 {
605
+ if s .view .modURI == "" {
606
+ return nil , errors .New ("no modules in this view" )
607
+ }
608
+ return s .GetFile (ctx , s .view .modURI )
609
+ }
610
+ var match span.URI
611
+ for _ , m := range s .modules {
612
+ // Add an os.PathSeparator to the end of each directory to make sure
613
+ // that foo/apple/banana does not match foo/a.
614
+ if ! strings .HasPrefix (fh .URI ().Filename ()+ string (os .PathSeparator ), m .rootURI .Filename ()+ string (os .PathSeparator )) {
615
+ continue
616
+ }
617
+ if len (m .modURI ) > len (match ) {
618
+ match = m .modURI
619
+ }
620
+ }
621
+ return s .GetFile (ctx , match )
622
+ }
623
+
610
624
func (s * snapshot ) getPackage (id packageID , mode source.ParseMode ) * packageHandle {
611
625
s .mu .Lock ()
612
626
defer s .mu .Unlock ()
0 commit comments