gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.39k stars 7.49k forks source link

NODE_PATH is incorrectly set in the os/exec environment when the NODE_PATH variable is already set #9800

Open Tate-CC opened 2 years ago

Tate-CC commented 2 years ago

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.96.0-2fd4a7d3d6845e75f8b8ae3a2a7bd91438967bbb+extended linux/amd64 BuildDate=2022-03-26T09:15:58Z VendorInfo=gohugoio

Does this issue reproduce with the latest release?

This issue does reproduce with the latest version.

When the NODE_PATH environment variable is unset, the Hugo exec environment correctly has NODE_PATH set as the node_modules directory of the current working directory. When the NODE_PATH is already set, however, the NODE_PATH is instead prepended with the current working directory but not the node_modules folder. This means that if you have a module which relies on locally installed modules, for example a PostCSS plugin, it can't find them and the build fails.

Tate-CC commented 2 years ago

It looks like the offending function is here: https://github.com/gohugoio/hugo/blob/41cc4e4ba3bd849cee7dcb691504ebebbfce680f/common/hugo/hugo.go#L108-L131 And the logic should instead be something like:

nodepath := filepath.Join(workDir, "node_modules")
if np := os.Getenv("NODE_PATH"); np != "" {
    nodepath = nodepath + string(os.PathListSeparator) + np
}

I'd be happy to go ahead and put this in a PR if that makes things easier 🙂