programing

레일 오류, YAML을 구문 분석 할 수 없습니다.

nasanasas 2020. 10. 15. 07:55
반응형

레일 오류, YAML을 구문 분석 할 수 없습니다.


보석을 업데이트 한 후 다음과 같은 결과를 얻었습니다.

/home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:148:in `parse': couldn't parse YAML at line 182 column 9 (Psych::SyntaxError)
    from /home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:148:in `parse_stream'
    from /home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:119:in `parse'
    from /home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:106:in `load'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth/formatters/latex.rb:6:in `<module:LATEX>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth/formatters/latex.rb:3:in `<top (required)>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth.rb:21:in `require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth.rb:21:in `<top (required)>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/case_sensitive_require/RedCloth.rb:6:in `require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/case_sensitive_require/RedCloth.rb:6:in `<top (required)>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:68:in `require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:66:in `each'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:66:in `block in require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:55:in `each'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:55:in `require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler.rb:120:in `require'
    from /home/megas/Work/railscasts/config/application.rb:10:in `<top (required)>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:28:in `require'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:28:in `block in <top (required)>'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:27:in `tap'
    from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:27:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

루비 -1.9.2-p136 레일 3.0.3

gem RedCloth 재설치를 시도했지만 도움이되지 않았습니다. 시스템에서 4.2.3 버전 만 사용하려고합니다.

그것을 고치는 방법을 아십니까? 감사


어딘가에 잘못된 YAML 코드가 있습니다. Psych (새로운 루비 YAML 파서)에는 유효하지 않습니다.

YAML 코드를 수정할 수 없거나 수정할 수없는 경우 이전 YAML 파서 (syck)를로드하여 시작 부분에 추가합니다. config/boot.rb

require 'yaml'
YAML::ENGINE.yamler = 'syck'

그것은 단지 '빠르고 더러운'수정일뿐입니다.


내 일반 Rails 3 앱에도이 문제가있었습니다. 날짜 / 시간에 현지화 된 yaml 파일을 사용했기 때문입니다.

이 커밋 https://github.com/rails/rails/commit/dc94d81 에서 볼 수 있듯이 배열을 별도의 줄에 배치하여 쉽게 "고정"할 수 있습니다.

         -    order: [ :year, :month, :day ]
    18  +    order:
    19  +      - :year
    20  +      - :month
    21  +      - :day

Paul Raupach의 답변에 대한 약간의 조정은 디렉토리에서 실행될 때 모든 하위 디렉토리에서 모든 * .yml 파일을 재귀 적으로 찾고 파일을 테스트합니다. Rails 루트 디렉토리에서 실행했습니다.

require 'yaml'

d = Dir["./**/*.yml"]
d.each do |file|
  begin
    puts "checking : #{file}"
    f =  YAML.load_file(file)
  rescue StandardError
    puts "failed to read #{file}: #{$!}"
  end
end

근본 원인은 여러 곳에 설명되어 있으며 다시 요약하겠습니다.

두 개의 기본 yaml 파서가 있습니다. Psych는 새로운 것입니다. Syck는 오래되고 유지되지 않고 죽어 가고 있으며 현재 libyaml이 없을 때 대체로 사용됩니다 (일반적으로 비 Linux 시스템).

중요한 것은 어딘가에 잘못된 yaml이 있다는 것 입니다. 아마도 번역 파일에있을 것입니다 (%로 표시되는 인용되지 않은 문자열이 있습니다). 프로덕션 상자에서 YAML.load_file을 사용하여 모든 yml 파일을로드하면 어떤 파일이 손상된 것을 볼 수 있습니다.


공백 대신 탭을 사용했기 때문에이 문제가 발생했습니다.


YAML 파일을 수정하는 것이 가장 좋습니다.

다음은 irb를 사용하여 작동하지 않는 rails 콘솔이 필요하지 않도록 하는 방법 입니다.

require 'yaml'
YAML::ENGINE.yamler = 'psych'
YAML.load_file('config/locales/xxx.en.yml')

문제가 어디에 있는지 알려주는 멋진 출력을 얻을 수 있습니다.

Psych::SyntaxError: couldn't parse YAML at line 25 column 17
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:148:in `parse'
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:148:in `parse_stream'
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:119:in `parse'
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:106:in `load'
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:205:in `load_file'
    from (irb):10
    from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

