gohugoio / hugo

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

PostCSS on Windows does not work in directories with spaces #7333

Open Heasummn opened 4 years ago

Heasummn commented 4 years ago

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.

exprez135 commented 4 years ago

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.

Heasummn commented 4 years ago

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.

jmooring commented 4 years ago

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

jannepeltola commented 3 years ago

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

moorereason commented 3 years ago

Please share your hugo version output.

jannepeltola commented 3 years ago

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 commented 3 years ago

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.

moorereason commented 3 years ago

A minimal demo repo is always helpful.

jannepeltola commented 3 years ago

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.
moorereason commented 3 years ago

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

jannepeltola commented 3 years ago

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.

Heasummn commented 3 years ago

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.

jannepeltola commented 3 years ago

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

jmooring commented 2 years ago

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 commented 2 years ago

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

anakinsleftleg commented 1 year ago

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.