Search code examples
luaminecraftopencomputers

Lua program throwing error with stdin after completion


I've written a lua program for OpenComputers in Minecraft which downloads a github repo to the computer, file structure and all. The code:

internet = require("internet")
io = require("io")
filesystem = require("filesystem")
handle = internet.request("https://api.github.com/repos/Vedvart/Minecraft/contents")

result = ""

for chunk in handle do result = result..chunk end

while(not (string.find(result, "download_url") == nil)) do

  i, j = string.find(result, "download_url")
  qi, qj = string.find(string.sub(result, j+4,-1), '"')

  url = string.sub(result, j + 4, j + qi + 2)

  if string.sub(url, -14) == '.gitattributes' then goto continue end
  
  ni, nj = string.find(url, "Vedvart")
  filepath = string.sub(url, nj+2, -1)

  ri, rj = string.find(string.reverse(filepath), "/")
  if not filesystem.isDirectory("/home/github/"..string.sub(filepath, 1, -ri)) then
    print('had to make')
    filesystem.makeDirectory("/home/github/"..string.sub(filepath, 1, -ri))
  end
  print('here 2')

  file_content_handle = internet.request(url)
  file_content = ''
  for chunk in file_content_handle do file_content = file_content .. chunk end

  print(filepath)
  print(io)
  print(filesystem.isDirectory("/home/github/"..string.sub(filepath, 1, -ri)))

  file = io.open('/home/github/'..filepath, 'w')
  print(file)
  file:write(file_content)
  file:close()

  print('here 3')
  print(file)

  ::continue::
  result = string.sub(result, j+qi+3, -1)

  print('here 4')

end

print('done')

The code actually runs fine, reaching the final print statement and successfully generating the file structure and downloading all files. However, after this, the program throws an error:

enter image description here

I've googled around, and struggled to even find the same error being thrown, much less in the same circumstances. It doesn't seem like the error is originating directly in my code, but other code seems to run fine, so something in my program must be triggering this error elsewhere. I'm not sure what is causing it - any help is appreciated.


Solution

  • Not sure why you need to require io. This is not necessary in vanilla Lua and looking at the example snippets at their website it shouldn't be necessary in OpenComputers either.

    io = require("io") overwrites any existing global io library and maybe that newly required table does not have a stdin field.

    Just an educated guess :)

    If your code causes this error after being completed a change in a global variable is one of the few possible reasons.