YAMl이 'syck'를 사용하도록 강요하여 실제 문제를 '마스킹'하는 것이 아니라 yaml 코드를 절대적으로 수정하십시오. 나는 이와 같은 문제가 있었고 내 지역화 파일에서 잘못된 yml 문을 발견했습니다. 이전 파서를 강제로 사용하면 프로젝트의 다른 곳에서 새 파서에 대한 모든 작업의 ​​이점을 얻을 수 없습니다.


원래 질문의 문제는 RedCloth에있었습니다. 동일한 문제가 발생했으며 RedCloth gem의 최신 버전 (현재 4.2.7)으로 업데이트하기 만하면 문제가 해결되었습니다.

Honza 및 FlyboyArt의 위의 조언은 건전하며 사용자 지정 YAML을 수정해야하지만 RedCloth가 인기있는만큼 인기가 있으므로 RedCloth를 사용하는이 질문을 찾는 대부분의 사용자는 GemFile에 다음 줄이 추가되었는지 확인해야합니다.

gem 'RedCloth', ">= 4.2.7"

이것을 읽는 다른 사람들을 위해 데이터베이스 구성에서 오타를 만든 후이 오류가 발생했습니다. /config/database.yml


번 들러 1.0.10 문제입니다. 자세한 내용은 여기

번 들러를 다운 데이트 해보십시오.


내 원인에서 수정 한 것은 실제로 다음과 같은 잘못된 YAML 번역 파일이었습니다.

config/locales/bg.yml

YAML 오류를 수정했으며 모두 괜찮 았습니다. :-)


이 문제를 추구하는 사람들을 위해 password : keyword와 password 사이에 공백이 없기 때문에 database.yml이이 오류를 유발한다는 것을 방금 발견했습니다. 거의 보이지 않는 오류와 이전 버전의 레일에서 오류없이 작동했던 database.yml이 있습니다.


나는이 문제가 있었다. 내 문제는 내 database.yml 파일에 추가 탭이 있다는 것입니다.


내가 구축 중인 에서 r18n 라이브러리사용 하면서이 문제를 발견Sinatra 했으며 번역 파일에는 다음이 포함되었습니다.

day: !!pl
  0: 0 days
  1: 1 day
  n: %1 days

이전 프로젝트에서는 잘 작동 Ruby 1.8.7했지만 Ruby 1.9.3.

@SB의 답변은 내 문제를 해결하는 데 필요한 단서를주었습니다. 최신 YAML은 %1. 몇 가지 빠른 파고와 실험을 통해 irb이제 최신 버전의 YAML파서에서는로 시작하는 문자열 주위에 따옴표를 넣어야 한다는 것을 알고 %1있으므로 방금 번역을 다음과 같이 변경했습니다.

day: !!pl
  0: 0 days
  1: 1 day
  n: "%1 days"

그리고 짜잔-불쾌한 오류 메시지가 사라졌습니다.


제 경우에는 번들 문제가 아닙니다. (Ruby 1.9로 가정)

  • Ruby는 기본적으로 libyaml이있는 경우 'psych'(C 라이브러리에 연결하는 최신 및 유지 관리 yaml 라이브러리 : libyaml)을 사용합니다.
  • 그렇지 않으면 Ruby는 'syck'를 사용합니다 (오래되고 유지되지 않음).
  • 따라서 YAML :: ENGINE.yamler = 'syck'는 Ruby가 'psych'도 설치된 시스템에서 'syck'를 사용하도록합니다.

여기에 더 많은 정보 : require "yaml"은 psych을 기본으로 사용하지 않습니다.


group : development 및 : test 내에 gem psych을 설치하여이 문제를 해결합니다.

gem 'psych'

