Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writeFile not available at compile time when using the JS backend #3124

Closed
bogen opened this issue Jul 16, 2015 · 9 comments
Closed

writeFile not available at compile time when using the JS backend #3124

bogen opened this issue Jul 16, 2015 · 9 comments

Comments

@bogen
Copy link

bogen commented Jul 16, 2015

in static: blocks or {.compileTime.} functions writeFile is not available with the JS backend.

$ cat writeFile.nim
static:
  "/dev/stdout".writeFile "\x1b[1;35mhello writeFile!\x1b[0m\n"

$ nim c -o:/dev/null --verbosity:0 writeFile.nim
hello writeFile!

$ nim js -o:/dev/null --verbosity:0 writeFile.nim
writeFile.nim(2, 16) Error: undeclared identifier: 'writeFile'

$ nim --version
Nim Compiler Version 0.11.2 (2015-07-15) [Linux: amd64]
Copyright (c) 2006-2015 by Andreas Rumpf

git hash: 45b6082c12dd6fc90a3dd3ca97e1ba157c3d6464
@bogen
Copy link
Author

bogen commented Jul 16, 2015

This issue can be worked around with staticExec:

$ cat writeFileSe.nim
import strutils

proc writeFileSe (file, text :string) {.compileTime.} =
  discard staticExec ("cat > '$1' <<-END_MARKER\n$2\nEND_MARKER\n" % [file,text])

static:
  const file = "temp.txt"
  file.writeFileSe "\x1b[1;35mhello writeFileSe!\x1b[0m\n"
  echo staticRead file

$ nim c --verbosity:0 -o:/dev/null writeFileSe.nim
hello writeFileSe!


$ nim js --verbosity:0 -o:/dev/null writeFileSe.nim
hello writeFileSe!

@dom96
Copy link
Contributor

dom96 commented Jul 26, 2015

Could this be because you called your file "writeFile.nim"?

@bogen
Copy link
Author

bogen commented Jul 27, 2015

No. See my above example. It works with the c back end. I named it writeFile.nim because that is what I was having issues with.

But again, now with a different file name, same results.

$ cat someThingElse.nim
static:
  "/dev/stdout".writeFile "\x1b[1;35mhello writeFile!\x1b[0m\n"

$ nim c -o:/dev/null --verbosity:0 someThingElse.nim
hello writeFile!

$ nim js -o:/dev/null --verbosity:0 someThingElse.nim
someThingElse.nim(2, 16) Error: undeclared identifier: 'writeFile'

@Araq Araq added the Javascript label Aug 1, 2015
@ghost
Copy link

ghost commented Nov 1, 2017

Still an issue

@marcolussetti
Copy link

Very new to Nim, but I seem to experience a variant of this still:

nim js -d:release -o:"a.js" "file.nim"
> undeclared identifier: 'open'

The relevant code in my file is:

var outputFile = open(targetFile, fmWrite)

Compilation to C works fine.

@alehander92
Copy link
Contributor

alehander92 commented Jan 10, 2019

EDIT: Nevermind, I misread and missed the compile time thing

(old comment
This would basically require supporting the node.js backend with more IO/os primitives: i've done similar stuff locally, but it seemed people are opposed to including this in the stdlib in order to motivate users to use the native backend in those cases

#7505
)

@andreaferretti
Copy link
Collaborator

At compile time you are not running on node

@alehander92
Copy link
Contributor

@andreaferretti ah, i've completely missed that part, you're right.

very bizarre

@andreaferretti
Copy link
Collaborator

Works for me now

@Araq Araq closed this as completed Feb 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants