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

Config file gets read as individual letters when using ES6 imports #2145

Closed
BadIdeaException opened this issue Mar 12, 2022 · 4 comments
Closed

Comments

@BadIdeaException
Copy link

BadIdeaException commented Mar 12, 2022

Reading a config file with the config option parses every letter as a separate option when yargs is used through an ES6 import.

For example, if this is foo.json:

{
	"foo": "bar"
}

The following code:

import _yargs from 'yargs'
const yargs = _yargs(process.argv);
const config = yargs.config().argv;
console.log(config);

Produces this result:

{
  '0': '{',
  '1': '\n',
  '2': '\t',
  '3': '"',
  '4': 'f',
  '5': 'o',
  '6': 'o',
  '7': '"',
  '8': ':',
  '9': ' ',
  '10': '"',
  '11': 'b',
  '12': 'a',
  '13': 'r',
  '14': '"',
  '15': '\n',
  '16': '}',
  _: [ '/usr/bin/node', '/vagrant/test.js' ],
  config: 'foo.json',
  '$0': 'test.js'
}

Evidently, every character from foo.json gets parsed as a separate, number-keyed option.

It works fine when using yargs through require:

const _yargs = require('yargs');
const yargs = _yargs(process.argv);
const config = yargs.config().argv;
console.log(config);

gives the expected result:

{
  _: [ '/usr/bin/node', '/vagrant/test.cjs' ],
  config: 'foo.json',
  foo: 'bar',
  '$0': 'test.cjs'
}
@bcoe
Copy link
Member

bcoe commented Mar 19, 2022

@BadIdeaException thank you for the bug report. Would happily take a patch if anyone is interested in contributing.

@jly36963
Copy link
Contributor

At one point, there were problems with the way config files were imported (esm):
issue
fix

Instead of iterating through keys of a parsed object, it was iterating through indices of the unparsed json string. Does this make sense?

I'm having trouble reproducing, but I'm using the (post-fix) yargs version 17.4

Example

index.js

yargs("cmd1 --settings ./config.json")
  .command(
    "cmd1",
    "cmd1 desc",
    (yargs) =>
      yargs.option("opt1", {
        describe: "opt1 description",
        type: "string",
        required: true,
      })
      .config("settings"),
    (argv) => {
      console.log({ argv });
    }
  )
  .parse()

config.json

{
  "opt1": "The day will come when people truly understand one another"
}

output

{
  argv: {
    _: [ 'cmd1' ],
    settings: './config.json',
    opt1: 'The day will come when people truly understand one another',
    '$0': 'index.js'
  }
}

@jly36963 jly36963 self-assigned this Mar 27, 2022
@bcoe
Copy link
Member

bcoe commented Mar 28, 2022

@BadIdeaException is this still happening to you on the most up-to-date version of yargs?

@BadIdeaException
Copy link
Author

Now working as expected. Thank you for the quick fix. 👍

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

3 participants