0
0

More than 3 years have passed since last update.

[Rails]vagrant 共有フォルダが共有できない & Encoding::UndefinedConversionError

Posted at


Railsの初心者の備忘録です。

初めに

Railsでvagrantの立ち上げから行ったら、今まで作成したファイルがteratermから見れなくなってしまいました。
反対にteratermでrails newしたものがエクスプローラーから見れない、、、
そのため、共有フォルダがおかしいのかと思ったのですが、対処法が分からなかったので、ここにメモしておきます。

基本情報

・windows
・VirtualBoxでPC上に仮想環境を構築
・Vagrantで仮想環境を操作
・作成した仮想環境にSSH接続

エラー内容

下記のように、本来ならteratermでworkディレクトリ内には、エクスプローラーのvagrantディレクトリの中にあるものが表示されなければいけないのですが、表示できていません。
また、teratermでrails newしたotameshitest.appというフォルダがエクスプローラーに反映されていません。
→共有フォルダがおかしい

(※エクスプローラーのファイル名は一部隠しています)

スクリーンショット (245).png

解決方法

そこでterminalを見ると、vagrant up(reload)した際にエラーが発生していることに気が付きました。

C:\Users\world\Documents\work\vagrant>vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 3000 (guest) => 3000 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
==> default: Machine booted and ready!
[default] GuestAdditions 6.0.8 running --- OK.
==> default: Checking for guest additions in VM...
==> default: Configuring and enabling network interfaces...
==> default: Rsyncing folder: /cygdrive/c/Users/world/Documents/work/vagrant/ => /vagrant
C:/HashiCorp/Vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/util/io.rb:32:in `encode': "\xEF\xBD" from Windows-31J to UTF-8 (Encoding::UndefinedConversionError)
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/util/io.rb:32:in `read_until_block'        from C:/HashiCorp/Vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/util/subprocess.rb:194:in `block in execute'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/util/subprocess.rb:192:in `each'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/util/subprocess.rb:192:in `execute'        from C:/HashiCorp/Vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/util/subprocess.rb:22:in `execute'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/plugins/synced_folders/rsync/helper.rb:214:in `rsync_single'
        from C:/HashiCorp/Vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/plugins/synced_folders/rsync/synced_folder.rb:48:in `block in enable'
(以下略)

エラー内容の中に、下記の記述があります。

C:/HashiCorp/Vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/util/io.rb:32:in `encode': "\xEF\xBD" from Windows-31J to UTF-8 (Encoding::UndefinedConversionError)

つまり、/HashiCorp/Vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/util/io.rbというファイルの32行目のUTF-8をWindows-31Jと書き換えればいいことが分かります。

io.rb
()
while true
          begin
            if Platform.windows?
              # Windows doesn't support non-blocking reads on
              # file descriptors or pipes so we have to get
              # a bit more creative.

              # Check if data is actually ready on this IO device.
              # We have to do this since `readpartial` will actually block
              # until data is available, which can cause blocking forever
              # in some cases.
              results = ::IO.select([io], nil, nil, 1.0)
              break if !results || results[0].empty?

              # Read!
              data << io.readpartial(READ_CHUNK_SIZE).encode("Windows-31J", Encoding.default_external)   
                                                              # ↑ ここを変更
            else
              # Do a simple non-blocking read on the IO object
              data << io.read_nonblock(READ_CHUNK_SIZE)
            end
          rescue Exception => e
()

書き換えて、vagrant up(reload) すると、、、
無事にファイルを共有できました!!

※なお、私の環境下ではio.rbファイルの編集権限がなかったので、エクスプローラーからプロパティを開き、アクセス権限を許可してから編集しました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0