MCPcopy Create free account
hub / github.com/bufbuild/buf / getModuleForFilePathUncached

Method getModuleForFilePathUncached

private/bufpkg/bufmodule/module_set.go:371–412  ·  view source on GitHub ↗
(ctx context.Context, filePath string)

Source from the content-addressed store, hash-verified

369}
370
371func (m *moduleSet) getModuleForFilePathUncached(ctx context.Context, filePath string) (_ Module, retErr error) {
372 matchingOpaqueIDToModule := make(map[string]Module)
373 // Note that we're effectively doing an O(num_modules * num_files) operation here, which could be prohibitive.
374 for _, module := range m.Modules() {
375 _, err := module.StatFileInfo(ctx, filePath)
376 if err == nil {
377 matchingOpaqueIDToModule[module.OpaqueID()] = module
378 } else if !errors.Is(err, fs.ErrNotExist) {
379 // This is important! If we have any error other than fs.ErrNotExist, make sure we return that error.
380 // Not doing so results in important errors not being propagated. In the case where this was found,
381 // we ended up returning a fs.ErrNotExist when the underlying error was a digest verification error
382 // that we expected. We want to return the verification error up the stack.
383 return nil, err
384 }
385 }
386 switch len(matchingOpaqueIDToModule) {
387 case 0:
388 // This will happen if there is a file path we cannot find in our modules, which will result
389 // in an error on ModuleDeps() or Digest().
390 //
391 // Note that we will commonly call getModuleForFilePathUncached for a WKT, which will result
392 // in this error (this is desired).
393 return nil, &fs.PathError{Op: "stat", Path: filePath, Err: fs.ErrNotExist}
394 case 1:
395 var matchingOpaqueID string
396 for matchingOpaqueID = range matchingOpaqueIDToModule {
397 }
398 return m.GetModuleForOpaqueID(matchingOpaqueID), nil
399 default:
400 // This actually could happen, and we will want to make this error message as clear as possible.
401 // The addition of opaqueID should give us clearer error messages than we have today.
402 return nil, &DuplicateProtoPathError{
403 ProtoPath: filePath,
404 ModuleDescriptions: xslices.ToUniqueSorted(
405 xslices.Map(
406 xslices.MapValuesToSlice(matchingOpaqueIDToModule),
407 Module.Description,
408 ),
409 ),
410 }
411 }
412}
413
414func (*moduleSet) isModuleSet() {}
415

Callers 1

getModuleForFilePathMethod · 0.95

Calls 4

ModulesMethod · 0.95
GetModuleForOpaqueIDMethod · 0.95
StatFileInfoMethod · 0.65
OpaqueIDMethod · 0.65

Tested by

no test coverage detected