Ruby syntax error


#1

i am trying to write simple cookbook which can replace the file , but i am getting syntax error . no idea where i am getting the error:
default.rb:
cookbook_file '/etc/motd' do
source "motd"
end

kinfe.rb:
current_dir = File.dirname(FILE)
log_level :info
log_location STDOUT
node_name "prashi89"
client_key "#{current_dir}/prashi89.pem"
chef_server_url "https://api.chef.io/organizations/prasanthinc"
cookbook_path ["#{current_dir}/../cookbooks"]

PS C:\opscode\chef-starter\chef-repo\cookbooks\motd> knife cookbook upload motd -V
INFO: Using configuration from C:/opscode/chef-starter/chef-repo/.chef/knife.rb
Uploading motd [0.1.1]
INFO: Validating ruby files
FATAL: Cookbook file has a ruby syntax error:


#2

can any one help , what is the syntax error in above files ?


#3

I don't see the syntax error in what you've posted.

If you have the chefdk installed consider running chef exec cookstyle in the cookbook folder and look for errors marked with E. It should give a line number and a short explanation of the syntax error.

You can also run ruby -c recipes/default.rb to check for syntax errors in any .rb file if you don't have the chefdk. Even better if you incorporate automated testing at any level (lint, unit, integration) into your workflow all of those tools will catch syntax errors fairly quickly and with more info than knife cookbook upload.


#4

HI Chris ,

Thanks for the response. i execute cookstyle and i found below offenses, but what is the error in this i cant get it . can you find anything wrong in these configs ?

PS C:\opscode\chef-starter\chef-repo\cookbooks> chef exec cookstyle
Inspecting 9 files
CC

Offenses:

motd/Berksfile:1:1: C: Layout/EndOfLine: Carriage return character detected.
source 'https://supermarket.chef.io' ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
motd/metadata.rb:1:1: C: Layout/EndOfLine: Carriage return character detected.
name 'motd' ...
^^^^^^^^^^^

2 files inspected, 2 offenses detected

Berks file:
source 'https://supermarket.chef.io'
metadata

metadata.rb:
name 'motd'
maintainer 'The Authors'
maintainer_email 'you@example.com'
license 'All Rights Reserved'
description 'Installs/Configures motd'
long_description 'Installs/Configures motd'
version '0.1.1'
chef_version '>= 12.14' if respond_to?(:chef_version)


#5

Hi prashi89,

No, I don't see the error there either, but it's strange that it only says "2 files inspected" it should be more than just the Berksfile and metadata.rb. (For example it should at least include the default.rb recipe you quoted above!) It seems to start off by saying "Inspecting 9 files" but then only finishes after 2.

I'm not sure what's happening. You can perhaps do chef exec cookstyle -L to list all the 9 files it plans to inspect and then chef exec cookstyle motd\recipes\default.rb for each of the listed files until you find a more serious error or perhaps the cause of this unusual behaviour.


#6

HI Chris,
now it shows more files: but how to fix this carriage return character detected error and invalid byte sequence error "Lint/Syntax: Invalid byte sequence in utf-8."

PS C:\opscode\chef-starter\chef-repo\cookbooks\motd> chef exec cookstyle
Inspecting 6 files
CCFCCC

Offenses:

Berksfile:1:1: C: Layout/EndOfLine: Carriage return character detected.
source 'https://supermarket.chef.io' ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Berksfile:2:9: C: Layout/TrailingBlankLines: Final newline missing.
metadata

metadata.rb:1:1: C: Layout/EndOfLine: Carriage return character detected.
name 'motd' ...
^^^^^^^^^^^
recipes/default.rb:1:1: F: Lint/Syntax: Invalid byte sequence in utf-8.
spec/spec_helper.rb:1:1: C: Layout/EndOfLine: Carriage return character detected.

frozen_string_literal: true ...

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/unit/recipes/default_spec.rb:1:1: C: Layout/EndOfLine: Carriage return character detected.

...

^
test/integration/default/default_test.rb:1:1: C: Layout/EndOfLine: Carriage return character detected.

# encoding: utf-8 ...

^^^^^^^^^^^^^^^^^^^

6 files inspected, 7 offenses detected


#7

for each file i try for default.rb recipe:

PS C:\opscode\chef-starter\chef-repo\cookbooks\motd> chef exec cookstyle recipes\default.rb
Inspecting 1 file

0 files inspected, no offenses detected
invalid byte sequence in UTF-8
C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/parser-2.5.1.2/lib/parser/source/buffer.rb:166:in source=' C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/cached_data.rb:47:indeserialize_offenses'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/cached_data.rb:13:in from_json' C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/result_cache.rb:94:inload'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/runner.rb:119:in file_offense_cache' C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/runner.rb:109:infile_offenses'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/runner.rb:100:in process_file' C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/runner.rb:78:inblock in each_inspected_file'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/runner.rb:75:in each' C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/runner.rb:75:inreduce'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/runner.rb:75:in each_inspected_file' C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/runner.rb:67:ininspect_files'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/runner.rb:39:in run' C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/cli.rb:156:inexecute_runner'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/cli.rb:84:in execute_runners' C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/lib/rubocop/cli.rb:41:inrun'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/bin/rubocop:13:in block in <top (required)>' C:/opscode/chefdk/embedded/lib/ruby/2.5.0/benchmark.rb:308:inrealtime'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/rubocop-0.55.0/bin/rubocop:12:in <top (required)>' C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/cookstyle-3.0.0/bin/cookstyle:13:inload'
C:/opscode/chefdk/embedded/lib/ruby/gems/2.5.0/gems/cookstyle-3.0.0/bin/cookstyle:13:in <top (required)>' C:/opscode/chefdk/bin/cookstyle:306:inload'
C:/opscode/chefdk/bin/cookstyle:306:in `'


#8

Most of those are warnings, the actual error is denoted by the F for "fatal". recipes/default.rb:1:1: F: Lint/Syntax: Invalid byte sequence in utf-8.

It looks like you have invalid utf-8 encoding in your source files. Ruby assumes the source files are utf-8 (and you also have # encoding: utf-8 in there to set it explicitly). The best course of action is to find out how to force utf-8 encoding in your editor and resave the files in that encoding, but the exact process depends on the text editor you're using.


#9

Excellent Chris, Thanks a lot . i am able to upload now the cookbook to server . i changed the editor to ut-8 encoding option