Skip to content

Windows line endings in config breaks CLI #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 4 tasks
crutchcorn opened this issue Feb 7, 2018 · 16 comments
Closed
1 of 4 tasks

Windows line endings in config breaks CLI #270

crutchcorn opened this issue Feb 7, 2018 · 16 comments

Comments

@crutchcorn
Copy link

Expected Behavior

If you have windows line endings in your config file, things should run as normal

Current Behavior

If you run the recommended echo command on Windows, it will break things as it will output Windows Line Endings.

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

Add support for windows line endings in CLI

Steps to Reproduce (for bugs)

  1. Open Windows machine
  2. Run echo "module.exports = {extends: ['@commitlint/config-angular']};" > commitlint.config.js
  3. Run commitlint

Context

Trying to setup commitlint in my project on Windows

Your Environment

Executable Version
commitlint --version 5.6.0
git --version git version 2.16.0.windows.2
node --version v8.9.1
@crutchcorn
Copy link
Author

Also seen in #231

@marionebl
Copy link
Contributor

I don't have a win32 machine and can't reproduce this on macOS:
floatdrop/require-from-string#17 (comment)

Can you please add the full stack trace to the report? Also, what terminal emulator are you using to create commitlint.config.js?

@crutchcorn
Copy link
Author

How can I get the full stack trace?

I'm using CMDer (conemu)

@marionebl
Copy link
Contributor

I meant the complete console output as you see it in the terminal window where the error is thrown.

@marionebl
Copy link
Contributor

Can you upload your commitlint.config.js as is? Maybe I can reproduce using your file.

@crutchcorn
Copy link
Author

commitlint.config.js.zip

C:\Users\CorbinCrutchley\git\test [master +3 ~0 -0 !]
λ  echo 'foo: bar' | node .\node_modules\commitlint\cli.js
C:\Users\CorbinCrutchley\git\test\commitlint.config.js:1
(function (exports, require, module, __filename, __dirname) { ��m
                                                              ^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at requireFromString (C:\Users\CorbinCrutchley\git\test\node_modules\require-from-string\index.js:28:4)
    at parseJsFile (C:\Users\CorbinCrutchley\git\test\node_modules\cosmiconfig\dist\loadJs.js:15:15)
    at <anonymous>

@marionebl
Copy link
Contributor

I fumbled around with the file (thanks for providing it!) and my tests indicate this is not caused by \r\n line endings.

It is the encoding, which is UTF-16 LE. cosmiconfig assumes config files are encoded in UTF-8 and consequently feeds a string with invalid characters to require-from-string.

There is nothing commitlint can do to support UTF-16 LE encoded files. I guess the cosmiconfig won't add support for other encodings, so your best bet will be to ensure config files are UTF-8 encoded.

@crutchcorn
Copy link
Author

I understand limitations in programs and such, but if I might offer a suggestion - it might be nice to have this documented for others in the future who may run into the same problem. I thought it was a windows issue because dos2unix fixed the problem

@ChiuMungZitAlexander
Copy link

The default config of encoding of VSCode in my machine is UTF-16 LE. I met the same problem when executing scripts in the VSCode terminal (powershell) as @crutchcorn did. It was just solved after I changed the encoding to UTF-8.

@indraraj26
Copy link

indraraj26 commented Apr 4, 2019

By Default in VS code i have UTF-8 still it gives me error

All files pass linting.
husky > commit-msg (node v8.14.0)
D:\ng\commitlint.config.js:1
(function (exports, require, module, __filename, __dirname) { ��m
                                                              ^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:617:28)
    at requireFromString (D:\ng-sma\node_modules\require-from-string\index.js:28:4)
    at parseJsFile (D:\ng-sma\node_modules\cosmiconfig\dist\loadJs.js:15:15)
    at <anonymous>
husky > commit-msg hook failed (add --no-verify to bypass)

commitlint.config.js

module.exports = {extends: ['@commitlint/config-conventional']}

@YupongUU
Copy link

YupongUU commented Apr 8, 2019

I got the same bug. Any one else solve this bug?

@ooliver1
Copy link

for anyone still coming back after this long, i had the issue and i fixed it after 30m of head smashing

Get-Content commitlint.config.js | Set-Content -Encoding utf8 commitlint.config-utf8.js

for windows then delete original and rename new (so it doesnt say used by another process)

@wangrongding
Copy link

wangrongding commented Feb 11, 2022

On Windows, must through the notepad to open the file,Through the save as, change document character set!

1644603589(1)

@crispinr
Copy link

crispinr commented Jul 22, 2022

for anyone still coming back after this long, i had the issue and i fixed it after 30m of head smashing

Get-Content commitlint.config.js | Set-Content -Encoding utf8 commitlint.config-utf8.js

for windows then delete original and rename new (so it doesnt say used by another process)

Windows:
And of course it worked, but in windows as mentioned we have to remove the old and file and set the new one for config. So I tried swapping the contents as in my case it didn't recognize the utf8.js. So I did this and it worked.

Get-Content commitlint.config.js | Set-Content -Encoding utf8 commitlint.config-utf8.js
rm .\commitlint.config.js
Get-Content commitlint.config-utf8.js | Set-Content -Encoding utf8 commitlint.config.js
rm .\commitlint.config-utf8.js
echo 'hello world' | commitlint

And yeah! It works like a charm now...

@ghost
Copy link

ghost commented Nov 30, 2022

if you are using VS Code, you could change encoding from here. It solved the issue for me

image

@felix-ping
Copy link

On Windows, must through the notepad to open the file,Through the save as, change document character set!

1644603589(1)

Thanks! It worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

9 participants