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

JS backend ignores first command line argument #318

Closed
marzipankaiser opened this issue Nov 28, 2023 · 7 comments · Fixed by #489
Closed

JS backend ignores first command line argument #318

marzipankaiser opened this issue Nov 28, 2023 · 7 comments · Fixed by #489
Labels
bug Something isn't working

Comments

@marzipankaiser
Copy link
Contributor

The runnables created for the JS backend always ignore the first command line argument.

Minimal Example

When compiling and running

import immutable/list
import immutable/option
import io/args
import text/string

def main() = println(commandLineArgs())

with the js backend:

effekt --compile printCliArgs.effekt
out/printCliArgs 1 2 3

we get:

Cons(2, Cons(3, Nil()))

Which ignores the 1.

Note: I thought we had an issue for that already, but could not find it. If you do, feel free to link and close.

@marzipankaiser marzipankaiser added the bug Something isn't working label Nov 28, 2023
@marzipankaiser
Copy link
Contributor Author

extern io def nativeArgs(): Array[String] = "process.argv.slice(2)"

This should be a 1 probably?

@b-studios
Copy link
Collaborator

b-studios commented Nov 28, 2023

Hmmm maybe this is a copy and paste error from a different backend? I believe Chez had two leading args that you needed to discard.

Excerpt from:
https://nodejs.org/docs/latest/api/process.html#processargv

The process.execArgv property returns the set of Node.js-specific command-line options passed when the Node.js process was launched. These options do not appear in the array returned by the process.argv property, and do not include the Node.js executable, the
name of the script, or any options following the script name. These options are useful in order to spawn child processes with the same execution environment as the parent.

node --harmony script.js --version COPY

Results in process.execArgv:

['--harmony'] COPY

And process.argv:

['/usr/local/bin/node', 'script.js', '--version']

Also the docs on argv say:

Launching the Node.js process as:

node process-args.js one two=three four COPY

Would generate the output:

0: /usr/local/bin/node
1: /Users/mjr/work/node/process-args.js
2: one
3: two=three
4: four 

Also

["/usr/local/bin/node", "/Users/mjr/work/node/process-args.js", "one", "two=three","four"].slice(2)

evaluates to

['one', 'two=three', 'four']

which is what we would expect.

@b-studios
Copy link
Collaborator

b-studios commented Nov 28, 2023

This seems related to us typically using --eval (nodejs/node#9017) and @dvdvgt wrapper not using eval. In both cases, however, at least the first argument is dropped.

node --eval "console.log(process.argv)" -- 1 2 3
[ '/usr/local/bin/node', '1', '2', '3' ]

whereas

Contents of out/clibug

#!/usr/bin/env node
console.log(process.argv)

and

> ./out/clibug 1 2 3
[
  '/usr/local/bin/node',
  '/Users/jonathan/Desktop/github/effekt/out/clibug',
  '1',
  '2',
  '3'
]

@marzipankaiser
Copy link
Contributor Author

The other part of the problem seems to be #319.

@b-studios
Copy link
Collaborator

I propose the following fix:

  1. fix JS backend: Array.toList drops first element #319
  2. change code generation of JS apps to not generate module.exports = { main: main_1202 }, but instead main_1202() as the last statement.
  3. never use --eval again, but replace with node out/clibug.js

@b-studios
Copy link
Collaborator

b-studios commented Nov 28, 2023

There might be a tiny problem that the website could potentially rely on the exports, we have to double check that. We really need more automated tests for LSP and the website.

@b-studios
Copy link
Collaborator

Another related problem is that we use an absolute path here:

val out = C.config.outputPath().getAbsolutePath

Moving the js file and the "binary" will not work (/cc @dvdvgt)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants