Fix it so disable a module does not disable transitive dependency required by others

The motivation behind the original implementation was probably to show disabled modules when running `hugo mod graph`.

Fixes #11376
This commit is contained in:
Bjørn Erik Pedersen 2023-08-23 12:39:24 +02:00
parent 9a8c84d600
commit dcf425c846
8 changed files with 37 additions and 42 deletions

View file

@ -140,7 +140,7 @@ func (c ConfigLanguage) GetConfigSection(s string) any {
return c.config.Permalinks return c.config.Permalinks
case "minify": case "minify":
return c.config.Minify return c.config.Minify
case "activeModules": case "allModules":
return c.m.Modules return c.m.Modules
case "deployment": case "deployment":
return c.config.Deployment return c.config.Deployment

View file

@ -84,7 +84,7 @@ func LoadConfig(d ConfigSourceDescriptor) (*Configs, error) {
return nil, fmt.Errorf("failed to create config: %w", err) return nil, fmt.Errorf("failed to create config: %w", err)
} }
configs.Modules = moduleConfig.ActiveModules configs.Modules = moduleConfig.AllModules
configs.ModulesClient = modulesClient configs.ModulesClient = modulesClient
if err := configs.Init(); err != nil { if err := configs.Init(); err != nil {
@ -471,7 +471,7 @@ func (l *configLoader) loadModules(configs *Configs) (modules.ModulesConfig, *mo
ex := hexec.New(conf.Security) ex := hexec.New(conf.Security)
hook := func(m *modules.ModulesConfig) error { hook := func(m *modules.ModulesConfig) error {
for _, tc := range m.ActiveModules { for _, tc := range m.AllModules {
if len(tc.ConfigFilenames()) > 0 { if len(tc.ConfigFilenames()) > 0 {
if tc.Watch() { if tc.Watch() {
l.ModulesConfigFiles = append(l.ModulesConfigFiles, tc.ConfigFilenames()...) l.ModulesConfigFiles = append(l.ModulesConfigFiles, tc.ConfigFilenames()...)

View file

@ -83,7 +83,7 @@ func New(fs *hugofs.Fs, cfg config.AllProvider) (*Paths, error) {
} }
func (p *Paths) AllModules() modules.Modules { func (p *Paths) AllModules() modules.Modules {
return p.Cfg.GetConfigSection("activeModules").(modules.Modules) return p.Cfg.GetConfigSection("allModules").(modules.Modules)
} }
// GetBasePath returns any path element in baseURL if needed. // GetBasePath returns any path element in baseURL if needed.

View file

@ -153,10 +153,6 @@ func (c *Client) Graph(w io.Writer) error {
continue continue
} }
prefix := ""
if module.Disabled() {
prefix = "DISABLED "
}
dep := pathVersion(module.Owner()) + " " + pathVersion(module) dep := pathVersion(module.Owner()) + " " + pathVersion(module)
if replace := module.Replace(); replace != nil { if replace := module.Replace(); replace != nil {
if replace.Version() != "" { if replace.Version() != "" {
@ -166,7 +162,7 @@ func (c *Client) Graph(w io.Writer) error {
dep += " => " + replace.Dir() dep += " => " + replace.Dir()
} }
} }
fmt.Fprintln(w, prefix+dep) fmt.Fprintln(w, dep)
} }
return nil return nil

View file

@ -109,11 +109,8 @@ func (h *Client) collect(tidy bool) (ModulesConfig, *collector) {
} }
type ModulesConfig struct { type ModulesConfig struct {
// All modules, including any disabled.
AllModules Modules
// All active modules. // All active modules.
ActiveModules Modules AllModules Modules
// Set if this is a Go modules enabled project. // Set if this is a Go modules enabled project.
GoModulesFilename string GoModulesFilename string
@ -123,7 +120,7 @@ type ModulesConfig struct {
} }
func (m ModulesConfig) HasConfigFile() bool { func (m ModulesConfig) HasConfigFile() bool {
for _, mod := range m.ActiveModules { for _, mod := range m.AllModules {
if len(mod.ConfigFilenames()) > 0 { if len(mod.ConfigFilenames()) > 0 {
return true return true
} }
@ -133,17 +130,11 @@ func (m ModulesConfig) HasConfigFile() bool {
} }
func (m *ModulesConfig) setActiveMods(logger loggers.Logger) error { func (m *ModulesConfig) setActiveMods(logger loggers.Logger) error {
var activeMods Modules
for _, mod := range m.AllModules { for _, mod := range m.AllModules {
if !mod.Config().HugoVersion.IsValid() { if !mod.Config().HugoVersion.IsValid() {
logger.Warnf(`Module %q is not compatible with this Hugo version; run "hugo mod graph" for more information.`, mod.Path()) logger.Warnf(`Module %q is not compatible with this Hugo version; run "hugo mod graph" for more information.`, mod.Path())
} }
if !mod.Disabled() {
activeMods = append(activeMods, mod)
} }
}
m.ActiveModules = activeMods
return nil return nil
} }
@ -228,7 +219,7 @@ func (c *collector) getVendoredDir(path string) (vendoredModule, bool) {
return v, found return v, found
} }
func (c *collector) add(owner *moduleAdapter, moduleImport Import, disabled bool) (*moduleAdapter, error) { func (c *collector) add(owner *moduleAdapter, moduleImport Import) (*moduleAdapter, error) {
var ( var (
mod *goModule mod *goModule
@ -318,7 +309,6 @@ func (c *collector) add(owner *moduleAdapter, moduleImport Import, disabled bool
ma := &moduleAdapter{ ma := &moduleAdapter{
dir: moduleDir, dir: moduleDir,
vendor: vendored, vendor: vendored,
disabled: disabled,
gomod: mod, gomod: mod,
version: version, version: version,
// This may be the owner of the _vendor dir // This may be the owner of the _vendor dir
@ -343,7 +333,7 @@ func (c *collector) add(owner *moduleAdapter, moduleImport Import, disabled bool
return ma, nil return ma, nil
} }
func (c *collector) addAndRecurse(owner *moduleAdapter, disabled bool) error { func (c *collector) addAndRecurse(owner *moduleAdapter) error {
moduleConfig := owner.Config() moduleConfig := owner.Config()
if owner.projectMod { if owner.projectMod {
if err := c.applyMounts(Import{}, owner); err != nil { if err := c.applyMounts(Import{}, owner); err != nil {
@ -352,17 +342,18 @@ func (c *collector) addAndRecurse(owner *moduleAdapter, disabled bool) error {
} }
for _, moduleImport := range moduleConfig.Imports { for _, moduleImport := range moduleConfig.Imports {
disabled := disabled || moduleImport.Disable if moduleImport.Disable {
continue
}
if !c.isSeen(moduleImport.Path) { if !c.isSeen(moduleImport.Path) {
tc, err := c.add(owner, moduleImport, disabled) tc, err := c.add(owner, moduleImport)
if err != nil { if err != nil {
return err return err
} }
if tc == nil || moduleImport.IgnoreImports { if tc == nil || moduleImport.IgnoreImports {
continue continue
} }
if err := c.addAndRecurse(tc, disabled); err != nil { if err := c.addAndRecurse(tc); err != nil {
return err return err
} }
} }
@ -531,7 +522,7 @@ func (c *collector) collect() {
projectMod := createProjectModule(c.gomods.GetMain(), c.ccfg.WorkingDir, c.moduleConfig) projectMod := createProjectModule(c.gomods.GetMain(), c.ccfg.WorkingDir, c.moduleConfig)
if err := c.addAndRecurse(projectMod, false); err != nil { if err := c.addAndRecurse(projectMod); err != nil {
c.err = err c.err = err
return return
} }

View file

@ -40,9 +40,6 @@ type Module interface {
// Directory holding files for this module. // Directory holding files for this module.
Dir() string Dir() string
// This module is disabled.
Disabled() bool
// Returns whether this is a Go Module. // Returns whether this is a Go Module.
IsGoMod() bool IsGoMod() bool
@ -81,7 +78,6 @@ type moduleAdapter struct {
dir string dir string
version string version string
vendor bool vendor bool
disabled bool
projectMod bool projectMod bool
owner Module owner Module
@ -115,10 +111,6 @@ func (m *moduleAdapter) Dir() string {
return m.gomod.Dir return m.gomod.Dir
} }
func (m *moduleAdapter) Disabled() bool {
return m.disabled
}
func (m *moduleAdapter) IsGoMod() bool { func (m *moduleAdapter) IsGoMod() bool {
return m.gomod != nil return m.gomod != nil
} }

View file

@ -0,0 +1,15 @@
hugo mod graph
stdout 'withhugotoml.*commonmod'
-- hugo.toml --
title = "Hugo Modules Test"
[module]
[[module.imports]]
path="github.com/gohugoio/hugo-mod-integrationtests/withconfigtoml"
disable = true
[[module.imports]]
path="github.com/gohugoio/hugo-mod-integrationtests/withhugotoml"
-- go.mod --
module foo
go 1.19

View file

@ -20,5 +20,6 @@ go 1.19
module github.com/gohugoio/testmod module github.com/gohugoio/testmod
-- golden/vendor.txt -- -- golden/vendor.txt --
# github.com/gohugoio/hugo-mod-integrationtests/withconfigtoml v1.0.0 # github.com/gohugoio/hugo-mod-integrationtests/withconfigtoml v1.1.0
# github.com/gohugoio/hugo-mod-integrationtests/withhugotoml v1.0.0 # github.com/gohugoio/hugo-mod-integrationtests/commonmod v0.0.0-20230823103305-919cefe8a425
# github.com/gohugoio/hugo-mod-integrationtests/withhugotoml v1.1.0