From e4efa40ba08ca82d10673e0a9f46f2a2e530bc83 Mon Sep 17 00:00:00 2001 From: Bryan Andrews Date: Wed, 13 Jul 2022 12:34:41 -0400 Subject: [PATCH] Add mutex to ensure struct gets initialized once --- depguard.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/depguard.go b/depguard.go index b727549..d7011cd 100644 --- a/depguard.go +++ b/depguard.go @@ -7,6 +7,7 @@ import ( "path" "sort" "strings" + "sync" "github.com/gobwas/glob" "golang.org/x/tools/go/loader" @@ -61,6 +62,9 @@ type Depguard struct { globIgnoreFileRules []negatableGlob prefixRoot []string + + isInitialized bool + isInitializedMutex sync.Mutex } // Run checks for dependencies given the program and validates them against @@ -102,6 +106,13 @@ func (dg *Depguard) Run(config *loader.Config, prog *loader.Program) ([]*Issue, } func (dg *Depguard) initialize(config *loader.Config, prog *loader.Program) error { + dg.isInitializedMutex.Lock() + defer dg.isInitializedMutex.Unlock() + + if dg.isInitialized { + return nil + } + // parse ordinary guarded packages for _, pkg := range dg.Packages { if strings.ContainsAny(pkg, "!?*[]{}") { @@ -168,6 +179,7 @@ func (dg *Depguard) initialize(config *loader.Config, prog *loader.Program) erro } } + dg.isInitialized = true return nil }