나는 루비 1.9.2-p180과 같은 문제가 있었는데, 1.9.2-p290으로 가면 이것을 해결했습니다.


Though the answer given by @Vicvega may or may not work (Didn't test it) it goes against Rails and Ruby common principle "Convention over configuration" and should be treated with care (and even more in collaborative work),,, even though the "configuration" in this case is not great

so my vote goes (If i could vote) for those who proposed to eliminate the syntax errors in the YAML files.

now... to solve the error, for me it was kind of a newby error, I didn't have the locale file which I had defined to be the default in Config/application.rb in my Config/locales directory

happy coding


Need to check .yml files for error, I have found problem in my database.yml


I had a similar issue with a malformed YAML translation file. It used a variable before defining it. The following was wrong:

...
messages:
  ...
  <<: *errors_messages
...
messages: &errors_messages
...

It had to be changed to:

...
messages: &errors_messages
...
messages:
  ...
  <<: *errors_messages
...

Then it started working again.


I got this error from trying to connect to a remote db with the password 'p@ssword' and figured out that psych doesn't like the '@' symbol. Changed the DB password and problem solved.


In my case there were 2 issues.

  1. As mentioned by @stwienert, the array representation was an issue.
  2. One more thing was, if a String started with a %{var} I received a Parse exception. I had to change the strings accordingly to avoid beginning with %{var}

For example if the string was

%{user_name} welcome to %{application_name} -- This threw an error

To fix it I had to change it to

Hi, %{user_name} welcome to %{application_name}

Hope this helps someone.

Regards,

Shardul.


Well, just in case this helps...
What I did:
- select all and copy from https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale/es.yml into a new es.yml with notepad++
- tried to watch this new file with netBeans IDE text editor, I got a warning about safe load with utf8 (cannot recall the exact text). Hence did not open it with this text editor.
- switched the local thru configuration/application.rb i18n
- when I loaded a irb page I got "couldn't parse YAML at line 0 column 0" referring to Psych.
- Went to IRB and loaded the file with syck it was ok; switched to psych and got same error.

How I solved it:
- went back to copy the contents from https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale/es.yml but this time I pasted it in a newly created file with netBeans editor.
- restarted webRick.
- problem solved.
Best Regards,
Victor


Remove unused databases from database.rb. If you use MySQL and there is no PostgreSQL then delete PG database code from databases.yml.


Pshych parse is suck to the core. I am not sure if this is elegant solution but i manage to fix this problem by uninstalling it.

gem uninstall psych

I had a really, really strange problem because I had spaces after. E.g.:

title: "NASA"

Did not work, but

title:"NASA"

Did.


If you're like me and facing a project (inherited) with hundreds of fixtures a few lines of Ruby can save you hours:

require 'yaml'

d = Dir.new('test/fixtures/')
d.each do |file|
  begin
     f =  YAML.load_file('test/fixtures/' + file)
  rescue StandardError
     puts "failed to read test/fixtures/#{file}: #{$!}"
   end
 end

Just put it in your Rails root and run it, trash it when you're done.


For other people looking at this I found the issue in rerun.txt which was being called by config/cucumber.yml in a Rails app. rerun.txt was configured to store the most recent cucumber failing test and I had somehow entered weird characters for a cucumber test in the console.

That was hard to find. Wish I had seen Glenn Rempe's answer a while back.


One of the possible causes is mapping values are not allowed in this context at line ...

Here is an incorrect YAML example (user: should not contain any value actually, because it contains children items some_key and some_other_key)

customer: Customer
user: User
  some_key: value
  some_other_key: value 2

It's not a trivial task to find such issue especially if you have a huge YAML file.

I've created a pretty simple regexp to detect such things. I checked it in RubyMine

^(\s+)['"\w]+:\s?['"\w]+.*\n\1\s\s

Be careful! It doesn't work correct with special chars like å ø æ etc.

Let me know in comments if it worked for you :)

참고URL : https://stackoverflow.com/questions/4980877/rails-error-couldnt-parse-yaml

반응형