0

I use a Makefile for web development, where I compile scss (sass) files to css and I want to add a banner (header with date, version, copyright info, git branch etc) to the compiled file.

While I finally managed to achieve this, when I open the result there is <feff> displayed at the joining position.

The Makefile is like this:

BANNER:=\
    "/**\n"\
    " * @project       $(PROJECT_NAME)\n"\
    " * @author        John Doe <j.doe@example.com>\n"\
    " * @build         $(DATE)\n"\
    " * @copyright     Copyright (c) " $(shell date +%Y) ", <Example Inc.>\n"\
    " */\n"\
    "\n"

css: $(CSS_DEST_DIR)/main.css
    $(call colorecho, 3, $(shell du -h $^))

$(CSS_DEST_DIR)/main.css: $(CSS_SRC_DIR)/main.scss $(CSS_SRC_FILES)
    @mkdir -p $(CSS_DEST_DIR)
    $(call colorecho, 3, "Compiling $@");
    $(eval TMPFILE := $(shell mktemp))
    @-$(SASS_COMPILER) $(SASS_COMPILER_OPTIONS) -o $(TMPFILE) $< 
    @echo $(BANNER) | cat - $(TMPFILE) > $@

And the resulting file looks like this in vim:

/**
  * @project       data-al
  * @author        Johannes Braun <j.braun@agentur-halma.de>
  * @build         2018-12-13-1619
  * @release       gitRevSync.long() + gitRevSync.branch()
  * @copyright     Copyright (c)  2018 , <HALMA GmbH & Co. KG>
  */


<feff>.button,.button--primary,.cookie-notification__accept,.search-form__submit,.mobnav__trigger,.mobnav__button{padding:0;...

od -a outputs

0000400   nl   <EF>   <BB>   <BF>   .   b   u   t   t   o   n   ,   .   b   u   t

When I do the same on the bash command line, everything is ok. I am on OSX btyw.

How could I get rid of this? Thanks for your help

  • That is probably coming from the sass compiler, in order to mark the output as utf8. (Look at the temp file.) – Raymond Chen Dec 13 '18 at 16:27
  • The tmp file starts with `ef` `bb` `bf` and does not display the in vim. – Johannes Braun Dec 13 '18 at 19:46
  • _Vim_ is treating the file as utf-8 encoded. It sees the `ef` `bb` `bf` byte sequence, which is utf-8 for the unicode byte-order-mark (i.e., _ZERO WIDTH NO-BREAK SPACE_) which has the code point `\ufeff`. Sometimes _vim_ will strip this before displaying the data in a buffer. See `:help bomb` in _vim_. `od` is a much tool in these circumstances. – bobbogo Dec 14 '18 at 15:53
  • Possible duplicate of [How can I remove the BOM from a UTF-8 file?](https://stackoverflow.com/questions/45240387/how-can-i-remove-the-bom-from-a-utf-8-file) – tripleee Dec 14 '18 at 16:10

2 Answers2

1

I urge you strongly to dump echo. It's massively non-portable for anything other than printing simple strings followed by a newline. For example the \n won't resolve to a newline on all systems. Instead, consider using printf (the program, not the function) which is well-defined for all sorts of special characters and formatting strings.

However, as mentioned by Raymond these characters are a UTF-8 BOM (byte order mark). They're probably generated by $(SASS_COMPILER); see if there's an option to prevent that from happening.

MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • Better would be to incorporate the banner into the sass output rather that trying to stick it on after the fact. That way, the utf8 BOM remains in place so that characters in the sass output are correctly interpreted. – Raymond Chen Dec 13 '18 at 16:40
  • Same behavior with `@printf "%b" $(BANNER) | cat - $(TMPFILE) > $@`. The Sass compiler is just a wrapper around `libsass` which I have written myself (in C) [Source code](https://github.com/hannenz/gsassc/blob/master/gsassc.c) – Johannes Braun Dec 13 '18 at 19:48
  • I guess I wasn't clear enough. The first paragraph was not in any way related to your problem with the BOM. That paragraph was in response to your difficulties getting the output right, and pointing out that if you run this makefile on other systems with other implementations of echo, it will not work anymore. – MadScientist Dec 13 '18 at 20:16
  • If you look at the contents of `TEMPFILE` does it contain the BOM? It absolutely has to. It's certainly not the case that any of `echo`, `printf`, or `cat` will insert it. – MadScientist Dec 13 '18 at 20:20
  • As statet in a comment above, the TMPFILE starts with HEX: `EF` `BB` `BF` before the actual text content... so yes, it does. – Johannes Braun Dec 13 '18 at 20:41
  • 1
    Well then, it seems clear that the `SASS_COMPILER` program is generating those characters. I don't know what that is or how it works, but this question is not related to makefiles, the make program, etc. Maybe `g_file_set_contents` is adding those characters. You'll need to tag this question to draw the attention of the appropriate folks. – MadScientist Dec 14 '18 at 02:06
0

Simply strip the bom from the start before concatenation. Many options, but dos2unix will work admirably. (Yes, it will preserve any other unicode characters, including any embedded non-breaking-zero-width-spaces.)

cp ${BANNER} $@
dos2unix <${TMPFILE} >>$@
bobbogo
  • 14,989
  • 3
  • 48
  • 57