hugo icon indicating copy to clipboard operation
hugo copied to clipboard

PostCSS and TailwindCSS on Windows do not work in directories with spaces

Open Heasummn opened this issue 5 years ago • 17 comments

I'm still having this issue as of 5/28/2020. I've narrowed down the issue to here: https://github.com/gohugoio/hugo/blob/6add6d77b48cf0aab8b39d7a2bddedb1aa2a52b8/resources/resource_transformers/postcss/postcss.go#L192

Specifically, Go's exec.Command will treat spaces in parameters as different parameters, so they must be escaped or quoted if they have spaces.

I do not have the capabilities (right now) to get Hugo building on my machine, but I believe the simplest fix will be somehow escaping the string when passing it to exec.Command.

I believe, but cannot be certain, that this line should fix this issue:

configFile = `"` + configFile + `"`

This was closed as stale in #6283, but I think my solution should fix the problem.

Heasummn avatar May 28 '20 22:05 Heasummn

Perhaps it would be better to use the Quote() function in strconv, e.g.

configFile = strconv.Quote(configFile)

Though I'm not sure:

  1. If configFile will ever already be defined with quotes,

  2. If Quote() already checks if it's quoted (I don't think it does).

I could test some changes if someone has an example repository that uses PostCSS.

exprez135 avatar May 30 '20 01:05 exprez135

I don't have a minimal failing example, but anything using the PostCSS hook that is built into Hugo should trigger this error.

{{ $css := resources.Get "css/main.css" }}
{{ $style := $css | resources.PostCSS }}

on your CSS file with PostCSS installed in a folder where postcss.config.js has a space in the path will not work.

Heasummn avatar Jun 11 '20 01:06 Heasummn

Was postcss-cli installed globally (npm -g) or locally within the project directory?

jmooring avatar Jun 16 '20 00:06 jmooring

I can confirm this is still an issue. I have tried with both npm -g postcss-cli and a local installation.

jannepeltola avatar Jan 13 '21 14:01 jannepeltola

Please share your hugo version output.

moorereason avatar Jan 13 '21 16:01 moorereason

Apologies for not doing that right away - here you go:

Hugo Static Site Generator v0.80.0/extended windows/amd64 BuildDate: unknown

It is the current version available on Chocolatey.

jannepeltola avatar Jan 13 '21 16:01 jannepeltola

If it's helpful, I can spend some time tomorrow to create a minimal repro? Unfortunately I probably won't have time to do it today.

jannepeltola avatar Jan 13 '21 16:01 jannepeltola

A minimal demo repo is always helpful.

moorereason avatar Jan 13 '21 17:01 moorereason

So here's a repro.

With https://github.com/jannepeltola/hugo_issue_7333/, here's what happens:

C:\Users\Janne Peltola\Documents\GitHub\hugo_issue_7333>hugo env
Hugo Static Site Generator v0.80.0/extended windows/amd64 BuildDate: unknown
GOOS="windows"
GOARCH="amd64"
GOVERSION="go1.15.1"

C:\Users\Janne Peltola\Documents\GitHub\hugo_issue_7333>hugo server
Start building sites …
WARN 2021/01/14 15:54:56 found no layout file for "HTML" for kind "taxonomy": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
WARN 2021/01/14 15:54:56 found no layout file for "HTML" for kind "taxonomy": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
Built in 24 ms
Error: Error building site: POSTCSS: failed to transform "css/main.css" (text/css): 'C:\Users\Janne' is not recognized as an internal or external command,
operable program or batch file.

jannepeltola avatar Jan 14 '21 13:01 jannepeltola

The underlying issue is that we're executing postcss.cmd. The cmd.exe command (which runs .bat and .cmd files) processes arguments differently than the rest of Windows. The os/exec package doesn't escape commands properly for cmd.exe usage.

Related upstream issues: https://github.com/golang/go/issues/15566 https://github.com/golang/go/issues/17149

moorereason avatar Jan 14 '21 17:01 moorereason

I see. Thanks for looking into this! I'll run Hugo inside Docker for the time being then until when (if...) the upstream issue gets resolved.

jannepeltola avatar Jan 15 '21 12:01 jannepeltola

It's a frustrating workaround, but it looks like adding quotes to the exec command should solve this issue. I can set up an environment to test that change.

Heasummn avatar Jan 19 '21 03:01 Heasummn

I can build and test a branch locally if you need a Windows environment!

jannepeltola avatar Jan 19 '21 07:01 jannepeltola

This came up again yesterday: https://discourse.gohugo.io/t/failed-to-transform-path-not-found/37309

On Windows, reproduce with:

git clone --single-branch -b hugo-github-issue-7333 https://github.com/jmooring/hugo-testing "hugo github issue 7333"
cd "hugo github issue 7333"
npm install
hugo server

There's a workaround where you don't have to change the project path:

  1. Delete the postcss.config.js file from the root of the project directory.
  2. Add the following (or similar) to package.json
  "postcss": {
    "plugins": {
      "autoprefixer": {}
    }
  }

jmooring avatar Feb 23 '22 09:02 jmooring

This came up again yesterday: https://discourse.gohugo.io/t/postcss-error/39101

jmooring avatar Jun 16 '22 16:06 jmooring

Came across this today also. Project root had a folder in its path with a space and PostCSS errored out.

Update: For unrelated reason, I setup WSL1 on Win10 with node and hugo 104.1 to build/serve/watch my project which is on the windows mount and has spaces in the folders of the path and PostCSS works fine in this case.

anakinsleftleg avatar Sep 25 '22 23:09 anakinsleftleg